其他分享
首页 > 其他分享> > POJ 2752 Seek the Name, Seek the Fame KMP

POJ 2752 Seek the Name, Seek the Fame KMP

作者:互联网

#include<iostream>
#include<string>
using namespace std;

string s;
int nex[400010];

void getNext()
{
    int i = 0, j = -1;
    int len_s = s.length();
    nex[0] = -1;
    while(i < len_s)
    {
        if(j == -1 || s[i] == s[j])
        {
            j++;           
            i++; 
            nex[i] = j;
        }
        else j = nex[j];
    }
}

void solve(int len)
{
    if(nex[len] > 0)
    {
        solve(nex[len]);
        printf("%d ", nex[len]);
    }
}

int main()
{
    while(getline(cin, s))
    {
        getNext();
        int len = s.length();
        solve(len);
        printf("%d ", len);
        printf("\n");
    }
    //getchar();
    return 0;
}

标签:getNext,Name,int,len,2752,nex,printf,solve,Seek
来源: https://www.cnblogs.com/znk97/p/14029025.html