标签:
aaaa abab
4 3
1 #include <bits/stdc++.h> 2 using namespace std; 3 const int maxn = 200010; 4 struct PalindromicTree{ 5 int son[maxn][26],fail[maxn],len[maxn],s[maxn]; 6 int tot,last,n,maxlen; 7 int newnode(int slen = 0){ 8 len[tot] = slen; 9 memset(son[tot],0,sizeof son[tot]); 10 return tot++; 11 } 12 void init(){ 13 maxlen = tot = last = n = 0; 14 newnode(0); 15 newnode(-1); 16 fail[0] = fail[1] = 1; 17 s[n] = -1; 18 } 19 int getFail(int x){ 20 while(s[n - len[x] -1] != s[n]) x = fail[x]; 21 return x; 22 } 23 void extend(int c){ 24 s[++n] = c; 25 int cur = getFail(last); 26 if(!son[cur][c]){ 27 int x = newnode(len[cur] + 2); 28 fail[x] = son[getFail(fail[cur])][c]; 29 son[cur][c] = x; 30 maxlen = max(maxlen,len[x]); 31 } 32 last = son[cur][c]; 33 } 34 }pt; 35 char str[maxn]; 36 int main(){ 37 while(~scanf("%s",str)){ 38 pt.init(); 39 for(int i = 0; str[i]; ++i) 40 pt.extend(str[i] - ‘a‘); 41 printf("%d\n",pt.maxlen); 42 } 43 return 0; 44 }
标签:
原文地址:http://www.cnblogs.com/crackpotisback/p/4913965.html