Period UVALive
来源:互联网 发布:钱咖淘宝返利是真的吗 编辑:程序博客网 时间:2024/06/08 21:21
Period(传送门)
题意
给定字符串,找到每个前缀的最大循环节的个数,即循环周期,如果大于1
的话就输出下标和周期数(循环节)
解题思路
KMP
就可以非常轻松的解决问题,首先提到一个next
数组的性质,对于当前的前缀a[j]
,如果j % (j - next[j]) == 0
则j - next[j]
即为最小周期长度,而周期数就是j / (j - next[i])
,至于为什么j - next[j]
为最小周期长度呢,请听我慢慢徐来,先看下面的示意图
通过看这幅图大家或许也能看懂,我就稍微讲述一下,我们当前失配了j
点,然后我们需要调到next[j]
,那我们知道1子串
和2子串
必须是相等的,就是公共后缀子串。
所以从j
开始往0
走的方向上,第一个长度为k
和第二个长度为k
的字符串是相等的。又因为w == w
(因为1==2
并且长度为k
的后缀字符串也是相等的),所以w字符串
的后缀长度为k
的字符串等于以j
和next[j]
为结尾长度为k
的后缀字符串。
以此类推,只要我们的j % (j - next[j])
为0
就可以保证从j
往前走有j / (j - next[j])
个长度为j - next[j]
的字符串相等。
如此,证明完毕,如果不懂滴,可以留言。
代码
#include <iostream>#include <fstream>#include <string>#include <map>#include <set>#include <algorithm>using namespace std;typedef long long LL;constexpr int MAXN = 2e6 + 5;constexpr int MAXM = 4e4 + 5;constexpr int mod = 20071027;string b;int n,nexts[MAXN];void getNext(string a) { int k = nexts[0] = -1; int j = 0; while(j < a.length()) { if(k == -1 || a[j] == a[k]) { j ++; k ++; nexts[j] = k; } else { k = nexts[k]; } }}int main() { ios::sync_with_stdio(false); //ifstream in("input.txt"); //cin.rdbuf(in.rdbuf()); int cas = 1; while(cin >> n && n) { cin >> b; getNext(b); cout << "Test case #" << cas ++ << endl; for(int i = 0; i <= n; i ++) { if(i && i % (i - nexts[i]) == 0 && i / (i - nexts[i]) > 1) { cout << i << " " << i / (i - nexts[i]) << endl; } } cout << endl; } return 0;}
1 0
- Period UVALive
- Period UVALive
- UVALive - 3026 Period
- Period +uvalive+next数组
- UVAlive 3026 Period [KMP]
- UVALIVE 3026 Period(kmp)
- UVALive - 3026 - Period (KMP)
- UVALive 3608 Period(二分答案+DP)
- uvalive 3026 Period KMP(失配函数)
- UVALive 3026 Period(KMP裸)
- 【暑假】[实用数据结构]UVAlive 3026 Period
- uvaLive 3026 - Period 水KMP循环结
- UVALive 3608 Period(DP+二分)
- UVALive 3026 Period ( kmp 求前缀最小循环节)
- UVALive 3026 (LA 3026) Period KMP求字符串周期
- uvalive 3026 Period (前缀最短循环节)
- UVALive 3026 period(周期) kmp算法的应用
- Period
- CSS入门笔记
- 2.8 CSS相对定位&&绝对定位&&固定定位&&z-index
- 1060. 爱丁顿数
- 装饰器 Decorators
- Python学习之字典的基本操作及字典中的函数
- Period UVALive
- 目标识别(object detection)中的 IoU(Intersection over Union)
- 设计模式
- JS中的递归
- REPL 与Java
- 怎么不发送请求,就能使用缓存
- Prototype模式
- yuv转成matlab可读的rgb格式
- 事件冒泡的原理及如何禁止