UVALIVE 3026 Period】的更多相关文章

UVAlive 3026 Period 题目: Period   Time Limit: 3000MS   Memory Limit: Unknown   64bit IO Format: %lld & %llu Submit Status Description For each prefix of a given string S with N characters (each character has an ASCII code between 97 and 126, inclusive…
题意:给你一个字符串,问第i位前是否有循环节,若存在,则循环节是多少? 思路:考察失配函数f[i]的意义.只要i%(i-f[i])==0,则循环节长度为i/(i-f[i]).字符在[0,f[i]],[i-f[i],i]范围内的相等,所以如果存在循环节则每i-f[i]可以分为一段.理解起来比较抽象,模拟一遍. #include <cstdio> #include <cstring> #include <cmath> #include <cstdlib> #in…
input n 2<=n<=1000000 长度为n的字符串,只含小写字母 output Test case #cas 长度为i时的最小循环串 循环次数(>1) 若没有则不输出 做法:若next数组是连续的整数且next[i]+1是错位部分长度的倍数,则必有循环出现,且循环次数为(i+1)/错位部分长度 #include <cstdio> #include <queue> #include <cstring> #include <iostream…
kmp的代码很短,但是不太容易理解,还是先说明一下这个算法过程吧. 朴素的字符串匹配大家都懂,但是效率不高,原因在哪里? 匹配过程没有充分利用已经匹配好的模版的信息,比如说, i是文本串当前字符的下标,j是要匹配的模版串当前正在匹配的字符的下标.(下标都从零开始,j同时可以表示已经匹配的字符长度) 当匹配到i = 4, j = 4的时候失配了,朴素的匹配做法是往右边移一位然后从j开始扫,这样做效率很低. 不难发现前面已经匹配好的串ab是最长公共前缀后缀.把串移动到后缀的第一个位置正好是 朴素的匹…
参考:http://www.cnblogs.com/jackge/archive/2013/01/05/2846006.html 总结一下,如果对于next数组中的 i, 符合 i % ( i - next[i] ) == 0 && next[i] != 0 , 则说明字符串循环,而且 循环节长度为:   i - next[i] 循环次数为:       i / ( i - next[i] ) #include <iostream> #include <cstdio>…
UVALive 3026     KMP中next[]数组的应用: 题意:给出一个字符串,问该字符串每个前缀首字母的位置和该前缀的周期. 思路:裸KMP直接上就是了: 设该字符串为str,str字符串的长度为len,next[]的有关前缀的周期的性质: 如果len % (len - next[len]) = 0 (next[len]  != 0)则该字符串有长度为len - next[len] 的循环节(长度最小的重复循环单位),而这个循环节的周期就是len / (len - next[len]…
For each prefix of a given string S with N characters (each character has an ASCII code between 97 and126, inclusive), we want to know whether the prefix is a periodic string. That is, for each i (2 ≤ i ≤ N)we want to know the largest K > 1 (if there…
用KMP里面的next数组即可,原理就是next数组的原理 #include<cstdio> #include<cstdlib> #include<algorithm> #include<cstring> #include<vector> #define MAXN 1000000+10 #define ll long long using namespace std; char s[MAXN]; int n; int nxt[MAXN]; voi…
题意: 给出一个长度不超过1000000的字符串S, 对于该字符串的所有前缀求其周期, 如果周期K >= 2输出起始位置是第几个字符和其周期K 解析: 先求next数组 对于每一个位置如果i % (i-next[i]) == 0 && i /(i - next[i]) >= 2 则成立 即i-next[i] 为其最短循环节 周期为i /(i - next[i]) #include <iostream> #include <cstdio> #include…
题目传送门 题意:(训练指南P213) 求每个前缀的最短循环节 分析:利用失配函数的性质,如果i % (i - fail[i]) == 0,那么正好错位移动一个循环节长度. #include <bits/stdc++.h> using namespace std; const int N = 1e6 + 5; char str[N]; int fail[N]; int len; void get_fail(char *P) { int i = 0, j = -1; fail[i] = j; w…