链接:http://poj.org/problem?id=2752
Time Limit: 2000MS | Memory Limit: 65536KB | 64bit IO Format: %I64d & %I64u |
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
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
原文大意就说要求出第i个数到第一个数之间的最大匹配数,比如一串数:
ababcababababcabab
写出来的next数组是-1、0、0、1、2、0、1、2、3、4、1、2、3、4、5、6、7、8、9;
结果就是输出2、4、9、18;(18是整个字符串的长度可单独输出);
#include<stdio.h>
#include<string.h>
char ss[400005];
int next[400005];
int gg[400005];
int main()
{
int i,j,k,m,n;
memset(ss,'\0',sizeof(ss));
while(scanf("%s",ss)!=EOF)
{
int len=strlen(ss);
i=0;
j=-1;
next[0]=-1;
while(i<len)
{
if(j==-1||ss[i]==ss[j])
{
i++;j++;
next[i]=j;
}
else
j=next[j];
}
int jj=len;
int k=0;
while(len>1)
{
len=next[len];
if(len!=0)//不要输出0,还有其他的非最大的数,可以把它去掉以后调试一下
gg[k++]=len;
}
k=k-1;
for(;k>=0;k--)
printf("%d ",gg[k]);
printf("%d",jj);
printf("\n");
memset(ss,'\0',sizeof(ss));
}
return 0;
}