【poj2752】Seek the Name, Seek the Fame kmp
2018-02-18 15:39
246 查看
题意:给你一个字符串,输出所有不同的前缀后缀字符串(即是前缀又是后缀)的长度。题解:运用kmp中的next数组的思想递推一下即可。
//poj2752 Seek the Name, Seek the Fame
#include<iostream>
#include<algorithm>
#include<string>
#include<cstring>
#include<cstdio>
using namespace std;
string s;
int nxt[400010],ans[400010];
int n,m, cnt;
int main()
{
while(cin>>s)
{
cnt=0;
memset(ans,0,sizeof(ans));
memset(nxt,0,sizeof(nxt));
cnt=0;
n=s.length();
nxt[0]=0;nxt[1]=0;
for (int i=1;i<n;i++)
{
int j=nxt[i];
while(j&&s[j]!=s[i]) j=nxt[j];
nxt[i+1]=s[i]==s[j]?j+1:0;
}
int k=n;
while(k) ans[cnt++]=k,k=nxt[k];
ans[cnt++]=s[0]==s[n-1]?1:0,cnt--;
sort(ans,ans+cnt);
printf("%d",ans[0]);
for (int i=1;i<cnt;i++) printf(" %d",ans[i]);
printf("\n");
}
return 0;
}
//poj2752 Seek the Name, Seek the Fame
#include<iostream>
#include<algorithm>
#include<string>
#include<cstring>
#include<cstdio>
using namespace std;
string s;
int nxt[400010],ans[400010];
int n,m, cnt;
int main()
{
while(cin>>s)
{
cnt=0;
memset(ans,0,sizeof(ans));
memset(nxt,0,sizeof(nxt));
cnt=0;
n=s.length();
nxt[0]=0;nxt[1]=0;
for (int i=1;i<n;i++)
{
int j=nxt[i];
while(j&&s[j]!=s[i]) j=nxt[j];
nxt[i+1]=s[i]==s[j]?j+1:0;
}
int k=n;
while(k) ans[cnt++]=k,k=nxt[k];
ans[cnt++]=s[0]==s[n-1]?1:0,cnt--;
sort(ans,ans+cnt);
printf("%d",ans[0]);
for (int i=1;i<cnt;i++) printf(" %d",ans[i]);
printf("\n");
}
return 0;
}
相关文章推荐
- [poj2752]Seek the Name, Seek the Fame_KMP
- 【POJ2752】【KMP】Seek the Name, Seek the Fame
- [poj2752]Seek the Name, Seek the Fame(KMP)
- POJ2752 Seek the Name, Seek the Fame 【KMP】
- 【POJ2752】Seek the Name, Seek the Fame(KMP)
- 【poj2752】Seek the Name, Seek the Fame KMP
- 【KMP】POJ2752 Seek the Name, Seek the Fame
- POJ2752 Seek the Name, Seek the Fame(kmp)
- POJ2752 Seek the Name, Seek the Fame 【KMP】
- poj2752 Seek the Name, Seek the Fame(kmp)
- Seek the Name, Seek the Fame---poj2752(kmp中的Next数组)
- 【POJ2752】Seek the Name, Seek the Fame-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中next数组的应用)
- poj 2752 Seek the Name, Seek the Fame(KMP应用)