1 #include <map> 2 #include <set> 3 #include <list> 4 #include <stack> 5 #include <queue> 6 #include <cmath> 7 #include <ctime> 8 #include <cstdio> 9 #include <vector> 10 #include <cstdlib> 11 #include <complex> 12 #include <cstring> 13 #include <iostream> 14 #include <algorithm> 15 16 #define REP(i,N) for (int i = 0;i < (N);i++) 17 #define REP_1(i,N) for (int i = 1;i < (N);i++) 18 #define REP_2(i,be,en) for (int i = (be);i < (en);i++) 19 #define DWN(i,N) for (int i = (N);i >= 0;i--) 20 #define DWN_1(i,N) for (int i = (N);i >= 1;i--) 21 #define DWN_2(i,en,be) for (int i = (en);i >= (be);i--) 22 #define INF 0x3f3f3f3f 23 #define MAXN 1000100 24 #define FR(N) freopen((N),"r",stdin) 25 #define FW(N) freopen((N),"w",stdout) 26 #define GETS(ch) fgets((ch),MAXN,stdin); 27 using namespace std; 28 29 typedef long long LL; 30 typedef map<string,int> MINT; 31 typedef vector<int> VINT; 32 33 const int M = 110010*2; 34 char str[M];//start from index 1 35 int p[M]; 36 char s[M]; 37 int n; 38 void checkmax(int &ans,int b){ 39 if(b>ans) ans=b; 40 } 41 inline int min(int a,int b){ 42 return a<b?a:b; 43 } 44 void kp(){ 45 int i; 46 int mx = 0; 47 int id; 48 REP_1(i,n) { 49 if( mx > i ) 50 p[i] = min( p[2*id-i], p[id]+id-i ); 51 else 52 p[i] = 1; 53 for(; str[i+p[i]] == str[i-p[i]]; p[i]++) ; 54 if( p[i] + i > mx ) { 55 mx = p[i] + i; 56 id = i; 57 } 58 } 59 } 60 void pre() { 61 int i,j,k; 62 n = strlen(s); 63 str[0] = ‘$‘; 64 str[1] = ‘#‘; 65 REP(i,n) { 66 str[i*2 + 2] = s[i]; 67 str[i*2 + 3] = ‘#‘; 68 } 69 n = n*2 + 2; 70 str[n] = 0; 71 } 72 73 void pt() { 74 int i; 75 int ans = 0; 76 REP(i,n) 77 checkmax(ans, p[i]); 78 printf("%d\n", ans-1); 79 } 80 81 int main() { 82 //FR("1.txt"); 83 int _=0; 84 int N; 85 cin >> N; 86 while( N-- ) { 87 scanf("%s", s); 88 pre(); 89 kp(); 90 pt(); 91 } 92 return 0; 93 }
原文:http://www.cnblogs.com/xiaoshanshan/p/4298972.html