KMP + 求相等前后缀--- POJ Seek the Name, Seek the Fame
来源:互联网 发布:淘宝手机端首页装修 编辑:程序博客网 时间:2024/06/06 02:12
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]具有最长后缀,且与s[0...k]具有最长前缀的位置。也就是在k位置失配后需要移到next[k-1]的位置。
这里特别说明一下,失配后移到next[k]还是next[k-1],取决于next数组的开始编号。如果从0开始编号,那就移到next[k-1];从1开始编号,就移到next[k]。
Time complexity: O(N)
Source code:
/*
* this code is made by crazyacking
* Verdict: Accepted
* Submission Date: 2015-07-28-08.55
* Time: 0MS
* Memory: 137KB
*/
#include <queue>
#include <cstdio>
#include <set>
#include <string>
#include <stack>
#include <cmath>
#include <climits>
#include <map>
#include <cstdlib>
#include <iostream>
#include <vector>
#include <algorithm>
#include <cstring>
#define LL long long
#define ULL unsigned long long
using namespace std;
const int MAXN=400010;
int n;
char s[MAXN];
int Next[MAXN];
int ans[MAXN];
void getNext()
{
Next[0]=0;
for(int i=1,k=0;i<n;++i)
{
while(s[i]!=s[k]&& k) k=Next[k-1];
if(s[i]==s[k]) ++k;
Next[i]=k;
}
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
while(~scanf("%s",s))
{
n=strlen(s);
getNext();
int k=n-1;
int cnt=0;
while(Next[k])
{
ans[cnt++]=Next[k];
k=Next[k-1];
}
ans[cnt++]=n;
sort(ans,ans+cnt);
for(int i=0;i<cnt;++i)
{
printf(i==cnt-1?"%d\n":"%d ",ans[i]);
}
}
return 0;
}
/*
*/
* this code is made by crazyacking
* Verdict: Accepted
* Submission Date: 2015-07-28-08.55
* Time: 0MS
* Memory: 137KB
*/
#include <queue>
#include <cstdio>
#include <set>
#include <string>
#include <stack>
#include <cmath>
#include <climits>
#include <map>
#include <cstdlib>
#include <iostream>
#include <vector>
#include <algorithm>
#include <cstring>
#define LL long long
#define ULL unsigned long long
using namespace std;
const int MAXN=400010;
int n;
char s[MAXN];
int Next[MAXN];
int ans[MAXN];
void getNext()
{
Next[0]=0;
for(int i=1,k=0;i<n;++i)
{
while(s[i]!=s[k]&& k) k=Next[k-1];
if(s[i]==s[k]) ++k;
Next[i]=k;
}
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
while(~scanf("%s",s))
{
n=strlen(s);
getNext();
int k=n-1;
int cnt=0;
while(Next[k])
{
ans[cnt++]=Next[k];
k=Next[k-1];
}
ans[cnt++]=n;
sort(ans,ans+cnt);
for(int i=0;i<cnt;++i)
{
printf(i==cnt-1?"%d\n":"%d ",ans[i]);
}
}
return 0;
}
/*
*/
0 0
- KMP + 求相等前后缀--- POJ Seek the Name, Seek the Fame
- POJ 2752 Seek the Name, Seek the Fame [前后缀相同的KMP]
- 【kmp算法next数组求解公共前后缀】Seek the Name, Seek the Fame POJ
- poj2752 Seek the Name, Seek the Fame(KMP前后缀相同)
- Seek the Name, Seek the Fame (POJ_2752) KMP + 最大公共前后缀
- POJ2752 Seek the Name, Seek the Fame(前后缀最大匹配,kmp)
- poj 2752 Seek the Name, Seek the Fame(KMP)
- poj 2752 Seek the Name, Seek the Fame---kmp
- POJ 2752 Seek the Name, Seek the Fame KMP
- [poj 2752] Seek the Name, Seek the Fame[KMP]
- poj 2752 Seek the Name, Seek the Fame (KMP)
- POJ 2752 Seek the Name, Seek the Fame (KMP)
- KMP poj 2752 Seek the Name, Seek the Fame
- POJ 2752 Seek the Name, Seek the Fame KMP
- poj 2752 Seek the Name, Seek the Fame(KMP)
- POJ 2752 Seek the Name, Seek the Fame(KMP)
- POJ 2752 Seek the Name, Seek the Fame / KMP
- poj 2752 Seek the Name, Seek the Fame(KMP)
- 菜鸟vimer成长记——第2.1章、normal模式
- 菜鸟vimer成长记——第2.2章、visual模式
- KMP + 求最小循环节 --- HDU 1358 Period
- KMP + 求最小循环节 --- HUST 1010
- KMP + 求最小循环节 --- POJ 2406 Power Strings
- KMP + 求相等前后缀--- POJ Seek the Name, Seek the Fame
- “练习一万小时”定律
- Java动态代理机制详解(JDK 和CGLIB,Javassist,ASM)
- 2015 Multi-University Training Contest 3 1002 RGCDQ
- 2015 Multi-University Training Contest 3 1001 Magician
- view、surfaceview、GLsurfaceview的区别和应用场景
- 2015 Multi-University Training Contest 3 1006 Beautiful Set
- 深入.NET第九章 小型资源管理器
- LRU算法