poj 2752 Seek the Name, Seek the Fame

题意

在字符串S中寻找出所有字串,它即是这个字符串的前缀,也是这个字符串的后缀,输出它们的长度。

知识储备与解题思路

KMP中的next数组的含义,对于以i-1为结尾的子串,T[0, next[i]-1] 与 T[i-next[i], i-1]是相等的,且这两个相等的字符串是这个字串的前后缀,以样例为例分析以下。

字符串 ababcababababcabab

next[i]  001201234343456789

next[n]是9,那么前后9个必相同,next[next[n]]代表的字符串一定是下一个满足条件的子串。自行好好理解一下就行,写不明白 = =。

代码

#include <stdio.h>      
#include <string.h>  
#include <iostream>  
#include <algorithm>  
      
using namespace std;  
      
const int MAX = 400010;

char s[MAX];
int n, f[MAX],ans[MAX];

void failure_function(){
	f[0] = f[1] = 0;
	for(int i=1; i<n; i++){
		int j = f[i];
		while(j && s[i] != s[j]) j = f[j];
		f[i+1] = (s[i] == s[j]) ? j + 1 : 0;
	}	
}

int main(){
	while(scanf("%s",s) == 1){
		n = strlen(s);
		failure_function();
		int t = 0, i = n;
		while(f[i] > 0){
			ans[t++] = f[i];
			i = f[i];
		}
		for(int i = t - 1; i >= 0; i--)
			printf("%d ",ans[i]);
		printf("%d\n",n);
		
		for(int i=1; i<=n; i++)
			cout << f[i] << " ";
		cout << endl;
	}
	return 0;
}



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值