题目链接:http://acm.uestc.edu.cn/#/problem/show/1065

题意: 中文题目

解法:理解KMP里面的next之后,就是水题了。

//UESTC 1065

#include <bits/stdc++.h>
using namespace std;

const int maxn = 3e6+10;
char s[maxn];
int fail[maxn];

int main()
{
    while(scanf("%s", s+1) != EOF)
    {
        memset(fail, 0, sizeof(fail));
        int j = 0;
        int len = strlen(s+1);
        for(int i = 2; i <= len; i++){
            while( j > 0 && s[j+1] != s[i]) j = fail[j];
            if(s[j+1] == s[i]) j++;
            fail[i] = j;
        }
        for(int i = 1; i <= len; i++){
            if(i%(i-fail[i])){
                if(i<len) printf("%d ", i);
                else printf("%d\n", i);
            }
            else{
                if(i<len) printf("%d ", (i-fail[i]));
                else printf("%d\n", (i-fail[i]));
            }
        }
        int tmp;
        if(len%(len-fail[len])) tmp = 1;
        else tmp = (len-fail[len]);
        for(int i = 1; i <= tmp; i++) printf("%c", s[i]);
        printf("\n");
    }
    return 0;
}