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 ...
随机推荐
- Opencv2.4.9+win7+VS2012一次性配置的方法--通过建立属性表永久配置
Opencv的配置对于初学者很麻烦,网上的教程也非常多,针对不同的操作系统.opencv版本.Visual studio版本都有相应的教程,但即便是按照教程一步一步来,仍然难免出错,很多教程还是一次性 ...
- sysbench - 单组件式测试工具
1 安装 > ./configure --with-mysql-includes=/usr/local/mysql/include --with-mysql-libs=/usr/local/my ...
- python基础----isinstance(obj,cls)和issubclass(sub,super)、反射、__setattr__,__delattr__,__getattr__、二次加工标准类型(包装)
一.isinstance(obj,cls)和issubclass(sub,super) isinstance(obj,cls)检查是否ob ...
- Virtual Box虚拟机下CentOS网络设置
VirtualBox中有4中网络连接方式: a. NAT 网络地址转换模式(Network Address Translation)b. Bridge ...
- Codeforces 937.D Sleepy Game
D. Sleepy Game time limit per test 2 seconds memory limit per test 256 megabytes input standard inpu ...
- 新式类 VS 经典类
一.概述 Python中支持多继承,也就是一个子类可以继承多个父类/基类.当一个调用一个自身没有定义的属性时,它是按照何种顺序去父类中寻找的呢?尤其是当众多父类中都包含有同名的属性,这就涉及到新式类 ...
- nginx 前后分离,地址重写,url匹配中遇到的问题
我遇到的问题: 前端用vue的路由做页面路由,后台用spring mvc做数据接口,但是遇到路由地址和接口地址无法区分的问题,导致nginx无法正确准发比如: 1)http://127.0.0.1/i ...
- 前端PHP入门-023-重点日期函数之程序执行时间检测
我们有的时经常需要做程序的执行时间执行效率判断. 实现的思路如下: <?php //记录开始时间 //记录结整时 // 开始时间 减去(-) 结束时间 得到程序的运行时间 ?> 可是大家不 ...
- NDK编译时两 .so之间调用问题
Android C++(NDK)项目需要调用别人的代码,因此将其编译成了.so库,而自己的代码也编成了一个.so库. 结果编译成功,但是在运行时自己的.so调用别人的.so会失败,提示说没有正确传入参 ...
- API图片路径和超链接语义化转换
<!DOCTYPE html><html> <head> <meta charset="UTF-8"> <title>& ...