POJ 2752 Seek the Name, Seek the Fame(KMP求公共前后缀)
题目链接:http://poj.org/problem?id=2752
题目大意:给你一串字符串s找到所有的公共前后缀,即既是前缀又是后缀的子串。
解题思路:
如图所示

假设字符串pi与jq为符合条件的一组公共前后缀,那么易得pi=jq,如下图所示

若在字符串pi内,pk1与k2i为公共前后缀,有因为pi=jq所以对应的k2i在字符串jq内有后缀k4q与其等价。所以pi内的公共前后缀等也是pq的公共前后缀,而i=next[q],显然是个递归。
所以,可以通过不断使pos=next[pos]进行递归求出所有的公共前后缀。这里其实利用了一些next[]数组的对称性质。
代码
#include<iostream>
#include<cstdio>
#include<cstring>
#include<map>
#include<string>
using namespace std;
const int N=1e6+; int m;
int nxt[N],ans[N];
char p[N]; void getnext(){
int i,j;
i=,j=nxt[]=-; while(i<m){
while(j!=-&&p[i]!=p[j])
j=nxt[j];
nxt[++i]=++j;
}
} int main(){
while(~scanf("%s",p)){
m=strlen(p);
getnext();
int cnt=;
ans[cnt++]=m;
int t=m;
while(t!=-){
if(nxt[t]>)
ans[cnt++]=nxt[t];
t=nxt[t];
}
for(int i=cnt-;i>=;i--){
printf("%d%c",ans[i],i==?'\n':' ');
}
}
return ;
}
POJ 2752 Seek the Name, Seek the Fame(KMP求公共前后缀)的更多相关文章
- KMP + 求相等前后缀--- POJ Seek the Name, Seek the Fame
Seek the Name, Seek the Fame Problem's Link: http://poj.org/problem?id=2752 Mean: 给你一个字符串,求这个字符串中有多少 ...
- 【kmp+求所有公共前后缀长度】poj 2752 Seek the Name, Seek the Fame
http://poj.org/problem?id=2752 [题意] 给定一个字符串,求这个字符串的所有公共前后缀的长度,按从小到达输出 [思路] 利用kmp的next数组,最后加上这个字符串本身 ...
- POJ 2752 (kmp求所有公共前后缀长度)
<题目链接> <转载于> 题目大意: 给出一个字符串str,求出str中存在多少子串,使得这些子串既是str的前缀,又是str的后缀.从小到大依次输出这些子串的长度.即输出该 ...
- 题解报告: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 ...
- (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 ...
随机推荐
- 【BZOJ4198】【NOI2015】荷马史诗(贪心,Huffman树)
[BZOJ4198][NOI2015]荷马史诗(贪心,Huffman树) 题面 BZOJ 洛谷 题解 合并果子都是不知道多久以前做过的了.现在才知道原来本质就是一棵哈夫曼树啊. 这题我们仔细研究一下题 ...
- HDU.1846 Brave Game (博弈论 巴什博弈)
HDU.1846 Brave Game (博弈论 巴什博弈) 题意分析 巴什博奕裸题 博弈论快速入门 代码总览 include <bits/stdc++.h> using namespac ...
- 小Q与内存
Portal --> broken qwq Description (这个描述好像怎么都精简不起来啊qwq) 大概是说你的计算机有1GB的物理内存,按照Byte寻址,其物理地址空间为\(0\si ...
- 初识python版本
区别一: python2x:源码重复不规范. python3x:重新整理规范了源码. 区别二: python2x: 默认的编码方式ascii,显示中文需要首行添加:# _*_ encoding: u ...
- HDU--2722
原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=2722 分析:简单最短路,读入数据烦. #include<iostream> #includ ...
- jdk1.5后枚举类的定义规则
转: http://blog.csdn.net/willcold/article/details/12844487 JDK1.5 新增的enum关键字用于定义枚举类 枚举类也 ...
- hihocoder 1580 dp最大子矩阵和
题意: 给出n*m的矩阵求最大子矩阵和,要求必须把矩阵中的某一个元素替换成p 代码: //求最大子矩阵和,容易想到压缩之后dp但是这道题要求必须替换一次p,必然优先替换最小的. //这样如果求得的结果 ...
- \G,sql中select 如果太长,可以在后面放\G,竖行显示~~~~
1.使用\G按行垂直显示结果 如果一行很长,需要这行显示的话,看起结果来就非常的难受. 在SQL语句或者命令后使用\G而不是分号结尾,可以将每一行的值垂直输出. mysql> select * ...
- Java基础之equals() 和 hashCode()
equals()是Object中的一个方法: public boolean equals(Object obj) { return (this == obj); } 在Object中equals()方 ...
- springboot项目启动成功后执行一段代码的两种方式
springboot项目启动成功后执行一段代码的两种方式 实现ApplicationRunner接口 package com.lnjecit.lifecycle; import org.springf ...