POJ 2752 - Seek the Name, Seek the Fame (KMP)
题意:给一个字符串s,问s的某个前缀与后缀相同的情况时,长度是多少。
此题使用KMP的next数组解决。
next数组中,j=next[i],next[i]表示S[0...i-1]的某个后缀(字符串S[i-j,i-1])与字符串S[0...j-1]完全相同,此时的j即该段字符串的长度。
字符串s本身是最长的串,next[L]对应的S[0...next[L]]是次长的符合条件的串,其他更短的串怎么求呢。
由于字符串S[L-j...L-1]等于S[0...j-1],这样在S[0...j-1]时继续利用next[j](令k=next[j])找到S[0...k-1]与S[0...j-1]的某个后缀(字符串S[j-k,j-1])相同,这段字符串等于S[L-k,L-1](即原字符串的某个后缀),此时的k即该段字符串的长度。如此下去,直到next[]数值为0,可以找到所有的符合条件的字符串。
这个过程可以使用递归倒序输出。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#define MAXN 400005
using namespace std;
int next[MAXN];
char str[MAXN];
void getFail()
{
; str[i]; ++i)
{
int j=next[i];
while(j&&str[i]!=str[j]) j=next[j];
next[i+]=(str[i]==str[j])?j+:;
}
}
void show(int p)
{
if(!next[p]) return ;
show(next[p]);
printf("%d ",next[p]);
}
int main()
{
while(gets(str))
{
getFail();
int L=strlen(str);
show(L);
printf("%d\n",L);
}
;
}
POJ 2752 - Seek the Name, Seek the Fame (KMP)的更多相关文章
- poj-------------(2752)Seek the Name, Seek the Fame(kmp)
Seek the Name, Seek the Fame Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 11831 Ac ...
- Seek the Name, Seek the Fame(Kmp)
Seek the Name, Seek the Fame Time Limit : 4000/2000ms (Java/Other) Memory Limit : 131072/65536K (J ...
- (KMP)Seek the Name, Seek the Fame -- poj --2752
http://poj.org/problem?id=2752 Seek the Name, Seek the Fame Time Limit: 2000MS Memory Limit: 65536 ...
- poj 2752 Seek the Name, Seek the Fame (KMP纯模版)
Seek the Name, Seek the Fame Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 13840 Ac ...
- POJ 2751:Seek the Name, Seek the Fame(Hash)
Seek the Name, Seek the Fame Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 24077 Ac ...
- POJ 2184 Cow Exhibition【01背包+负数(经典)】
POJ-2184 [题意]: 有n头牛,每头牛有自己的聪明值和幽默值,选出几头牛使得选出牛的聪明值总和大于0.幽默值总和大于0,求聪明值和幽默值总和相加最大为多少. [分析]:变种的01背包,可以把幽 ...
- 题解报告:poj 2752 Seek the Name, Seek the Fame(kmp前缀表prefix_table的运用)
Description The little cat is so famous, that many couples tramp over hill and dale to Byteland, and ...
- POJ 2752 Seek the Name, Seek the Fame(KMP求公共前后缀)
题目链接:http://poj.org/problem?id=2752 题目大意:给你一串字符串s找到所有的公共前后缀,即既是前缀又是后缀的子串. 解题思路: 如图所示 假设字符串pi与jq为符合条件 ...
- POJ 2752 Seek the Name, Seek the Fame (KMP)
传送门 http://poj.org/problem?id=2752 题目大意:求既是前缀又是后缀的前缀的可能的长度.. 同样是KMP,和 HDU 2594 Simpsons' Hidden Tale ...
随机推荐
- FileReader本地预览图片
<body> <p><label>请选择一个图像文件:</label><input type="file" id=" ...
- 20145218《Java程序设计》第一周学习总结
20145218 <Java程序设计>第一周学习总结 教材学习内容总结 今天下午看了Java学习的视频,感觉很是新奇,之前觉得Java学起来是艰难枯燥的,但通过第一章的学习觉得如果自己可以 ...
- HTML5自学笔记[ 17 ]canvas绘图基础4
绘制图像: drawImage(oImg,x,y),oImg是一个Image对象,(x,y)为绘制起点,绘制的图像大小和源图大小一样. drawImage(oImg,x,y,w,h),后两个参数设置绘 ...
- hadoop分布式的环境搭建
版本: 使用hadoop1.1.2 JDK为java7 1.下载hadoop 2.配置hadoop文件 3测试 1.下载hadoop: 1.1 在https://archive.apache.o ...
- jquery iframe高度自适应
$(document).ready(function () { $("#test").load(function () { var thisheight = $(this).con ...
- MASM6.15汇编程序例子
/***************通过调用(INT 21H)表中的01h号功能号从键盘输入一个字符并回显到视频显示器上*****************/ DATAS SEGMENT ;此处输入数据段代 ...
- backbonejs中的模型篇(二)
一:模型标识符 每个模型都有一个用作唯一标识符的ID属性,以便在不同模型间进行区分.通过id属性我们可以直接访问模型对象当中用于标识符存放的属性,默认属性名为id,但也可以通过设置idAttribut ...
- sharepoint2010无法创建网站集
出现以上错误,查看IIS中有关Sharepoint的网站中的“身份验证”中ASP.Net模拟是否为禁用,如果为禁用,请启用即可.
- event.srcElement在火狐(FireFox)下的兼容问题。搜索框获得焦点时默认文字变化
前言: 项目中用到了一个功能,搜索框里有默认的文字,当搜索框获得焦点时里面的默认文字消失,如果失去焦点时搜索框内容为空则让里面的内容回复默认!,. 实现: 很轻松的在网上找到了类似代码 $(" ...
- 绑定本地Service并与之通信-----之一
import android.app.Service;import android.content.Intent;import android.os.Binder;import android.os. ...