您的位置:首页 > 其它

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

2014-05-07 20:27 453 查看
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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: