POJ - 2752 Seek the Name, Seek the Fame

https://vjudge.net/problem/POJ-2752

POJ - 2752 Seek the Name, Seek the Fame

题目

在这里插入图片描述

分析

在这里插入图片描述

AC代码

#include<cstdio>
#include<iostream>
#include<cstring>
using namespace std;
const int N=4e5+10;
int ne[N];
string s;
int main()
{
	while(cin>>s)
	{
		int n=s.size();
		ne[0]=-1;
		for(int i=0,j=-1;i<=n;)
		{
			if(j==-1 || s[i]==s[j])
			{
				i++,j++;
				ne[i]=j;
			}
			else
				j=ne[j];
		}
		
		int h[N];
		int u=0;
		int r=ne[n];
		while(r)
		{
			h[u]=r;
			u++;
			r=ne[r];
		}
		for(int i=u-1;i>=0;i--)
			printf("%d ",h[i]);
		printf("%d\n",n);
	}
	return 0;
}
posted @ 2021-07-19 15:32  斯文~  阅读(10)  评论(0)    收藏  举报

你好!