seek

topic:

As the saying goes, "not as lucky good name," little h ready to give his pet dog from the new name, so he put some English full name written down, written in a long line of string, a small h that name if is a good name, then the name in this string is both a prefix and a suffix, that is, the name began to be matched from the front, began to be matched from the back, such as abc in abcddabc in both a prefix, also the suffix, but ab is not but as long as 4 * 10 ^ 5 characters make small h almost fainted, to give his dog a good name, a small h to you for help, and he asked all of the length of the good name you want to output to both .

Input:

Line, the character string to be processed (all lowercase)

Output:

Several digital line, from small to large, this means the good name of length

Example:


abcddabc

 

3 8

 

KMP

KMP is determined next array, in seeking at next [strlen (s) -1] scheme

 

#include<cstdio>
#include<cstring>
using namespace std;
char b[500010];
int n,m,a[500010],f[500010],cnt=0;

int main()
{
    scanf("%s",&b);
    n=strlen(b);
    f[0]=-1;
    int j=-1;
    for (int i=1;i<=n;i++)
    {
        j=f[i-1];
        while (b[j+1]!=b[i]&&j!=-1) j=f[j];
        if (b[j+1]==b[i]) f[i]=j+1;
        else f[i]=-1;
    }
    j=f[n-1];
    while (j>=0)
    {
        if (b[j]==b[n-1]) a[++cnt]=j+1;
        j=f[j];
    }
    for (int i=cnt;i>=1;i--)
        printf("%d ",a[i]);
    printf("%d\n",n);
}

 

 

 

Guess you like

Origin www.cnblogs.com/nibabadeboke/p/11346000.html