poj2752Seek the Name, Seek the Fame

Seek the Name, Seek the Fame

Time Limit : 4000/2000ms (Java/Other)   Memory Limit : 131072/65536K (Java/Other)
Total Submission(s) : 11   Accepted Submission(s) : 6
Problem Description
The little cat is so famous, that many couples tramp over hill and dale to Byteland, and asked the little cat to give names to their newly-born babies. They seek the name, and at the same time seek the fame. In order to escape from such boring job, the innovative little cat works out an easy but fantastic algorithm: 

Step1. Connect the father's name and the mother's name, to a new string S. 
Step2. Find a proper prefix-suffix string of S (which is not only the prefix, but also the suffix of S). 

Example: Father='ala', Mother='la', we have S = 'ala'+'la' = 'alala'. Potential prefix-suffix strings of S are {'a', 'ala', 'alala'}. Given the string S, could you help the little cat to write a program to calculate the length of possible prefix-suffix strings of S? (He might thank you by giving your baby a name:) 
 

Input
The input contains a number of test cases. Each test case occupies a single line that contains the string S described above. 

Restrictions: Only lowercase letters may appear in the input. 1 <= Length of S <= 400000. 
 

Output
For each test case, output a single line with integer numbers in increasing order, denoting the possible length of the new baby's name.
 

Sample Input
  
  
ababcababababcabab aaaaa
 

Sample Output
  
  
2 4 9 18 1 2 3 4 5
 
比较字符串,题意就是输出n,满足前n个字符和后n个字符相等。kmp问题,
下面给出描述: (i>1)[下标从0开始] 
next[i]的意义就是:前面长度为i的字串的【前缀和后缀的最大匹配长度】 

那么这题怎么利用这个性质呢? 

详细分析一下:【就用上面的第一个例子说明吧】 
求出next值:[非修正]
下标:      0  1  2  3  4  5  6  7  8  9  10 11 12 13 14 15 16 17
 
串:          a  b  a  b  c  a  b  a  b  a  b   a   b   c   a   b   a   b
 
next值:  -1  0  0  1  2  0  1  2  3  4  3   4   3   4   5   6   7   8   9

len2 = 18    next[len2] = 9 
说明对于前面长度为18的字符串,【长度为9的前缀】和【长度为9的后缀】是匹配的, 即上图的蓝色跟红色匹配 
也就是整个串的 最大 前后缀匹配长度就是9了 
所以接下来根本 不需要考虑长度大于9的情况 啦 

好了!既然现在只需考虑长度小于9的前后缀匹配情况,那么 
[问题就转化成蓝色串的前缀红色串的后缀的匹配问题了!!! 
又因为 蓝串 == 红串  
所以问题又转化成 
找蓝串自己的前缀跟自己的后缀的最大匹配了!!!  
那么我们现在就要找next[9]的值了【next[9]的含义:蓝串的最大前后缀匹配】 

回忆第一步:我们找的是next[len2]=9【len2=18】 
怎么使得第二部目标变成9【求next[9]】呢? 
其实next[len2]=9同时可以表示为:最大匹配前后缀的【 前缀长度 】 
那么next[9]的意义就是: 
【主串】的最大匹配前后缀的【 前缀 】的【 最大匹配前后缀 】了!! 
也就是上面 蓝串 的前后缀最大匹配长度了!! 

那么算法描述就是: 
第一步:求next[len2], 即next[18] = 9; 
第二步:把9代进来,即求next[9] = 4; 
第三步:把4代进来,即求next[4] = 2; 
第四步:next[2] = 0; 也就是下标2之前的串已经没有前后缀可以匹配了 
所以答案就是: 2 4 9 18 
附ac代码
#include<stdio.h>
#include<string.h>
#define N 400005
char c[N];
int p[N],len,ans[N];
void kmp()//kmp算法,好难好难的,也不是很清楚 
{
	int i=0;
	int j=-1;
	p[i]=j;//p[i]即为next[i],根据个人爱好酌量添加. 
	while(i<len)
	{
		if(j==-1||c[i]==c[j])
		{
			i++;
			j++;
			p[i]=j;
		}
		else
		j=p[j];
	}
}
int main()
{
	while(scanf("%s",c)!=EOF)
	{
		len=strlen(c);
		kmp();
		int i;
		int j=len;
		int cnt=0;
		while(j!=-1)
		{
			ans[cnt++]=j;
			j=p[j];
		}
		for(i=cnt-1;i>0;i--)
		printf("%d ",ans[i-1]);
		printf("\n");
	}
return 0;
}


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值