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[]数组的对称性质。

代码

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<map>
 5 #include<string>
 6 using namespace std;
 7 const int N=1e6+5;
 8
 9 int m;
10 int nxt[N],ans[N];
11 char p[N];
12
13 void getnext(){
14     int i,j;
15     i=0,j=nxt[0]=-1;
16
17     while(i<m){
18         while(j!=-1&&p[i]!=p[j])
19             j=nxt[j];
20         nxt[++i]=++j;
21     }
22 }
23
24 int main(){
25     while(~scanf("%s",p)){
26         m=strlen(p);
27         getnext();
28         int cnt=0;
29         ans[cnt++]=m;
30         int t=m;
31         while(t!=-1){
32             if(nxt[t]>0)
33                 ans[cnt++]=nxt[t];
34             t=nxt[t];
35         }
36         for(int i=cnt-1;i>=0;i--){
37             printf("%d%c",ans[i],i==0?‘\n‘:‘ ‘);
38         }
39     }
40     return 0;
41 }

原文地址:https://www.cnblogs.com/fu3638/p/8491138.html

时间: 2024-10-08 10:37:40

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: 给你一个字符串,求这个字符串中有多少前缀是和后缀相等的, 按递增顺序输出这些前缀的长度. analyse: KMP之next数组的运用. next[k]表示的是s[0....next[k]] 和s[k-next[k] .... k] 这段是相等的,即以k结尾的最长相等前后缀长度. next[k-1]表示的是与s[0....k]具有最长

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

http://poj.org/problem?id=2752 [题意] 给定一个字符串,求这个字符串的所有公共前后缀的长度,按从小到达输出 [思路] 利用kmp的next数组,最后加上这个字符串本身 [AC] 1 #include<iostream> 2 #include<cstring> 3 #include<string> 4 #include<cstdio> 5 #include<algorithm> 6 using namespace s

POJ2752——Seek the Name, Seek the Fame

Seek the Name, Seek the Fame Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 12620   Accepted: 6198 Description The little cat is so famous, that many couples tramp over hill and dale to Byteland, and asked the little cat to give names t

poj 2752 Seek the Name, Seek the Fame KMP

对于KMP算法中next函数的应用 题意是对于一个字符串的前缀和后缀比较是否相等,再把相等的可能按字符串长度进行输出 #include <iostream> #include<stdio.h> #include<string.h> using namespace std; int len; int next[1000005]; char s[1000005]; int kmp_next() { int i=0,j=-1; next[0]=-1; while(i<l

(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: 65536K Total Submissions: 14611   Accepted: 7320 Description The little cat is so famous, that many couples tramp over hill and dale to Byteland, and asked

Seek the Name, Seek the Fame poj 2752

Seek the Name, Seek the Fame Time Limit : 4000/2000ms (Java/Other)   Memory Limit : 131072/65536K (Java/Other) Problem Description The little cat is so famous, that many couples tramp over hill and dale to Byteland, and asked the little cat to give n

POJ 2752 Seek the Name, Seek the Fame KMP题解

本题是KMP的next数组的灵活运用. 具体就是看最后整个数列的最后一个字母,能有多少前缀. 理解了next数组就很容易了. #include <stdio.h> #include <string.h> #include <vector> using std::vector; const int MAX_N = 400001; char name[MAX_N]; int next[MAX_N], len; void genNext() { for (int i = 1,

[kuangbin带你飞]专题十六 KMP &amp; 扩展KMP &amp; Manacher H - Seek the Name, Seek the Fame POJ - 2752(kmp的next数组应用)

H - Seek the Name, Seek the Fame POJ - 2752 题目链接:https://vjudge.net/contest/70325#problem/H 题目: The little cat is so famous, that many couples tramp over hill and dale to Byteland, and asked the little cat to give names to their newly-born babies. Th

KMP中next的应用 POJ 2752 Seek the Name, Seek the Fame

Seek the Name, Seek the Fame Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 19163   Accepted: 9849 Description The little cat is so famous, that many couples tramp over hill and dale to Byteland, and asked the little cat to give names t