首页 > 其他 > 详细

POJ 2752 Seek the Name, Seek the Fame

时间:2016-02-05 19:22:30      阅读:250      评论:0      收藏:0      [点我收藏+]

对字符串建一个next表,然后逆推即可。

#include<iostream>
#include<cstdio>
#include<cstring>
#define maxn 400005
using namespace std;
char p[maxn];
int next[maxn],t[maxn],l,tot=0;
void make_next()
{
memset(next,0,sizeof(next));
next[1]=0;
int cnt=0;
for (int i=2;i<=l;i++)
{
while (cnt>0 && p[i-1]!=p[cnt])
cnt=next[cnt];
if (p[i-1]==p[cnt]) cnt++;
next[i]=cnt;
}
}
void find()
{
t[++tot]=l;
int cnt=next[l];
while (cnt>0)
{
t[++tot]=cnt;
cnt=next[cnt];
}
for (int i=tot;i>=1;i--)
{
printf("%d ",t[i]);
t[i]=0;
}
printf("\n");
tot=0;
}
int main()
{
while (scanf("%s",p)!=EOF)
{
l=strlen(p);
make_next();
find();
}
return 0;
}

POJ 2752 Seek the Name, Seek the Fame

原文:http://www.cnblogs.com/ziliuziliu/p/5183436.html

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