ural 1960 Palindromes and Super Abilities 题解

题意:给一个串s,按顺序一个个加入到序列里面。输出每次加入之后序列中的本质不同的回文串个数。

回文自动机模板题- -extend函数里面,如果那个if进去了,就代表多了一个本质不同的回文串。

 1 #include<cstdio>
 2 #include<cstring>
 3 const int MAXN=100000+5;
 4 const int SIGMA_SIZE=26;
 5 struct Node{
 6     int num,len;
 7     Node* go[SIGMA_SIZE],*fail;
 8     Node():fail(0) {num=len=0;memset(go,0,sizeof(go));}
 9 };
10 Node mem[MAXN],*cur=mem;
11 Node* root0,*root1;
12 Node* last;
13 void init()
14 {
15     cur=mem;
16     root0=cur++;
17     root1=cur++;
18     root0->fail=root1;
19     root1->len=-1;
20     last=root1;
21 }
22 inline Node* newNode(int len)
23 {
24     cur->len=len;
25     return cur++;
26 }
27 char s[MAXN];
28 int p=0;
29 inline Node* getFail(Node* t)
30 {
31     while(s[p- t->len -1]!=s[p]) t=t->fail;
32     return t;
33 }
34 int ans=0;
35 inline void extend(int w)
36 {
37     ++p;
38     Node* t=getFail(last);
39     if(!t->go[w])
40     {
41         Node* nt=newNode(t->len+2);
42         t->go[w]=nt;
43         if(t==root1) nt->fail=root0;
44         else nt->fail=getFail(t->fail)->go[w];
45         nt->num=nt->fail->num+1;
46         ++ans;
47     }
48     last=t->go[w];
49     printf("%d",ans);
50 }
51 int main()
52 {
53     init();
54     scanf("%s",s+1);
55     int n=strlen(s+1);
56     s[0]='$';
57     for(int i=1;i<=n;++i)
58     {
59         extend(s[i]-'a');
60         if(i!=n) putchar(32);
61         else putchar(10);
62     }
63     return 0;
64 }
View Code

 

posted @ 2015-04-12 00:19  lowsfish  阅读(131)  评论(0编辑  收藏  举报