UVA 1328 - Period (KMP)
2017-05-21 11:13
369 查看
UVA 1328 - Period
题意:给定一个字符串,求出有几个位置的前缀串是由个数大于1的串循环得到的。求出位置和循环次数
思路:利用kmp的next数组的性质,i - next[i]就是循环长度,然后推断一下是不是正好是倍数就可以
代码:
#include <cstdio> #include <cstring> const int N = 1000005; int n, next ; char str ; int main() { int cas = 0; while (~scanf("%d", &n) && n) { scanf("%s", str + 1); next[1] = 0; int j = 0; for (int i = 2; i <= n; i++) { while (j && str[i] != str[j + 1]) j = next[j]; if (str[i] == str[j + 1]) j++; next[i] = j; } printf("Test case #%d\n", ++cas); for (int i = 1; i <= n; i++) { if (!next[i]) continue; if (i % (i - next[i])) continue; printf("%d %d\n", i, i / (i - next[i])); } printf("\n"); } return 0; }
相关文章推荐
- uva 1328 - Period (周期串的判断 kmp)
- UVA 1328 Period(KMP)
- UVA 1328 - Period KMP
- UVA 1328 Period(KMP:最短循环节)
- UVa 1328 (KMP求字符串周期) Period
- KMP UVA1328 Period
- UVa 1328 Period (KMP&失配函数的理解)
- uva 1328 Period(KMP)
- KMP【UVA1328】 Period
- UVA 1328 - Period (KMP)
- UVA1328 Period【KMP/周期串/循环节】By cellur925
- uva 1328 Period(KMP循环节)
- UVA-1328 - Period(KMP)
- UVALIVE 3026 Period(kmp)
- KMP求前缀周期uva1328
- UVa 1328 Period
- UVa 1328 Period
- Uvalive - 3026 Period (kmp求字符串的最小循环节+最大重复次数)
- UVALive 3026 (LA 3026) Period KMP求字符串周期
- UVA - 1328 Period KMP算法