poj2752—Seek the Name, Seek the Fame

来源:互联网 发布:ipo 净利润 数据 编辑:程序博客网 时间:2024/06/03 17:37

传送门:点我

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
ababcababababcababaaaaa
Sample Output
2 4 9 181 2 3 4 5

题意:给定一个字符串s,从小到大输出s中既是前缀又是后缀的子串的长度。

贴代码:

#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>#include<stack>using namespace std;#define N 400006char 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;}


原创粉丝点击