bzoj3676 [Apio2014]回文串

回文自动机模板题

一篇比较详细的讲解:http://blog.csdn.net/u013368721/article/details/42100363

 1 #include<algorithm>
 2 #include<iostream>
 3 #include<cstdlib>
 4 #include<cstring>
 5 #include<cstdio>
 6 #include<string>
 7 #include<cmath>
 8 #include<ctime>
 9 #include<queue>
10 #include<stack>
11 #include<map>
12 #include<set>
13 #define rre(i,r,l) for(int i=(r);i>=(l);i--)
14 #define re(i,l,r) for(int i=(l);i<=(r);i++)
15 #define Clear(a,b) memset(a,b,sizeof(a))
16 #define inout(x) printf("%d",(x))
17 #define douin(x) scanf("%lf",&x)
18 #define strin(x) scanf("%s",(x))
19 #define LLin(x) scanf("%lld",&x)
20 #define op operator
21 #define CSC main
22 typedef unsigned long long ULL;
23 typedef const int cint;
24 typedef long long LL;
25 using namespace std;
26 void inin(int &ret)
27 {
28     ret=0;int f=0;char ch=getchar();
29     while(ch<'0'||ch>'9'){if(ch=='-')f=1;ch=getchar();}
30     while(ch>='0'&&ch<='9')ret*=10,ret+=ch-'0',ch=getchar();
31     ret=f?-ret:ret;
32 }
33 const int xxxx=600010;
34 LL ans;
35 namespace pam
36 {
37     int ch[xxxx][26],pre[xxxx],sum[xxxx],len[xxxx],ss[xxxx],num[xxxx];
38     int last,n,ed=-1;
39     int newnode(int x)
40     {
41         len[++ed]=x;
42         return ed;
43     }
44     void init()
45     {
46         newnode(0),newnode(-1);
47         last=n=0;ss[n]=-1,pre[0]=pre[1]=1;
48     }
49     int getfail(int x)
50     {
51         while(ss[n-len[x]-1]!=ss[n])x=pre[x];
52         return x;
53     }
54     void add(int c)
55     {
56         c-='a';
57         ss[++n]=c;
58         int temp=getfail(last);
59         if(!ch[temp][c])
60         {
61             int now=newnode(len[temp]+2);
62             int k=getfail(pre[temp]);
63             pre[now]=ch[k][c];
64             ch[temp][c]=now;
65             num[now]=num[pre[now]]+1;
66         }
67         last=ch[temp][c];
68         sum[last]++;
69     }
70     void add(char *s)
71     {
72         int limit=strlen(s);
73         re(i,0,limit-1)add(s[i]);
74     }
75     void count()
76     {
77         rre(i,ed,1)sum[pre[i]]+=sum[i];
78     }
79     void solve()
80     {
81         rre(i,ed,1)ans=max(ans,(LL)sum[i]*len[i]);
82         printf("%lld",ans);
83     }
84 }
85 char s[xxxx];
86 int main()
87 {
88     strin(s);
89     pam::init();
90     pam::add(s);
91     pam::count();
92     pam::solve();
93     return 0;
94 }

 

posted @ 2016-03-22 16:26  HugeGun  阅读(196)  评论(0编辑  收藏  举报