题目链接:http://poj.org/problem?id=2752
| Time Limit: 2000MS | Memory Limit: 65536K | |
| Total Submissions: 12018 | Accepted: 5906 |
Description
Input
Output
Sample Input
ababcababababcabab aaaaa
Sample Output
2 4 9 18 1 2 3 4 5
Source
题意:寻找前子串与后子串相等的子串。
代码如下:
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
using namespace std;
#define MAXN 1000017
int next[MAXN];
int ans[MAXN];
int len;
void getnext( char T[])
{
int i = 0, j = -1;
next[0] = -1;
while(i < len)
{
if(j == -1 || T[i] == T[j])
{
i++,j++;
next[i] = j;
}
else
j = next[j];
}
}
int main()
{
char ss[MAXN];
int length;
while(~scanf("%s",ss))
{
len = strlen(ss);
getnext(ss);
int n = 0 ;int i = len;
ans[0]=len;
while(next[i] > 0 )//倒着扫描next数组
{//递归查找前子串和后子串相等的子串
i = next[i];
ans[++n] = i ;
}
for( i = n ; i >= 0 ; i--)
printf("%d ",ans[i]);
printf("\n");
}
return 0;
}
poj2752 Seek the Name, Seek the Fame(next数组的运用),布布扣,bubuko.com
poj2752 Seek the Name, Seek the Fame(next数组的运用)
原文地址:http://blog.csdn.net/u012860063/article/details/38541957