HDU4763——Theme Section

Problem

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


思路

题意是给定字符串,找出最长的子串长度,该子串需满足:在串的开头、中间和结尾分别出现一次,且不能交叉。
思路为KMP变形。先用字符串生成失配数组。设文本串长度为len。串从0开始编号。我们考虑匹配第len个位置。失配数组每跳一个位置。都能保证文本串的开头位置匹配。所以只需在剩下的中间部分找有没有匹配的子串。如果有说明满足条件。

代码

#include 
#include 
using namespace std;
const int M=1e6+5;
int f[M];
char str[M];
void getnext(char *p){
    int n=strlen(p);
    f[0]=f[1]=0;
    for(int i=1;i=3*j&&kmp(str+j,str,len-2*j,j)){res=j;break;}
            j=f[j];
        }
        printf("%d\n",res);
    }
    return 0;
}

你可能感兴趣的:(HDU4763——Theme Section)