标签:scan other sea cti ecif tput nal next not
地址:http://acm.hdu.edu.cn/showproblem.php?pid=4763
题目:
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3491 Accepted Submission(s): 1623
1 #include <bits/stdc++.h>
2 #define PB push_back
3 #define MP make_pair
4 using namespace std;
5 typedef long long LL;
6 typedef pair<int,int> PII;
7 #define PI acos((double)-1)
8 #define E exp(double(1))
9 const int K=1e6+9;
10 int nt[K];
11 char a[K];
12
13 //参数为模板串和next数组
14 //字符串均从下标0开始
15 void kmp_next(char *T,int *nt)
16 {
17 nt[0]=0;
18 for(int i=1,j=0,m=strlen(T);i<m;i++)
19 {
20 while(j&&T[i]!=T[j])j=nt[j-1];
21 if(T[i]==T[j])j++;
22 nt[i]=j;
23 }
24 }
25
26 int main(void)
27 {
28 int t;cin>>t;
29 while(t--)
30 {
31 scanf("%s",a);
32 kmp_next(a,nt);
33 int len=strlen(a);
34 int ans=nt[len-1],ff=1;
35 while(ans)
36 {
37 for(int i=ans;i<=len-ans&&ff;i++)
38 if(nt[i]==ans)
39 ff=0;
40 if(!ff) break;
41 ans=nt[ans-1];
42 }
43 printf("%d\n",ans);
44 }
45
46 return 0;
47 }
标签:scan other sea cti ecif tput nal next not
原文地址:http://www.cnblogs.com/weeping/p/6528066.html