Theme Section

Meaning of the questions:

That is, every song is EAEBE this type, where A and B can not, is to let you find out the maximum length E

 

answer:

The first idea:

Can find someone out of the head and tail of the same maximum degree, beginning from the minimum 1, the maximum is less than len / 3

Then use kmp algorithm to find out there is not the same as the middle part of it

But this kind of method is too cumbersome, as well as cutting string, see below

 

The second:

We can start by seeking out his next array kmp

Such prefixes and suffixes E has been met, only to find the next [i] is also equal to the intermediate next [len]

 

 

Code:

 1 #include<stdio.h>
 2 #include<string.h>
 3 #include<stdlib.h>
 4 #include<algorithm>
 5 using namespace std;
 6 const int maxn=1e6+10;
 7 char ptr[maxn];
 8 int nnn[maxn];
 9 void get_next(int plen)
10 {
11     int i=0,j;
12     j=nnn[0]=-1;
13     while(i<plen)
14     {
15         while(j!=-1 && ptr[i]!=ptr[j]) j=nnn[j];
16         nnn[++i]=++j;
17     }
18 }
19 int kmp(int plen)
20 {
21     get_next(plen);
22     for(int k=nnn[plen];k;k=nnn[k])
23     {
24         //printf("%d %d %d\n",k,2*k-1,plen-k);
25         for(int i=2*k-1;i<plen-k;++i)
26         {
27             if(nnn[i]+1==k)
28                 return k;
29         }
30     }
31     return 0;
32 }
33 int main()
34 {
35     int t;
36     scanf("%d",&t);
37     while(t--)
38     {
39         scanf("%s",ptr);
40         int len=strlen(ptr);
41         int q=kmp(len);
42         printf("%d\n",q);
43     }
44     return 0;
45 }
View Code

 

Guess you like

Origin www.cnblogs.com/kongbursi-2292702937/p/11270974.html