#POJ 2752 Seek the Name, Seek the Fame (KMP)

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

 Title effect: an input string, the outputs of all the prefix length equal to the suffix.

Ideas: First, KMP role in the next array that represents the prefix and suffix largest public length, but this question is not the same, he may be output equal to the length of all prefixes suffixes, rather than the maximum length, when I just started to see that ideas are not, then his next try out an array of table columns, we have found:

For example, I entered "ababcababababcabab", corresponding is "001201234343456789" look at what the answer is not all the answers are contained within it? Think again next array of role, i denotes until i, the maximum length of the common prefix and suffix, if the next one is the last array of 0, indicating that the whole of prefixes and suffixes are not the same situation, so his length on direct output good; represents a total length len, len] [if next! = 0, then, it means that from 0 to the next cross string [len] and the len - len [] next to the cross-sectional llen are the same, but smaller than his == suffix prefix length, can only be [len] in front of the next, then the next length? [Len] is next! Because at this time before the next string [len] [next item after item len] are equal, so you will see the previous section and other parts of the back and then again, this is a recursive process, just like a person to cut a rope, like half a day, the first half and second half are equal, so after half can not read until he can no longer be cut, it should be understood under the ^ _ ^

AC Code:

#include<iostream>
#include<cstring>
using namespace std;
const int maxn = 4e5 + 5;

string line;
int nxt[maxn], pre[maxn];
void getnext() {
    nxt[0] = -1;
    int i = 0, j = -1;
    while (i < line.size()) {
        if (j == -1 || line[i] == line[j])
            nxt[++i] = ++j;
        else
            j = nxt[j];
    }
}

int main()
{
    while (cin >> line) {
        memset(nxt, 0, sizeof(nxt));
        memset(pre, 0, sizeof(pre));
        getnext();
        int len_ = line.size();
        int ans = nxt[len_], X = 0;len_;
        if (!ans) {cout << len_ << endl; continue;}  // 自己就是循环体的情况
        pre[X++] = len_, pre[X++] = ans;  //把自己先存进去
        while (nxt[ans] != 0) {  //当不能再被截
            pre[X++] = nxt[ans];
            ans = nxt[ans];
        }
        for (int i = X - 1; i >= 0; i--) cout << pre[i] << " ";
        cout << endl;
    }
    return 0;
}

 

 

Guess you like

Origin blog.csdn.net/weixin_43851525/article/details/91903191