POJ_1961 KMP next的典型应用 类似于 poj2406

POJ_1961 KMP next的典型应用 类似于 poj2406
/* 
 * POJ_1961 KMP next的典型应用 类似于 poj2406
 * Author : a_clay  2014/05/06 
 */  
#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <algorithm>
#include <cmath>
#define Bug cout << "here\n";

using namespace std;
const int M = 1000005;

char t[M];
int next[M];

void get_next(int len) {
    int i, j;
    i = 0, j = -1;
    next[0] = -1;
    while (i < len) {
        if (j == -1 || t[i] == t[j]) {
            i++, j++, next[i] = j;
        }
        else {
            j = next[j];
        }
    }
}
int main() {
    int n, ca = 1;
    while (scanf("%d", &n) && n != 0) {
        scanf("%s", t);
        get_next(n);
        printf("Test case #%d\n", ca++);
        for (int i = 2; i <= n; i++) {
            if (i% (i - next[i]) == 0 && i / (i - next[i]) > 1) {
                printf("%d %d\n", i, i / (i - next[i]));
            }
        }
        printf("\n");
    }
    return 0;
}

posted @ 2014-05-07 20:27  小尼人00  阅读(103)  评论(0编辑  收藏  举报