首页 > 其他 > 详细

POJ 2752 Seek the Name, Seek the Fame KMP

时间:2020-11-24 12:29:21      阅读:26      评论:0      收藏:0      [点我收藏+]
#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;
}

POJ 2752 Seek the Name, Seek the Fame KMP

原文:https://www.cnblogs.com/znk97/p/14029025.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!