POJ 2752 Seek the Name, Seek the Fame (KMP next 数组 变形)
题意:给一个字符串S,判断在什么下标的时候,前缀和后缀相等,输出前缀和后缀相等的点。
分析:next数组的一种很巧妙的用法
next数组表示的意义是当前下标前面k字符和开头的前面k个字符相等
所以就会有xy=ab(用xy表示x - y的这一段),则next[b]=y,那么下次就从y这个位置开始匹配
如果xk=wy,因为xy=ab,故wy=lb,所以xk=lb,就得到了前缀和后缀相等。
详细见:http://www.mamicode.com/info-detail-977140.html
代码:
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<queue>
#include<algorithm>
#include<cmath>
#include<map>
using namespace std;
#define INF 0x7fffffff
char s[400010];
int next[400010];
void get_next(){
int i,j,len = strlen(s);
next[0] = 0;
next[1] = 0;
for(i=1;i<len;i++){
j = next[i] ;
while(j && s[i] != s[j]) j = next[j] ;
if(s[i] == s[j]) next[i+1] = j+1 ;
else next[i+1] = 0 ;
}
}
int main(){
int i,j,num[400000],cnt;
while(scanf("%s",s) == 1){
cnt = 0;
get_next();
int len =strlen(s);
j = len -1 ;
while(j){
i = j;
j = next[j];
while(j && s[i] != s[j]) j = next[j] ;
if(s[i] == s[j]) num[cnt++] = j+1 ;
else break ;
}
sort(num,num+cnt);
for(i=0;i<cnt;i++)
printf("%d ",num[i]);
printf("%d\n",len);
}
return 0;
}
POJ 2752 Seek the Name, Seek the Fame (KMP next 数组 变形)的更多相关文章
- POJ2752 Seek the Name, Seek the Fame —— KMP next数组
题目链接:https://vjudge.net/problem/POJ-2752 Seek the Name, Seek the Fame Time Limit: 2000MS Memory Li ...
- (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 ...
- Seek the Name, Seek the Fame POJ - 2752
Seek the Name, Seek the Fame POJ - 2752 http://972169909-qq-com.iteye.com/blog/1071548 (kmp的next的简单应 ...
- KMP POJ 2752 Seek the Name, Seek the Fame
题目传送门 /* 题意:求出一个串的前缀与后缀相同的字串的长度 KMP:nex[]就有这样的性质,倒过来输出就行了 */ /************************************** ...
- POJ 2752 Seek the Name, Seek the Fame [kmp]
Seek the Name, Seek the Fame Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 17898 Ac ...
- poj 2752 Seek the Name, Seek the Fame(KMP需转换下思想)
Seek the Name, Seek the Fame Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 10204 Ac ...
- poj 2752 Seek the Name, Seek the Fame【KMP算法分析记录】【求前后缀相同的子串的长度】
Seek the Name, Seek the Fame Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 14106 Ac ...
- POJ 2752 Seek the Name, Seek the Fame(next数组运用)
Seek the Name, Seek the Fame Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 24000 ...
- POJ 2752 Seek the Name,Seek the Fame(KMP,前缀与后缀相等)
Seek the Name,Seek the Fame 过了个年,缓了这么多天终于开始刷题了,好颓废~(-.-)~ 我发现在家真的很难去学习,因为你还要陪父母,干活,做家务等等 但是还是不能浪费时间啊 ...
随机推荐
- poj3667---Hotel 线段树区间合并,区间更新
题意:有N个房间,M次操作.有两种操作(1)"1 a",表示找到连续的长度为a的空房间,如果有多解,优先左边的,即表示入住.(2)"2 b len",把起点为b ...
- poj 2226 Muddy Fields(最小点覆盖+巧妙构图)
Description Rain has pummeled the cows' field, a rectangular grid of R rows and C columns (1 <= ...
- 关于win8开发的一点小总结
我今天做画面的时候,发现了一点小问题. 我在xmal文件里面加了一个CheckBox控件,设置IsChecked属性为True,并添加了Checked事件.Checked事件里面有对另外一个TextB ...
- hadoop2对比hadoop1
hadoop2对比hadoop1 1.体系结构 HDFS+MapReduce,共同点都是分布式的,主从关系结构. HDFS=一个NameNode+多个DataNode, NameNode含有我们用户存 ...
- NetAnalyzer笔记 之 四. C#版的抓包软件
[创建时间:2015-09-10 22:37:04] NetAnalyzer下载地址 不好意思啊,NetAnalyzer停更有点长了,今天继续填坑^&^ NetAnalyzer实现结构 在上一 ...
- Hibernate的查询 HQL查询 查询某几列
HQL 是Hibernate Query Language的简写,即 hibernate 查询语言:HQL采用面向对象的查询方式.HQL查询提供了更加丰富的和灵活的查询特性,因此Hibernate将H ...
- android 银行卡。。空格输入
class myWatcher implements TextWatcher { int beforeTextLength = 0; int onTextLength = 0; boolean isC ...
- [转]Spring Boot——2分钟构建spring web mvc REST风格HelloWorld
Spring Boot——2分钟构建spring web mvc REST风格HelloWorld http://projects.spring.io/spring-boot/ http://spri ...
- SELECT--UNION,UNION ALL,MINUS, INTERSECT,EXISTS
SELECT--UNION,UNION ALL,MINUS, INTERSECT返回两个查询结果的集合操作,两个查询结果集必须字段相同.UNION和UNION ALL并集操作,UNION并集后去掉重复 ...
- javascript将毫秒还原为可读时间格式
<script type="text/javascript"> //随便设置一个时间 var otime = new Date("2015-11-11 20: ...