两道题都是求循环节的...但是一道是学哈希时做的,另一道是学$KMP$时做的 POJ2604 用的哈希...枚举长度的因数作为循环节的长度,然后暴力算出所有循环节位置的哈希值,看看是否相等. #include<cstdio> #include<iostream> #include<algorithm> #include<cstring> #include<cmath> #include<cctype> #include<cstd…
/** 题目:poj1961 Period 链接:http://poj.org/problem?id=1961 题意:求从1到i这个前缀(2<=i<=N) ,如果有循环节(不能自身单独一个),输出前缀字符串长度以及最大的循环周期: 思路: 参考自:http://www.cnblogs.com/chenxiwenruo/p/3546457.html 定理:假设S的长度为len,则S存在最小循环节,循环节的长度L为len-next[len],子串为S[0…len-next[len]-1]. (1)…
题意: 定义a为一个字符串,a*a表示两个字符相连,即 an+1=a*an ,也就是出现循环了.给定一个字符串,若将其表示成an,问n最大为多少? 思路: 如果完全不循环,顶多就是类似于abc1这样,即n=1.但是如果循环出现了,比如abab,那就可以表示成(ab)2.还有一点,就是要使得n尽量大,那么当出现abababab时,应该要这么表示(ab)4,而不是(abab)2. 此题用神奇的KMP解决,也就是主要利用next数组.举例说明. 一般出现循环的都会大概是这样的:abcabcabc.而这…
Description Problem D: Power Strings Given two strings a and b we define a*b to be their concatenation. For example, ifa = "abc" and b = "def" then a*b = "abcdef". If we think of concatenation as multiplication, exponentiatio…
Power Strings Time Limit: 3000MS Memory Limit: 65536K Description Given two strings a and b we define a*b to be their concatenation. For example, if a = "abc" and b = "def" then a*b = "abcdef". If we think of concatenation as…
Period Time Limit: 3000MS Memory Limit: 30000K Description For each prefix of a given string S with N characters (each character has an ASCII code between 97 and 126, inclusive), we want to know whether the prefix is a periodic string. That is, for e…
Power Strings Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 31111   Accepted: 12982 Description Given two strings a and b we define a*b to be their concatenation. For example, if a = "abc" and b = "def" then a*b = "…
鲜有的在luogu发题解以及使用LaTex??? 就丢链接跑了.…
https://www.cnblogs.com/widsom/p/8058358.htm (详细解释) //#include<bits/stdc++.h> #include<vector> #include<cstdio> #include<cstring> #include<iostream> #define ull unsigned long long using namespace std; ; char a[maxn]; ull b[ma…
题目链接:http://poj.org/problem?id=2406 题意:确定字符串最多是多少个相同的字串重复连接而成的 思路:关键是找到字符串的最小循环节 code: #include <cstdio> #include <cstring> ; char s[MAXN]; int next[MAXN]; void GetNext() { int len = strlen(s); ; ; next[] = -; while (i < len) { == j || s[i]…
题目链接:https://vjudge.net/problem/POJ-2406 Power Strings Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 52631   Accepted: 21921 Description Given two strings a and b we define a*b to be their concatenation. For example, if a = "abc"…
Power Strings Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 36926   Accepted: 15254 Description Given two strings a and b we define a*b to be their concatenation. For example, if a = "abc" and b = "def" then a*b = "…
终于靠着理解写出KMP了,两种KMP要代码中这种才能求循环节.i-next[i]就是循环节. #include<cstdio> #define N 1000005 char s[N]; int next[N]; void solve(){ int ans; int k=-1,i=0; next[i]=k; while(s[i]){ if(k==-1||s[i]==s[k]){//记住两个都是等于 i++; k++; next[i]=k; }else k=next[k]; } ans=i-nex…
题目链接:http://poj.org/problem?id=2406 题目大意:如果n%(n-next[n])==0,则存在重复连续子串,长度为n-next[n]. 例如:      a    b    a    b    a    b next:-1   0    0    1    2    3    4 next[n]==4,代表着,前缀abab与后缀abab相等的最长长度,这说明,ab这两个字母为一个循环节,长度=n-next[n]; #include <iostream> #inc…
                                                                                                  Power Strings Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 38038   Accepted: 15740 Description Given two strings a and b we define a*b t…
Power Strings Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 33178   Accepted: 13792 Description Given two strings a and b we define a*b to be their concatenation. For example, if a = "abc" and b = "def" then a*b = "…
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3746 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Problem Description CC always becomes very depressed at the end of this month, he has checked his credit card yesterd…
题目链接:https://vjudge.net/problem/HDU-1358 Period Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 9652    Accepted Submission(s): 4633 Problem Description For each prefix of a given string S with…
B. Power Strings Time Limit: 3000ms Memory Limit: 65536KB 64-bit integer IO format: %lld      Java class name: Main   Given two strings a and b we define a*b to be their concatenation. For example, if a = "abc" and b = "def" then a*b =…
题目大意 给定一个长度为n的字符串,求它的每个前缀的最短循环节 题解 白书例题~~~ "错位部分"长度为i-f[i], 如果这个前i个字符能够组成一个周期串,那么"错位"部分刚好是一个循环节i-f[i]就是循环节长度~~~~ 代码: #include <iostream> #include <cstdio> #include <cstring> #include <algorithm> using namespace…
证明1: 对于一个字符串S,长度为L,如果由长度为len的字符串s(字符串s的最小循环节是其本身)循环k次构成,那么字符串s就是字符串S的最小循环节 那么字符串有个很重要的性质和KMP挂钩,即  i - next[i] 为字符串s的长度 i%(i - next[i]) ==0 证明:字符串S由s循环k次构成,那么有S[0-->L-len-1] == S[len-->L-1],即前k-1个循环节和后k-1个循环节构成的字符串相等 那么此时KMP数组的next[L] = k-1个循环节的长度, 也…
hdu3746 Cyclic Nacklace Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 2262    Accepted Submission(s): 1005 Problem Description CC always becomes very depressed at the end of this month, he ha…
题意 有一个字符串A,一次次的重写A,会得到一个新的字符串AAAAAAAA.....,现在将这个字符串从中切去一部分得到一个字符串B.例如有一个字符串A="abcdefg".,复制几次之后得到abcdefgabcdefgabcdefgabcdefg....,现在切去中间红色的部分,得到字符串B,现在只给出字符串B,求出字符串A的最小长度. 思路 容易发现这个"循环节"是可以"旋转"的.即上述的"abcdefg"也可以是&quo…
/** 题目:F. String Compression 链接:http://codeforces.com/problemset/problem/825/F 题意:压缩字符串后求最小长度. 思路: dp[i]表示前i个字符需要的最小次数. dp[i] = min(dp[j]+w(j+1,i)); (0<=j<i); [j+1,i]如果存在循环节(自身不算),那么取最小的循环节x.w = digit((i-j)/x)+x; 否则w = i-j+1; 求一个区间最小循环节: 证明:http://w…
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1358 题目描述: 给出一个字符串S,输出S的前缀能表达成Ak的所有情况,每种情况输出前缀的结束位置和k. 解题思路: 打表算出next数组,然后对位置i求循环节,如果满足 i % (i - Next[i]) == 0 && Next[i] != 0,所对应的循环节(i - Next[i]), 循环次数是i / (i - Next[i]) #include<cstdio> #inc…
利用KMP的next数组的性质,我们可以找到next数组的循环节. 先说结论: 设字符串长n,则若其  i % ( i – next[n] ) == 0 ,则其有循环节(循环节数目大于1),其循环节数目为 i / ( i – next[n] ) 这里的next数组存储的是匹配到i匹配不成立时,下一个要匹配的位置.即next数组记录的是下一次匹配的位置,而不是下一次匹配的偏移量,若是记录的偏移量,只需修改一下公式即可. 言归正传,我们先证明第一个结论,根据下图来进行说明: 若结论中的关系成立(整除…
https://vjudge.net/problem/UVA-1328 题意 求每个前缀的最小循环节,要求至少循环两次且为完整的. 分析 求next数组,i-next[i]即为前缀i的最小循环节,再判断一下限制条件即可. #include <iostream> #include <cstdio> #include <cstdlib> #include <cstring> #include <string> #include <algorit…
Problem - 3374   KMP求循环节. http://www.cnblogs.com/wuyiqi/archive/2012/01/06/2314078.html   循环节推导的证明相当的好,这题是很裸的套算法的题. 代码如下: #include <cstdio> #include <cstring> #include <iostream> #include <algorithm> using namespace std; ; char buf…
http://poj.org/problem?id=2406 Power Strings Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 27003   Accepted: 11311 Description Given two strings a and b we define a*b to be their concatenation. For example, if a = "abc" and b = &q…
Power Strings Problem's Link: http://poj.org/problem?id=2406 Mean: 给你一个字符串,让你求这个字符串最多能够被表示成最小循环节重复多少次得到. analyse: KMP之next数组的运用.裸的求最小循环节. Time complexity: O(N) Source code:  ;;      ;);      ) ;}/* */…