​http://www.elijahqi.win/archives/402​​​
Theme Section
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3773 Accepted Submission(s): 1763

Problem Description
It’s time for music! A lot of popular musicians are invited to join us in the music festival. Each of them will play one of their representative songs. To make the programs more interesting and challenging, the hosts are going to add some constraints to the rhythm of the songs, i.e., each song is required to have a ‘theme section’. The theme section shall be played at the beginning, the middle, and the end of each song. More specifically, given a theme section E, the song will be in the format of ‘EAEBE’, where section A and section B could have arbitrary number of notes. Note that there are 26 types of notes, denoted by lower case letters ‘a’ - ‘z’.

To get well prepared for the festival, the hosts want to know the maximum possible length of the theme section of each song. Can you help us?

Input
The integer N in the first line denotes the total number of songs in the festival. Each of the following N lines consists of one string, indicating the notes of the i-th (1 <= i <= N) song. The length of the string will not exceed 10^6.

Output
There will be N lines in the output, where the i-th line denotes the maximum possible length of the theme section of the i-th song.

Sample Input
5
xy
abc
aaa
aaaaba
aaxoaaaaa

Sample Output
0
0
1
1
2

Source
2013 ACM/ICPC Asia Regional Changchun Online

Recommend
liuyiding | We have carefully selected several similar problems for you: 6055 6054 6053 6052 6051

先求出我们的的next数组

然后暴力枚举中间部分进行判断,枚举的时候注意好利用next数组

#include<cstdio>
#include<cstring>
#define N 1100000
int next[N],n1;
inline bool judge(char s[],char w[],int len,int gl){
int tmp=0;
for (int i=1;i<=len;++i){
while (tmp&&w[tmp+1]!=s[i]) tmp=next[tmp];
if (w[tmp+1]==s[i])++tmp;
if (tmp==gl) return true;
}
return false;
}

char a[N];
int main(){
freopen("hdu4763.in","r",stdin);
//freopen("hdu4763.out","w",stdout);
scanf("%d",&n1);
while (n1--){
scanf("%s",a);
int n=strlen(a);
int i=0,j=-1;next[0]=-1;
while (i<n){
if (j==-1||a[i]==a[j]){
++i;++j;next[i]=j;
}else j=next[j];
}
int k=next[n];
for (int i=n;i>=1;--i) a[i]=a[i-1];
while (k){
if (judge(a+k,a,n-2*k,k)) break;
k=next[k];
}
printf("%d\n",k);
}
return 0;
}