bzoj 3670: [Noi2014]动物园

什么什么KMP,,,看题解说是裸题的样子。。

 1 #include<bits/stdc++.h>
 2 #define N 100005
 3 #define LL long long
 4 #define inf 0x3f3f3f3f
 5 using namespace std;
 6 inline int ra()
 7 {
 8     int x=0,f=1; char ch=getchar();
 9     while (ch<'0' || ch>'9') {if (ch=='-') f=-1; ch=getchar();}
10     while (ch>='0' && ch<='9') {x=x*10+ch-'0'; ch=getchar();}
11     return x*f;
12 }
13 const int mod=1000000007;
14 LL ans;
15 int T,fail[1000005],num[1000005];
16 char ch[1000005];
17 void kmp()
18 {
19     int n=strlen(ch+1);
20     int fix=0; num[1]=1;
21     for (int i=2; i<=n; i++)
22     {
23         while (ch[i]!=ch[fix+1] && fix) fix=fail[fix];
24         if (ch[fix+1]==ch[i]) fix++;
25         fail[i]=fix; num[i]=num[fix]+1;
26     }
27     fix=0;
28     for (int i=2; i<=n; i++)
29     {
30         while (ch[fix+1]!=ch[i] && fix) fix=fail[fix];
31         if (ch[fix+1]==ch[i]) fix++;
32         while ((fix<<1)>i && fail) fix=fail[fix];
33         ans=ans*(num[fix]+1)%mod;
34     //    printf("%d  %d\n",fix,num[fix]+1);
35     }
36     //for (int i=1; i<=n; i++)
37     //    printf("%d\n",num[i]);
38 }
39 int main()
40 {
41     T=ra();
42     while (T--)
43     {
44         scanf("%s",ch+1);
45         ans=1;
46         kmp();
47         printf("%lld\n",ans);
48     }
49 }

 

posted @ 2017-02-24 20:34  ws_ccd  阅读(169)  评论(0编辑  收藏  举报