标签:
aba
1 2 3
1 #include <bits/stdc++.h> 2 using namespace std; 3 const int maxn = 100010; 4 struct PalindromicTree{ 5 int fail[maxn],len[maxn],son[maxn][26]; 6 int tot,last,n; 7 char s[maxn]; 8 int newnode(int slen = 0){ 9 memset(son[tot],0,sizeof son[tot]); 10 len[tot] = slen; 11 return tot++; 12 } 13 void init(){ 14 n = tot = last = 0; 15 newnode(0); 16 newnode(-1); 17 fail[1] = fail[0] = 1; 18 s[n] = -1; 19 } 20 int getFail(int x){ 21 while(s[n - len[x] - 1] != s[n]) x = fail[x]; 22 return x; 23 } 24 void extend(int c){ 25 s[++n] = c; 26 int cur = getFail(last); 27 if(!son[cur][c]){ 28 int x = newnode(len[cur] + 2); 29 fail[x] = son[getFail(fail[cur])][c]; 30 son[cur][c] = 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 bool flag = false; 40 for(int i = 0; str[i]; ++i){ 41 pt.extend(str[i] - ‘a‘); 42 if(flag) putchar(‘ ‘); 43 printf("%d",pt.tot - 2); 44 flag = true; 45 } 46 putchar(‘\n‘); 47 } 48 return 0; 49 }
Ural 1960 Palindromes and Super Abilities
标签:
原文地址:http://www.cnblogs.com/crackpotisback/p/4913902.html