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 ...
随机推荐
- Java 中String常用方法
java中String的常用方法 1.length() 字符串的长度 例:char chars[]={'a','b'.'c'}; String s=new String(chars); int len ...
- 【转】 C++ vector用法
在c++中,vector是一个十分有用的容器,下面对这个容器做一下总结. 1 基本操作 (1)头文件#include<vector>. (2)创建vector对象,vector<in ...
- 关于http响应内容压缩的一点小积累。
1.在tomcat的server.xml配置文件中,添加上背景颜色为绿色的配置,服务器就会自动压缩 <Connector port="80" maxHttpHeaderSiz ...
- HTML5自学笔记[ 15 ]canvas绘图基础6
关于线条的一些属性: lineCap,这个属性表示的是线条两端的样式,值有butt(默认)/round/square. lineJoin,这个属性表示线条相交的方式,值有miter(默认)/bevel ...
- Objective-C(一简介)
Objective-C简介 通常写作ObjC和较少用的Objective C或Obj-C,是扩充C的面向对象编程语言.它主要使用于Mac OS X和GNUstep这两个使用OpenStep标准的系统, ...
- GBK编码相关
如上图.的GBK编码是A3AE, 那么·对应的无符号整数值应该是A*16**3+E*16**2+A+3, 无符号整数值所在地址的第一个自己是A3,第二个字节是AE
- linux 查看僵尸进程
top -b -i -n 1 查看僵死进程命令 ps -A -o stat,ppid,pid,cmd | grep -e '^[Zz]' 查看apache 当前进程数 ps -ef | grep ht ...
- php solr 查询
$options = array( 'hostname' => 'localhost', 'port' => 8080, 'path' => 'solr/test'); $clien ...
- 第二章 XHTML基础
1.一个网页,也就是一个XHTML文档,是由元素组成.元素定义了文本和图形在XHTML文档中的结构.XHTML文档的扩展名通常是.html或者htm. 2.XHTML元素使用XHTML标记定义,每个标 ...
- 使用MediaRecorder录制音频
手机一般都提供了麦克风硬件,而Android系统就可以利用该硬件来录制音频了. 为了在Android应用中录制音频,Android提供了MediaRecorder类,使用MediaRecorder录制 ...