字符串-前缀中的周期(数据结构基础 第4周)
来源:互联网 发布:如何还房贷最划算知乎 编辑:程序博客网 时间:2024/04/27 21:15
问题描述
分析
将KMP中next数组的值往前移动一位,规律就出来了。
比如
判断是否为周期的条件:i%(i-next[i-1])==0 && i/(i-next[i-1]) > 1; (i为第i个字符,i-1为其next数组下标)
参考:http://blog.csdn.net/u013487630/article/details/18989437
源码
#include <iostream>#include <string>#include <cstring>#include <vector>using namespace std;int main() { int n; int cases=0; string P; while(cin>>n && n!=0) { vector<int> next(n, 0); cin>>P; next[0]=0; int j=0; for(int i=1; i<n; i++) { int j=next[i-1]; //j表示当前匹配了多少位 while(j && P[j]!=P[i]) j=next[j-1]; if (P[j]==P[i]) { next[i]=j+1; } else { next[i]=0; } } cout << "Test case #" << ++cases << endl; for(int i=2; i<=n; i++) { if (i%(i-next[i-1])==0 && i/(i-next[i-1]) > 1) { cout << i << " " << i/(i-next[i-1]) << endl; } } cout << endl; } return 0;}
0 0
- 字符串-前缀中的周期(数据结构基础 第4周)
- 【数据结构】第3周 字符串 4:前缀中的周期
- 4:前缀中的周期
- 3-4:前缀中的周期
- 字符串-英语数字转换器(数据结构基础 第4周)
- 字符串-KMP算法实现(数据结构基础 第4周)
- 字符串-拼写检查(数据结构基础 第4周)
- 前缀中的周期
- poj 前缀中的周期
- 【Openjudge】前缀中的周期
- KMP与周期字符串前缀
- 字符串-字符串类的实现(数据结构基础 第4周)
- pku openjudge 百炼 前缀中的周期
- 【数据结构】第4周 二叉树基础
- 前缀中的周期---非优化KMP算法的运用
- POJ2406 KMP前缀周期
- 第8周SHH数据结构-【项目4-字符串加密 】
- 线性表-约瑟夫问题(数据结构基础 第2周)
- 易經大意(凸紫色) p401-478
- jzoj3442&3816 期望异或&&大新闻
- 性能调优(转载)
- Android MessageQueue与Message详解
- Happy Number
- 字符串-前缀中的周期(数据结构基础 第4周)
- 人工神经网络中的activation function的作用具体是什么?为什么ReLu要好过于tanh和sigmoid function?
- [Cloud Computing]Mechanisms: Certificate Validation Service
- Context的使用
- Qt OpenGL教程 (非常详细)
- Error running app: Instant Run requires 'Tools | Android | Enable ADB integration' to be enabled.
- 深度理解Android InstantRun原理以及源码分析
- CodeForces 687B - Remainders Game(中国剩余定理)
- 116. N-Queens