传送门:点我
Input
Output
Sample Input
Sample Output
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.
For each test case, output a single line with integer numbers in increasing order, denoting the possible length of the new baby's name.
ababcababababcabab aaaaa
2 4 9 18 1 2 3 4 5
题意:给定一个字符串s,从小到大输出s中既是前缀又是后缀的子串的长度。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<stack>
using namespace std;
#define N 400006
char s[N];
int Next[N];
void make_Next(int n)
{
int i,j;
for(i=1,j=0; i<n; i++)
{
while(j>0&&s[i]!=s[j])
j=Next[j-1];
if(s[i]==s[j])
j++;
Next[i]=j;
}
}
int main()
{
int t,j;
cin>>t;
while(~scanf("%s",s))
{
int len=strlen(s);
Next[0]=0;
make_Next(len);
stack<int> sta;
j=len-1;
while(Next[j]!=0)
{
sta.push(Next[j]);
j=Next[j-1];
}
while(!sta.empty())
{
printf("%d ",sta.top());
sta.pop();
}
printf("%d\n",len);
}
return 0;
}