poj2752—kmp的next数组的简单应用
2014-04-22 14:52
387 查看
因为next数组表示的意义就是与前面匹配是前面的最大下标,而下标=长度-1,所以只要求出next数组之后对j从后往前找next就可以了;(找的过程可以用迭代来实现)
#include<cstdio>
#include<cstring>
#include<iostream>
using namespace std;
const int maxn=400000;
int lt;
char t[maxn];
int ans[maxn];
int Next[maxn];
void getNext()
{
int i,j=-1;
Next[0]=-1;
for(i=1;i<lt;i++){
while(j!=-1&&t[i]!=t[j+1])j=Next[j];
if(t[i]==t[j+1])j++;
Next[i]=j;
}
}
int main()
{
while(~scanf("%s",t)){
lt=strlen(t);
getNext();
int j=lt-1,sum=0;
while(j!=-1){
ans[sum++]=j+1;
j=Next[j];
}
for(int i=sum-1;i>=0;i--)
printf("%d%c",ans[i],i==0?'\n':' ');
}
return 0;
}
#include<cstdio>
#include<cstring>
#include<iostream>
using namespace std;
const int maxn=400000;
int lt;
char t[maxn];
int ans[maxn];
int Next[maxn];
void getNext()
{
int i,j=-1;
Next[0]=-1;
for(i=1;i<lt;i++){
while(j!=-1&&t[i]!=t[j+1])j=Next[j];
if(t[i]==t[j+1])j++;
Next[i]=j;
}
}
int main()
{
while(~scanf("%s",t)){
lt=strlen(t);
getNext();
int j=lt-1,sum=0;
while(j!=-1){
ans[sum++]=j+1;
j=Next[j];
}
for(int i=sum-1;i>=0;i--)
printf("%d%c",ans[i],i==0?'\n':' ');
}
return 0;
}
相关文章推荐
- POJ 2752: S 4000 eek the Name, Seek the Fame(简单KMP-NEXT数组的应用)
- POJ 2752 KMP中next数组的应用
- POJ - 2752 Seek the Name, Seek the Fame(KMP中对next数组的前缀后缀匹配应用)
- poj2752_Seek the Name,Seek the Fame_KMP(next数组定义的应用)
- poj 2752 KMP的next数组应用(prefix-suffix string)
- POJ_2752_KMP-next数组的应用
- POJ2752 KMP中next数组的应用
- poj 2752 kmp(next数组的应用)
- POJ-2752 Seek the Name, Seek the Fame(kmp中next数组的应用)
- POJ 2752 Seek the Name, Seek the Fame(KMP next数组应用)
- POJ-2752-Seek the Name, Seek the Fame(KMPnext数组应用)
- POJ 2752、2406、1961 KMP的next[](或p[])简单应用
- POJ 2752 Seek the Name, Seek the Fame(KMP,next数组的应用)
- POJ 2752 KMP中next数组的应用
- POJ 2752 KMPnext的应用
- POJ 2572(Seek the Name, Seek the Fame) KMP中next数组的应用
- poj-2406 kmp中的next数组应用
- poj 2406 Power Strings(kmp next数组的应用)
- POJ 2752 KMP算法中next数组的应用
- POJ 2406 KMP next数组的应用