loj10036. 「一本通 2.1 练习 2」Seek the Name, Seek the Fame

思路:
  nxt数组依次输出即可。

#include<cstdio>
#include<iostream>
#include<cstring>
using namespace std;
const int maxn = 400010;
int n, m;
char A[maxn];
int nxt[maxn];
void findnxt() {
    int j = 0;
    for(int i = 1; i < n; ++i) {
        while(j > 0 && A[i + 1] != A[j + 1])    j = nxt[j];
        if(A[i + 1] == A[j + 1])    ++j;
        nxt[i + 1] = j;
    }
}
void show(int x){
    if(nxt[x]){
        show(nxt[x]);
        printf("%d ", nxt[x]);
    }
}
int main(void) {
    while(~scanf("%s", A + 1)) {
        memset(nxt, 0, sizeof(nxt));
        n = strlen(A + 1);
        findnxt();
        show(n);
        printf("%d\n", n);
    }
}

猜你喜欢

转载自www.cnblogs.com/junk-yao-blog/p/9509902.html