poj1961Period【kmp next数组】
2015-08-26 20:21
381 查看
大意:跟poj2406一样的题 思路见/article/6635868.html
代码:
View Code
代码:
#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
相关文章推荐
- poj1990MooFest(树状数组)
- 自定义ArrayWritable
- hdu5418 BestCoder Round #52 (div.2) Victor and World ( floyd+状压dp)
- C语言(7)--高精度加法、减法、乘法、今天是星期几、四位平方数、候选人选票问题
- iframe请求失效重新登录后直接跳转到请求内容页面的解决方案
- Java心得21
- What day is that day?(快速幂,打表找周期,或者求通项公式)
- GridView学习笔记
- 最小费用最大流
- Masonry介绍与使用实践:快速上手Autolayout
- C++强制类型转换操作符 dynamic_cast
- 几行和一千年
- LeetCode题解:Count Primes
- iOS 开发笔记-UILable/UIFont/UIButton常见设置
- android 飞行模式分析
- UIWindow
- test 2.2-2
- 动态规划之最大子序列和
- 实践出真知
- spring requestMapping学习及测试