【KMP】POJ 2752 Seek the Name Seek the Fame

前言

这名字好丑。。。
一波操作。。。
POJ 2752 Seek the Name,Seek the Fame P O J   2752   S e e k   t h e   N a m e , S e e k   t h e   F a m e

链接

http://poj.org/problem?id=2752

大意

有很多组数据,每组数据给定一个字符串,从小到大输出既是前缀又是后缀的子串的长度

思路

KMP K M P 算法的 next n e x t 操作

代码

#include<cstdio>
#include<cstring>
using namespace std;
int kmp[500000],ans[500001],n,m;
char s[500000];
int main()
{
    while(scanf("%s",s)!=EOF&&s[0])//输入
    {
        n=strlen(s);//取下长度
        for(int i=1;i<n;i++) kmp[i]=0;//清0
        kmp[0]=-1;//初始化
        for(int i=0,j=-1;s[i];)
        if(j==-1||s[i]==s[j])
        {
            i++;j++;
            kmp[i]=j;
        }
        else j=kmp[j];//next
        m=0;
        for(int i=n;i>0;i=kmp[i])
         if(kmp[i]) ans[++m]=kmp[i];//把所有答案存到ans数组里
        for(int i=m;i>0;i--)
         printf("%d ",ans[i]);//因为我们是倒过来存的,所以要倒过来输出
        printf("%d\n",n);//自己本身也是既是前缀又是后缀的子串
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值