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

来源:互联网 发布:电脑清理软件 知乎 编辑:程序博客网 时间:2024/05/22 14:21
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;}

0 0