您的位置:首页 > 其它

UVA 10298 - Power Strings(KMP)

2014-08-02 18:03 393 查看


UVA 10298 - Power Strings

题目链接

题意:本意其实就是,给定一个字符串,求出最小循环节需要几次循环出原字符串

思路:利用KMP中next数组的性质,n - next
就是最小循环节,然后n / 循环节就是答案

代码:

#include <cstdio>
#include <cstring>

const int N = 1000005;
char str
;
int next
;

void getnext() {
int n = strlen(str);
next[0] = next[1] = 0;
int j = 0;
for (int i = 2; i <= n; i++) {
while (j && str[i - 1] != str[j]) j = next[j];
if (str[i - 1] == str[j]) j++;
next[i] = j;
}
printf("%d\n", n / (n - next
));
}

int main() {
while (~scanf("%s", str) && strcmp(str, ".") != 0) {
getnext();
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: