POJ-2752 Seek the Name, Seek the Fame ——-KMP
Seek the Name, Seek the Fame
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 8261 Accepted: 3883
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:)
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
aaaaa
Sample Output

2 4 9 18
1 2 3 4 5

中文
小猫是如此着名,许多夫妇在山上和山谷上奔跑到Byteland,并要求小猫给他们新生的婴儿命名。 他们寻求名字,同时寻求名声。 为了摆脱这样的无聊的工作,创新的小猫可以做出一个简单而又神奇的算法:

步骤1。 把父亲的名字和母亲的名字连接起来,换成一个新的字符串S.
第2步。 找到一个适当的前缀 - 后缀字符串S(它不仅是前缀,也是S的后缀)。

示例:父亲=’ala’,母亲=’la’,我们有S =’ala’+’la’=’alala’。 S的潜在前缀 - 后缀字符串是{‘a’,’ala’,’alala’}。 给定字符串S,你可以帮助小猫编写一个程序来计算S的可能的前缀 - 后缀字符串的长度吗? (他可能会给你的宝宝一个名字感谢你:)
输入

输入包含多个测试用例。 每个测试用例占用包含上述串S的单行。

思路:
本题巧妙利用kmp算法中的next数组 算出后从后往前找
next数组中的就是答案
引用http://www.cnblogs.com/dongsheng/archive/2012/08/13/2636261.html的思路

这里写图片描述
如左图,假设黑色线来代表字符串str,其长度是len,红色线的长度代表next[len],根据next数组定义易得前缀的next[len]长度的子串和后缀next[len]长度的子串完全相同(也就是两条线所对应的位置)。我们再求出next[len]位置处的next值,也就是图中蓝线对应的长度。同样可以得到两个蓝线对应的子串肯定完全相同,又由于第二段蓝线属于左侧红线的后缀,所以又能得到它肯定也是整个字符串的后缀。
所以对于这道题,求出len处的next值,并递归的向下求出所有的next值,得到的就是答案。

#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<stack>
#include<iostream>
using namespace std;
char t[400010];
int len;
int Next[400010];
int ans[500010];
void makeNext(){//与标准kmp一样 从一开始 Next[1]写为0
    Next[1]=0;
    int i=1,j=0;
    while(i<=len){
        if(j==0||t[i]==t[j]){
            i++;j++;
            Next[i]=j;
        }
        else j=Next[j];
    }
}
int main(){
    //freopen("in.txt","r",stdin);
    while(scanf("%s",t+1)!=EOF){
        len=strlen(t+1);
        makeNext();
        int num=0;
        int j=len;
        while(j!=0){//核心:从后往前找
            if(t[j]==t[len])
    //特殊判断 如果t[j]和最后一个相等 它又和前缀字母相等
    //所以最后一个就和前缀相等(这是最关键部分)
                ans[++num]=j;
            j=Next[j];
        }
        for(int i=num;i>=1;i--)//输出
            cout<<ans[i]<<" ";
        cout<<endl;
    }
    return 0;
}