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 aaaaaSample Output
2 4 9 18 1 2 3 4 5
#include<stdio.h>
#include<iostream>
#include<string.h>
using namespace std;
int ne[400010];
char s[400010];
int b[400010];
int n,m;
int l;
void get_next()
{
int i=0,j=-1;
ne[0]=-1;
while(i<l)
{
if(s[i]==s[j]||j==-1)
{
ne[++i]=++j;
}
else
j=ne[j];
}
}
int main()
{
while(cin>>s)
{
l=strlen(s);
get_next();
int t=ne[l];
int count=0;
while(t!=-1)
{
if(s[t-1]==s[l-1])
{
b[count++]=t;
}
t=ne[t];
}
for(int i=count-1;i>=0;i--)
printf("%d ",b[i]);
printf("%d\n",l);
}
return 0;
}
这篇博客探讨了一种创新的字符串处理算法,该算法由一只著名的小猫提出,用于为新生儿取名。它涉及到字符串的前缀和后缀匹配,并通过编程实现找出可能的名字长度。示例中展示了如何处理'ababcababababcabab'和'aaaaa'这两个字符串,输出了可能的名字长度。博客深入解析了算法的实现和应用,适合对字符串处理和算法感兴趣的读者。
659

被折叠的 条评论
为什么被折叠?



