【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 std;
 7 typedef long long ll;
 8
 9 const int maxn=4e5+3;
10 char str[maxn];
11 int nxt[maxn];
12 int ans[maxn];
13 void kmp_pre(char x[],int m,int nxt[])
14 {
15     int i,j;
16     j=nxt[0]=-1;
17     i=0;
18     while(i<m)
19     {
20         while(j!=-1&&x[i]!=x[j]) j=nxt[j];
21         nxt[++i]=++j;
22     }
23 }
24
25 int main()
26 {
27     while(~scanf("%s",str))
28     {
29         memset(nxt,0,sizeof(nxt));
30         int len=strlen(str);
31         kmp_pre(str,len,nxt);
32         int cnt=0;
33         int x=len;
34         while(nxt[x])
35         {
36             ans[cnt++]=nxt[x];
37             x=nxt[x];
38         }
39         ans[cnt++]=len;
40         sort(ans,ans+cnt);
41         for(int i=0;i<cnt;i++)
42         {
43             if(i==0)
44                 printf("%d",ans[i]);
45             else
46                 printf(" %d",ans[i]);
47         }
48         puts("");
49     }
50     return 0;
51 }

kmp

时间: 2024-07-28 22:23:02

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

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]进行递

Codeforces Round #545 (Div. 2)D(KMP,最长公共前后缀,贪心)

#include<bits/stdc++.h>using namespace std;const int N=1000007;char s1[N],s2[N];int len1,len2;int nex[N];int cnt1[7],cnt2[7];int main(){    scanf("%s %s",s1+1,s2+1);    len1=strlen(s1+1);    len2=strlen(s2+1);    for(int i=1;i<=len1;i++

可变字符串 插入 删除 重赋值 替换 前后缀 长度

#import <Foundation/Foundation.h> int main(int argc, const char * argv[]) { @autoreleasepool { //nsstring父类   NSMutableString可变字符串子类 NSMutableString *mustr=[[NSMutableString alloc]init]; NSMutableString *mustr1=[NSMutableString stringWithFormat:@&qu

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]具有最长

POJ 2752+KMP+利用next数组性质求出所有相同的前缀和后缀

题目链接:点击进入 这个题目要求所有相同的前缀和后缀的长度.我们可以利用KMP算法中next数组的性质,在next[len]这个点不断的失配下去,这样就可以将所有相同的前后缀的长度求出来.还要注意这个中整个串的长度也可以看成是一个合法的解. 代码如下: #include<iostream> #include<cstdio> #include<cstring> using namespace std; const int maxn=400000+100; char str

POJ 2752 Seek the Name, Seek the Fame(求所有既是前缀又是后缀的子串长度)

题目链接:http://poj.org/problem?id=2752 题意:给你一个字符串,求出所有前缀后缀(既是前缀又是后缀的子串)的长度 思路:首先整个字符串肯定既是前缀又是后缀,为最大的前缀后缀. 假设next[len] = k,也即:s[1,k] = s[len-k+1,len]此时s[1,k]是前缀后缀. 处理完next[len]后跳转到next[k+1],用这种方法可以得到所有的前缀后缀. code: 1 #include <cstdio> 2 #include <cstr

poj 2752 求一个字符串所有的相同前后缀

求一个字符串所有的相同前后缀Sample Input ababcababababcababaaaaaSample Output 2 4 9 181 2 3 4 5 1 #include <iostream> 2 #include <cstring> 3 #include <cstdio> 4 #include <stack> 5 using namespace std; 6 7 const int N = 400010; 8 int next[N]; 9 c

poj3080(Blue Jeans)kmp求多个串公共子串

题意:给出1-10个长度为60的字符串,求出最长的公共子串(长度不能小于3),如果有多个一样长的,输出字典序最短的. 解法:想到kmp时,自己第一反应枚举第一个串的所有子串,在其他所有串中走一遍kmp,复杂度为10*60*60*60,但是发现只需枚举第一个串后缀就可以,每次枚举记录在所有串能走最远中走的最短的那个长度.这样复杂度就成了10*60*60,0ms AC. 代码: /**************************************************** * autho

POJ 3080 Blue Jeans(KMP 最长公共子串)

Blue Jeans Description The Genographic Project is a research partnership between IBM and The National Geographic Society that is analyzing DNA from hundreds of thousands of contributors to map how the Earth was populated. As an IBM researcher, you ha