现在的位置: 首页 > 综合 > 正文

【KMP】【poj 2752】Seek the Name, Seek the Fame

2017年04月25日 ⁄ 综合 ⁄ 共 1329字 ⁄ 字号 评论关闭

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

直接画图

#define _TEST _TEST
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <cmath>
#include <algorithm>
using namespace std;
/************************************************
Code By willinglive    Blog:http://willinglive.cf
************************************************/
#define rep(i,l,r) for(int i=l,___t=(r);i<=___t;i++)
#define per(i,r,l) for(int i=r,___t=(l);i>=___t;i--)
#define MS(arr,x) memset(arr,x,sizeof(arr))
#define LL long long
#define INE(i,u,e) for(int i=head[u];~i;i=e[i].next)
inline const int read()
{int r=0,k=1;char c=getchar();for(;c<'0'||c>'9';c=getchar())if(c=='-')k=-1;
for(;c>='0'&&c<='9';c=getchar())r=r*10+c-'0';return k*r;}
/////////////////////////////////////////////////
char s[400010];
int len,next[400010];
int ans[400010],t;
/////////////////////////////////////////////////
void get_next()
{
	int i=0,j=-1;
	next[0]=-1;
	while(i<len)
	{
		if(j==-1||s[i]==s[j]) next[++i]=++j;
		else j=next[j];
	}
}
/////////////////////////////////////////////////
void input()
{
	t=0;
    len=strlen(s);
}
void solve()
{
    get_next();
    for(int i=len;i>0;i=next[i])
    {
    	ans[++t]=next[i];
    }
    per(i,t-1,1) printf("%d ",ans[i]);
    printf("%d\n",len);
}
/////////////////////////////////////////////////
int main()
{
    #ifndef _TEST
    freopen("std.in","r",stdin); freopen("std.out","w",stdout);
    #endif
    while(~scanf("%s",s))
    input(),solve();
    return 0;
}

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

抱歉!评论已关闭.