和上一题一样,不过这题只是要求最长回文子串的长度
在此采用了非常好用的Manacher算法
据说还是O(n) 的效率QAQ
详细用法参考了上篇博客的参考资料,这两天有空学习一下~
Source code:
//#pragma comment(linker, "/STACK:16777216") //for c++ Compiler #include <stdio.h> #include <iostream> #include <fstream> #include <cstring> #include <cmath> #include <stack> #include <string> #include <map> #include <set> #include <list> #include <queue> #include <vector> #include <algorithm> #define Max(a,b) (((a) > (b)) ? (a) : (b)) #define Min(a,b) (((a) < (b)) ? (a) : (b)) #define Abs(x) (((x) > 0) ? (x) : (-(x))) #define MOD 1000000007 #define pi acos(-1.0) using namespace std; typedef long long ll ; typedef unsigned long long ull ; typedef unsigned int uint ; typedef unsigned char uchar ; template<class T> inline void checkmin(T &a,T b){if(a>b) a=b;} template<class T> inline void checkmax(T &a,T b){if(a<b) a=b;} const double eps = 1e-7 ; const int N = 1 ; const int M = 1100011*2 ; const ll P = 10000000097ll ; char str[M];//start from index 1 int p[M]; char s[M]; int n; void kp(){ int i; int mx = 0; int id; for(i = 1; i < n; ++i){ if( mx > i ) p[i] = Min( p[2*id-i], p[id]+id-i ); else p[i] = 1; for(; str[i+p[i]] == str[i-p[i]]; p[i]++) ; if( p[i] + i > mx ){ mx = p[i] + i; id = i; } } } void pre(){ int i,j,k; n = strlen(s); str[0] = '$'; str[1] = '#'; for(i = 0; i < n; ++i){ str[i*2 + 2] = s[i]; str[i*2 + 3] = '#'; } n = n*2 + 2; str[n] = 0; } int main(){ int T,_=0, t, i, ans; while(EOF != scanf("%s", s)){ ans = 0; pre(); kp(); for(i = 0; i < n; ++i) checkmax(ans, p[i]); printf("%d ", ans-1); } return 0; }