hdu 4763 Theme Section

#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
const int maxn = 1e6 + 10;
char str[maxn];
int len,nxt[maxn];

void GetNext()
{
    nxt[0] = -1;
    int i = 0,j = -1;
    while(i < len)
    {
        if(j == -1 || str[i] == str[j])
            nxt[++i] = ++j;
        else j = nxt[j];
    }
}

int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%s",str);
        len = strlen(str);
        GetNext();
        int ans = 0;
        for(int i = 0; i < len ; i++)
            ans = max(ans,nxt[i]);
        if(ans == 0)
            printf("0\n");
        else
        {
            ans = min(ans,nxt[len]);
            if(ans == 0)
                printf("0\n");
            else printf("%d\n",ans);
        }
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/feynmanz/article/details/80429830