hdu 4763 Theme Section(exkmp)

来源:互联网 发布:怎么登录淘宝图片空间 编辑:程序博客网 时间:2024/06/03 11:16

Theme Section

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3990 Accepted Submission(s): 1880

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

#include <bits/stdc++.h>using namespace std;typedef long long LL;const int N= 1e6+10;char s1[N], s2[N];int extend[N], nex[N];void getnext(char a[],int len){    int k=0, i=1;    nex[0]=len;    while(k+1<len&&a[k]==a[k+1])++k;    nex[1]=k;    k=1;    while(++i<len)    {        int p=k+nex[k]-1;        nex[i]=min(nex[i-k],max(p-i+1,0));        while(i+nex[i]<len&&a[nex[i]]==a[i+nex[i]]) ++nex[i];        if(i+nex[i]>k+nex[k]) k=i;    }    return ;}void exkmp(char a[],char b[],int len,int len2){    getnext(a,len);    int k=0, i=0;    while(k<len2&&a[k]==b[k]) ++k;    extend[0]=k;    k=0;    while(++i<len2)    {        int p=k+extend[k]-1;        extend[i]=min(nex[i-k],max(p-i+1,0));        while(i+extend[i]<len2&&a[extend[i]]==b[i+extend[i]]) ++extend[i];        if(i+extend[i]>k+extend[k]) k=i;    }    return ;}char v[1000];int a[N];int main(){    int t, ncase=1;    scanf("%d", &t);    while(t--)    {        scanf("%s",s1);        int len=strlen(s1);        if(len<3)        {            printf("0\n");            continue;        }        getnext(s1,len);        int k=1;        a[0]=0;        for(int i=len-len/3;i<len;i++)        {            if(nex[i]==len-i) a[k++]=nex[i];        }        a[k]=N+10;        if(k==1)        {            printf("0\n");            continue;        }        k++;        sort(a,a+k);        int m=0, ans=0;        for(int i=1;i<len-1;i++)        {            m=min(i,nex[i]);            if(m==0) continue;            int pos=lower_bound(a,a+k,m)-a;            for(int j=pos;j>=0;j--)            {                if(a[j]<=m&&i+a[j]-1<len-a[j])                {                    ans=max(ans,a[j]);                    break;                }            }        }        printf("%d\n",ans);    }    return 0;}