Seek the Name, Seek the Fame
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 10593 | Accepted: 5140 |
Description
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:)
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.
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
考察的就是对next数组的理解
如ababcababababcabab next数组为0 0 1 2 0 1 2 3 4 3 4 3 4 5 6 7 8 9(1-18)
根据题意len=18符合题意,而next[18]=9;故pattern[0]—pattern[9-1] 和 pattern[9]-pattern[18-1](9个数字)完全一样,然后考虑前9个数字,next[9]=4; 由上面得,a[0]-a[3] 和 a[5]-a[8]一样,即p[0]~p[next[j]-1]与p[j-next[j]]~p[j-1]完全相等。
因为这是逆序访问,而题目要求是从小到大输出,可以借助stack,或者bfs搞定。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <stack>
using namespace std;
const int MAXN = 400005;
char pattern[MAXN];
int next[MAXN];
void get_next(){
next[0]=next[1]=0;
int m =strlen(pattern);
for(int i=1; i<m; i++){
int j = next[i];
while(j && pattern[i]!=pattern[j]) j=next[j];
next[i+1]=(pattern[i]==pattern[j]? j+1 : 0);
}
}
int flag;
void dfs(int len){
if(len==0) return;
dfs(next[len]);
if(!flag){
cout<<len;
flag=1;
}else
cout<<" "<<len;
}
void print(){
stack<int> S;
int m = strlen(pattern);
S.push(m);
while(next[m]){
S.push(next[m]);
m=next[m];
}
while(S.size()>1){
int a = S.top();
S.pop();
cout<<a<<" ";
}
int a=S.top();
cout<<a<<endl;
S.pop();
}
int main()
{
while(scanf("%s",pattern)!=EOF){
get_next();
//print();
flag=0;
dfs(strlen(pattern));
cout<<endl;
}
return 0;
}