您的位置:首页 > 其它

Seek the Name, Seek the Fame---poj2752(kmp中的Next数组)

2015-09-24 15:41 337 查看
题目链接:http://poj.org/problem?id=2752

题意就是求出是已知s串的前缀的长度x,并且要求此前缀也是s串的后缀;求出所有的 x ;

Next【i】的含义是前i个元素的前缀和后缀的最大匹配;

所以本题就很简单了。。。

#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<string.h>
using namespace std;

const int N = 4*1e5+7;

char s
;
int Next
, n, a
;

void GetNext()
{
int i=0, j=-1;
Next[0] = -1;
while(i<n)
{
if(j==-1 || s[i] == s[j])
Next[++i] = ++j;
else
j=Next[j];
}
}
int main()
{
int i;
while(scanf("%s", s)!=EOF)
{
n = strlen (s);
GetNext();
a[0] = n;
i=1;
while(Next[a[i-1]]!=0)
{
a[i] = Next[a[i-1]];
i++;
}
sort(a, a+i);
for(int j=0; j<i; j++)
printf("%d%c", a[j], i-1==j?'\n':' ');
}
return 0;
}


View Code
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: