标签:
Manacher算法学习资料:http://blog.csdn.net/dyx404514/article/details/42061017
aaaa abab
4 3
#include<stdio.h> #include<iostream> #include<math.h> #include<stdlib.h> #include<ctype.h> #include<algorithm> #include<vector> #include<string> #include<queue> #include<stack> #include<set> #include<map> using namespace std; const int N = 110055; int p[2 * N];//记录回文半径 char str0[N];//原始串 char str[2 * N];//转换后的串 void init() { int i, l; str[0] = '@'; str[1] = '#'; for (i = 0, l = 2; str0[i]; i++, l += 2) { str[l] = str0[i]; str[l + 1] = '#'; } str[l] = 0; } int solve() { int ans = 0; int i, mx, id; mx = 0;//mx即为当前计算回文串最右边字符的最大值 for (i = 1; str[i]; i++) { if (mx>i) p[i] = p[2 * id - i]>(mx - i) ? (mx - i) : p[2 * id - i]; else p[i] = 1;//如果i>=mx,要从头开始匹配 while (str[i + p[i]] == str[i - p[i]]) p[i]++; if (i + p[i]>mx)//若新计算的回文串右端点位置大于mx,要更新po和mx的值 { mx = i + p[i]; id = i; } ans = max(ans,p[i]); } return ans - 1; } int main() { while (scanf("%s", str0) != -1) { init(); printf("%d\n", solve()); } return 0; }
标签:
原文地址:http://blog.csdn.net/u014427196/article/details/44065307