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 ...
随机推荐
- Supervisor 守护 dotnetcore 程序
版权声明:本文由屈政斌原创文章,转载请注明出处: 文章原文链接:https://www.qcloud.com/community/article/240 来源:腾云阁 https://www.qclo ...
- 小米域名过渡JS
<script>jQuery(function($) { (function() { var $bnBar = $(''), $topBn = $('#J_topBn'), $topBnB ...
- IntelliJ IDEA 中properties中文显示问题
- C语言知识整理(1):简介
由于项目要求,需要学习iOS移动端开发.iOS开发的核心语言是Objective-C,Objective-C是在C语言的基础加了一层面向对象的语法.为了能够更好地掌握Objective-C,故先学习C ...
- mvc+ef中比较数据
例如:根据Para表中的type和paraid 字段进行比较 public class TypeComparer : IEqualityComparer<Para> { bool IEqu ...
- 关于PATH_INFO SCRIPT_NAME SCRIPT_FILENAME REDIRECT_URL 详解
参考:http://www.nginx.cn/426.html http://www.cnblogs.com/xiaochaohuashengmi/archive/2011/09/13/217507 ...
- mysql 无法远程访问(授权也没办法,确切的说是因为只绑定了127IP)
默认状况下,出于安全考虑,mysql数据库屏蔽了远程访问功能. 然而在许多状况下,你需要在家或者从web程序去访问远端数据库服务器,这就相当麻烦了. 第一步: 激活网络设置你需要编辑mysql配置文件 ...
- C#基础学习文章导航
第一部分:入个门 C#入门篇-1:HelloWorld的类 C#入门篇-2:什么是变量 C#入门篇-3:数据类型及转换 C#入门篇-4:使用运算符 第二部分:流程控制语句 C#入门篇5-1:流程控制语 ...
- VSS Plugin配置FAQ(翻译)[转]
前言(译者) 就个人的成长历程来说,刚参加工作用的是 CVS ,前前后后有接近三年的使用体验,从今年开始使用 SVN .总的来说我更喜欢 SVN ,用起来的确很方便,例如在本地源代码文件中加一个空格然 ...
- redhat enterprixe 5.0 NFS服务配置与管理
一.了解NFS Samba 是主要用于实现Linux和Windows操作系统之间文件共享的协议,而NFS则是实现UNIX和Linux操作系统之间文件共享的协议. NFS可以把网络上远程的文件挂载到本机 ...