您的位置:首页 > 其它

POJ - 2406 Power Strings(KMP 循环节)

2015-10-26 22:47 351 查看
题目大意:问一个字符串由多少个循环节构成

解题思路:循环节和next的关系

#include <cstdio>
#include <cstring>
const int N = 1000010;
char str
;
int len;
int next
;

void getFail() {
len = strlen(str);
int i = 0, j = -1;
next[0] = -1;
while (i < len) {
if (j == -1 || str[i] == str[j]) {
i++; j++;
next[i] = j;
}
else j = next[j];
}
}

int main() {
while (scanf("%s", str) && str[0] != '.') {
getFail();
if (len % (len - next[len]) == 0)
printf("%d\n", len / (len - next[len]));
else printf("1\n");
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: