Seek the Name, Seek the Fame
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 8567 | Accepted: 4036 |
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
Source
首先这个字符串本身肯定是满足的。
若已知`T1`T2`T3...`Tk=`Ti-k+1`Ti-k+2`Ti-k+1...`Ti
且有`T1`T2`T3...`Tk2=`Tk-k2+1`Tk-k2+2`Tk-k2+3...`Tk(k2<=k)
可知`T1`T2`T3...`Tk2=`Ti-k2+1`Ti-k2+2`Ti-k2+3...`Ti。
即满足要求的字串。
所以我们要求的,即是:
每一个最长首尾相同子串它的最长首尾相同的子串。
因此这道题只需用到next函数。
当然,注意到next[i]表示的是1~i-1的最长首尾相同的子串,所以我们应该把它当做开区间处理,而输出的时候,亦需减一。
另外要注意,因为虽然next[i]表示的是1~i-1的最长首尾相同的子串,但是除开了自我重复,也就是1~i-1和1~i-1是相同的情况,很简单,因为如果要考虑这种情况,那么可知next[i] = i-1,这个算法就没有意义了。因为这个原因,我们要单独考虑1~n和1~n是相同的情况,这是符合题意的。
#include <cstdio>
#include <cstring>
#include <string>
char str[400010];
long next[400010];
long len;
void get_next()
{
long k = 0;
long j = 1;
while (j < len+1)
{
if (k==0||str[j]==str[k])
{
j ++; k ++;
next[j] = k;
}
else k = next[k];
}
}
long que[400010];
int main()
{
freopen("stnstf.in","r",stdin);
freopen("stnstf.out","w",stdout);
while (scanf("%s",str+1)!=EOF)
{
len = strlen(str+1);
get_next();
que[0] = 1;
que[1] = len;
long now = next[len+1];
while (now > 1)
{
que[++que[0]] = now-1;
now = next[now];
}
for (long i=que[0];i>0;i--)
{
printf("%ld ",que[i]);
}
printf("\n");
}
return 0;
}