题目链接: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求公共前后缀)的更多相关文章

  1. KMP + 求相等前后缀--- POJ Seek the Name, Seek the Fame

    Seek the Name, Seek the Fame Problem's Link: http://poj.org/problem?id=2752 Mean: 给你一个字符串,求这个字符串中有多少 ...

  2. 【kmp+求所有公共前后缀长度】poj 2752 Seek the Name, Seek the Fame

    http://poj.org/problem?id=2752 [题意] 给定一个字符串,求这个字符串的所有公共前后缀的长度,按从小到达输出 [思路] 利用kmp的next数组,最后加上这个字符串本身 ...

  3. POJ 2752 (kmp求所有公共前后缀长度)

    <题目链接> <转载于> 题目大意:  给出一个字符串str,求出str中存在多少子串,使得这些子串既是str的前缀,又是str的后缀.从小到大依次输出这些子串的长度.即输出该 ...

  4. 题解报告: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 ...

  5. (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 ...

  6. 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的简单应 ...

  7. KMP POJ 2752 Seek the Name, Seek the Fame

    题目传送门 /* 题意:求出一个串的前缀与后缀相同的字串的长度 KMP:nex[]就有这样的性质,倒过来输出就行了 */ /************************************** ...

  8. 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 ...

  9. 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 ...

随机推荐

  1. Jenkins远程代码执行漏洞检查(CVE-2017-1000353)

    Jenkins的反序列化漏洞,攻击者使用该漏洞可以在被攻击服务器执行任意代码,漏洞利用不需要任何的权限 漏洞影响范围: 所有Jenkins主版本均受到影响(包括<=2.56版本)所有Jenkin ...

  2. mysql互为主从复制配置笔记--未读,稍后学习

    MySQL-master1:192.168.72.128 MySQL-master2:192.168.72.129 OS版本:CentOS 5.4MySQL版本:5.5.9(主从复制的master和s ...

  3. day4-python基础

  4. apk签名验证机制

    声明: 1.本帖转载自:http://riusksk.blogbus.com/logs/272154406.html,仅供自用,勿喷 2.欢迎交流学习 签名后的APK,在/META-INF目录下会生成 ...

  5. 【转】虚拟机克隆之后,网卡名称从eth0变成eth1之后的解决办法

    使用VMware安装了CentOS虚拟机,克隆之后使用service network restart指令来重新启动网络服务时,会看到有eth0网卡不存在的提示.   出现这种现象的原因是,很多Linu ...

  6. 用for语句从数组中剔除数据,注意,count,要放到for语句之外才行

    date_default_timezone_set('Asia/Shanghai'); $arr = array( ,), ,), ,), ,) ); print_r($arr); ;$i<co ...

  7. Nginx高级应用之Location Url 配置

    原文地址:https://www.linuxidc.com/Linux/2017-03/141910.htm 基本配置 为了探究nginx的url配置规则,当然需要安装nginx.我使用了vagran ...

  8. C++ string的那些坑

    1. size_type find_first_of( const basic_string &str, size_type index = 0 ); 查找在字符串中第一个与str中的某个字符 ...

  9. Xamarin Error:Could not find android.jar for API Level 23.

    背景:打开别人Xamarin项目找不到android.jar文件 报错: 解决方案1:工具——Android——Amdroid SDK 管理器…出现以下窗口(根据需要的[API级别])勾选相应的Pla ...

  10. Web Api返回值

    首先:注明,我还没这么强的功力啦!这是我看的网上的,因为怕博主删除就自己复制了一下 博主的网址是:http://www.cnblogs.com/landeanfen/p/5501487.html 使用 ...