hdu4763Theme Section

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4763

nex。

 1 /*
 2 #include<cstdio>
 3 #include<cstring>
 4 const int maxn=1e6+10;
 5 char s[maxn];
 6 int v[maxn];
 7 
 8 int main()
 9 {
10     int t;
11     scanf("%d",&t);
12     while(t--)
13     {
14         int cnt=0;
15         scanf("%s",s);
16         int n=0;
17         int len=strlen(s);
18         for(int i=0;i<len;i++)
19             if(s[i]==s[0]) v[n++]=i;
20         for(int i=0;i<n;i++)
21             for(int j=i+1;j<n;j++)
22         if(v[j]-v[i]>1) {
23             cnt++;
24             i=j;
25             break;
26         }
27         printf("%d
",cnt);
28     }
29 }
30 */
31 //ртио╤а╢МлБак-_-||
32 #include<cstdio>
33 #include<cstring>
34 const int maxn=1e6+10;
35 char s[maxn];
36 int nex[maxn];
37 int n;
38 int main()
39 {
40     int t;
41     scanf("%d",&t);
42     while(t--)
43     {
44         scanf("%s",s);
45         n=strlen(s);
46         int i=0,j=-1;
47         nex[0]=-1;
48         while(i<n)
49         {
50             if(j==-1||s[i]==s[j])
51                 nex[++i]=++j;
52             else j=nex[j];
53         }
54         if(nex[n]*2>n) printf("%d
",n/2);
55         else printf("%d
",nex[n]);
56     }
57 }
原文地址:https://www.cnblogs.com/yijiull/p/6614067.html