您的位置:首页 > 其它

poj1961Period【kmp next数组】

2015-08-26 20:21 381 查看
大意:跟poj2406一样的题 思路见/article/6635868.html

代码:

#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;

const int maxn = 1000005;

int next[maxn];

void get(char *s) {
int l = strlen(s);
int j = 0, k = -1;
next[0] = -1;
while(j < l) {
if(k == -1 || s[j] == s[k]) {

//            if(s[++j] == s[++k]) {
//                next[j] = next[k];
//            } else {
//                next[j] = k;
//            }
next[++j] = ++k;
} else {
k = next[k];
}
}
}
char s[maxn];

int main() {
int n;
int kase = 1;
while(scanf("%d",&n) && n) {
scanf("%s",s);
printf("Test case #%d\n", kase++);
get(s);
int l = strlen(s);
//        for(int i = 0; i <= l; i++) {
//            printf("%d ", next[i]);
//        }puts("");
for(int i = 2; i <= l; i++) {
int ans = 1;
if(i % (i - next[i]) == 0) {
ans = i / (i - next[i]);
}
if(ans > 1) {
printf("%d %d\n", i, ans);
}
}
puts("");
}
}


View Code
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: