您的位置:首页 > 其它

uva 10298 - Power Strings(KMP)

2014-03-19 21:57 288 查看
题目链接:uva 10298 - Power
Strings 


题目大意:给出一个字符串,求出该字符串是由几个最小循环节组成的。

解题思路:KMP, n-next
即为最小循环节。

#include <stdio.h>
#include <string.h>

const int N = 1000005;

int n, next
;
char s
;

void getNext () {
int p = 0;
n = strlen(s+1);
for (int i = 2; i <= n; i++) {
while (p > 0 && s[p+1] != s[i])
p = next[p];

if (s[p+1] == s[i])
p++;

next[i] = p;
}
}

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