题干:

The little cat is so famous, that many couples tramp over hill and dale to Byteland, and asked the little cat to give names to their newly-born babies. They seek the name, and at the same time seek the fame. In order to escape from such boring job, the innovative little cat works out an easy but fantastic algorithm: 

Step1. Connect the father's name and the mother's name, to a new string S. 
Step2. Find a proper prefix-suffix string of S (which is not only the prefix, but also the suffix of S). 

Example: Father='ala', Mother='la', we have S = 'ala'+'la' = 'alala'. Potential prefix-suffix strings of S are {'a', 'ala', 'alala'}. Given the string S, could you help the little cat to write a program to calculate the length of possible prefix-suffix strings of S? (He might thank you by giving your baby a name:) 

Input

The input contains a number of test cases. Each test case occupies a single line that contains the string S described above. 

Restrictions: Only lowercase letters may appear in the input. 1 <= Length of S <= 400000. 

Output

For each test case, output a single line with integer numbers in increasing order, denoting the possible length of the new baby's name.

Sample Input

ababcababababcabab
aaaaa

Sample Output

2 4 9 18
1 2 3 4 5

题目大意:

给一个字符串S,找一个 S 的“前缀-后缀串”作为新生儿的名字(“前缀-后缀串”的定义是 既是S的前缀,又是S的后缀) 

比如:S = 'alala'。S的 “前缀-后缀串” 是{'a','ala','alala'}。给定字符串S,让你计算S的可能  “前缀-后缀串”  的长度。

解题报告:

  求到最后一个字符的next数组,然后每次跳转到next,,最后倒序输出 就可以得到答案了

AC代码:

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<queue>
#include<stack>
#include<map>
#include<vector>
#include<set>
#include<string>
#include<cmath>
#include<cstring>
#define ll long long
#define pb push_back
#define pm make_pair
#define fi first
#define se second
using namespace std;
const int MAX = 2e6 + 5;
char s[MAX];
int Next[MAX],len,ans[MAX];
void getnext() {
	Next[0] = -1;
	int k = -1,j = 0;
	int len = strlen(s);
	while(j < len) {
		if(k == -1 || s[k] == s[j]) {
			k++,j++;
			Next[j] = k;
		} else k = Next[k];
	}
}
int main() 
{
	int n;
	while(~scanf("%s",s)) {
		getnext();
		int j = strlen(s),tot = 0;
		while(j) ans[++tot] = j,j = Next[j];
		for(int i = tot; i>=1; i--) {
			printf("%d%c",ans[i], i == 1 ? '\n' : ' ');
		}
	}
	return 0;
}

总结:

  这也告诉我们next数组中只有next[0] 才= -1;其他的就算是没有,也是=0;;;main函数中这个while(j)就可以证明这一点、、、