Cyclic Nacklace HDU 3746 KMP 循环节 题意 给你一个字符串,然后在字符串的末尾添加最少的字符,使这个字符串经过首尾链接后是一个由循环节构成的环. 解题思路 next[len]-len的差即是循环部分的长度. 这个是重点.这个题目自己开始没有想明白,看的博客,推荐这个. 代码实现 #include<cstdio> #include<cstring> #include<algorithm> using namespace std; const i…
Cyclic Nacklace Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 15406    Accepted Submission(s): 6419 Problem Description CC always becomes very depressed at the end of this month, he has checke…
http://acm.hdu.edu.cn/showproblem.php?pid=3746 Cyclic Nacklace Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 4498    Accepted Submission(s): 2051 Problem Description CC always becomes very dep…
题目大意 给出字符串,寻找最小要补全的字符个数,使得字符串是两次的循环 解法 通过寻找规律,我们又发现了len-next[len]又派上了用场 ①如果next[len]是0,说明最大前缀后缀和为0,那么这个串里面没有什么重复的那种部分,也就是输出len例如abcde ②如果len%(len-next[len])==0也就是说里面已经具有这种重复两次的串了,所以输出0,例如aaa ③如果上述情况都不是,那么说明这个串里面包含重复部分但是不全.那么输出的公式是(len-next[len[)-len%…
CC这个月底总是很郁闷,昨天他查了他的信用卡,没有任何意外,只剩下99.9元了.他很苦恼,想着如何度过这最后的几天.受"HDU CakeMan"企业家精神的启发,他想卖一些小东西来赚钱.当然,这并非易事.圣诞节就要到了,男孩们都忙着挑选圣诞礼物送给女朋友.人们认为,链手镯是一个很好的选择.然而,事情并不总是那么简单,众所周知,女孩喜欢用五颜六色的装饰来使手镯显得生动活泼,同时她们也想展示自己作为大学生成熟的一面.CC了解了女孩们的需求后,打算出售这款名为CharmBracelet的ch…
题目大意:给你一些串,问如果想让这个串里面的循环节至少循环两次,需要添加几个字符(只能在最前面或者最后面添加).比如ababc 需要添加5个就是添加ababc. 分析:其实字符串的长度len-next[len] = 最小循环节长度,为什么?其实也是需要对next的深刻了解,首先我们都知道next是求的最大的前缀和后缀的匹配度比如下面的字符串 ABCABCABC,很直接的就可以看出来最大的匹配串是ABCABC,但怎么从这个看出来最小的循环节呢?我们看到这个串的后缀串是从第4个字符开始的,也就是说从…
题意: 给出一个字符串,要求在后面添加最少的字符是的新串是循环的,且至少有两个循环节.输出最少需要添加字符的个数. 分析: 假设所给字符串为p[0...l-1],其长度为l 有这样一个结论: 这个串的最小循环节为 l - next[l] 感觉自己想得不是特别透彻,所以把别人的博客贴上来吧. 里面有个小错误就是:next[i]的值应该为j-k 对于这种情况还可以这样想: |←②→|←④→| ------------------------ ------------------------ |←①→…
题目大意: 求最少在结尾补上几个字符才能形成循环 基本思路: next数组有一个性质,长度为len的字符串的最小长度的循环节(可能没有,但有的话一定是)len-next[len],因为最长不能是原串,所以最长为next[len],所以最短的,也就是最基本的循环节就是len-next[[len]; 对于这个题,当next[len]不是0(即len=循环节长度tmp),而且长度len%tmp=0时,结果是0:否则结果为tmp-len%tmp,其中len%tmp表示出去多少个循环后还剩几个字符: 代码…
题目链接:https://cn.vjudge.net/problem/HDU-3746 题意 给一串珠子,我们可以在珠子的最右端或最左端加一些珠子 问做一条包含循环珠子的项链,最少还需要多少珠子 思路 KMP的另一个用法,求最小循环节minloop=len-fail[len] 用我的观点来看KMP的fail数组,就是值域和定义域都是串的长度,返回值是这个串能够匹配后缀的最大前缀串长度 但是纯循环节构成的串中,这个返回值不包括第一个循环节 比如aabaabaab fail[9]==6 fail[6…
题意就是将所给的字符串变成多个完整的循环(至少两个),然后给出最少需要添加的字符数.…
分析转自:http://972169909-qq-com.iteye.com/blog/1114968 十分易懂 题意:求字串中[前缀+跟前缀相同的子串]的个数? Sample Input 1 4 abab Sample Output 6 abab:包括2个a,2个ab,1个aba,1个abab 这里要用到next值的意义: next[i]表示前i个字符所组成的字符串的最大前后缀匹配长度 举个例子: next[5]=2, 表示下标5前面那个字符串abcab的前后缀匹配的最大长度是2,显然就是ab…
Cyclic Nacklace Problem Description CC always becomes very depressed at the end of this month, he has checked his credit card yesterday, without any surprise, there are only 99.9 yuan left. he is too distressed and thinking about how to tide over the…
串珠子 题目大意:给定一个字串,要你找到如果要使之成为循环串,在末尾需要的最小的字数(只能添加字符,不能删减字符) 首先联动一下之前做过的动态规划问题POJ 3280,当然了3280这一题是用的LD,因为他可以添加或者删除(加上修改也行,但是要改状态方程了) 而我们现在要讨论的这一题(HDU 3746)是有限制的,首先它只能添加不能删除,其次一定是要在末尾添加. 其实这样的题用kmp可以很快地解决,其实这样的题目是循环节的题目,之前我已经写过很多了,这一题是对循环节的深刻理解. 首先我们我们明白…
Cyclic Nacklace Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 14416    Accepted Submission(s): 6016 Problem Description CC always becomes very depressed at the end of this month, he has checke…
Cyclic Nacklace Problem's Link: http://acm.hdu.edu.cn/showproblem.php?pid=3746 Mean: 给你一个字符串,让你在后面加尽量少的字符,使得这个字符串成为一个重复串. 例: abca---添加bc,成为abcabc abcd---添加abcd,成为abcdabcd aa---无需添加 analyse: 经典的求最小循环节. 首先给出结论:一个字符串的最小循环节为:len-next[len]. 证明: 举个例子:abcab…
题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=3746 KMP算法—— AC代码: #include <iostream> #include <cstdio> #include <cstdlib> #include <cmath> #include <cstring> #include <string> #include <vector> #include <list&…
Cyclic Nacklace Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 13790    Accepted Submission(s): 5738 Problem Description CC always becomes very depressed at the end of this month, he has check…
Cyclic Nacklace Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 11516    Accepted Submission(s): 4913 Problem Description CC always becomes very depressed at the end of this month, he has checke…
题目链接:HDU 3746 Sample Input 3 aaa abca abcde Sample Output 0 2 5 Author possessor WC Source HDU 3rd "Vegetable-Birds Cup" Programming Open Contest Solution 题意 给定一个字符串,问至少需要在末尾添加多少个字符使得字符串循环. 思路 KMP 设前缀函数为 \(\pi()\),字符串长度为 \(n\),下标从 \(1\) 开始. 最小循环…
Cyclic Nacklace 题意:给一个长度为Len( 3 <= Len <= 100000 )的英文串,问你在字符串后面最少添加几个字符可以使得添加后的串为周期串? Sample Input 3 aaa abca abcde   Sample Output 0 2 5   思路:要是写过period再来写这道题会发现很简单:就是在getfail()得到f[]之后,贪心地看最后一个字符所能用到的最长后缀长度为多少?而这个后缀长度就是n - n%len;(n为字符串长度,len为循环节的长度…
Cyclic Nacklace Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 4212    Accepted Submission(s): 1913 Problem Description CC always becomes very depressed at the end of this month, he has checked…
Cyclic Nacklace Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 5663    Accepted Submission(s): 2557 Problem Description CC always becomes very depressed at the end of this month, he has checke…
Cyclic Nacklace Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2716    Accepted Submission(s): 1244 Problem Description CC always becomes very depressed at the end of this month, he has checked…
Cyclic Nacklace Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 2538    Accepted Submission(s): 1154 Problem Description CC always becomes very depressed at the end of this month, he has checke…
Cyclic Nacklace Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 4420    Accepted Submission(s): 2011 Problem Description CC always becomes very depressed at the end of this month, he has checked…
Cyclic Nacklace Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 16307    Accepted Submission(s): 6753 Problem Description CC always becomes very depressed at the end of this month, he has checke…
题目链接:https://vjudge.net/problem/HDU-3746 Cyclic Nacklace Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 10985    Accepted Submission(s): 4692 Problem Description CC always becomes very depresse…
Period Problem 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 each i (2 <= i <= N) we want to…
http://acm.hdu.edu.cn/showproblem.php?pid=3746 kmp的Nxet数组求字符串循环节例题 lenB%(lenB-Next[lenB])==0则其有周期lenB/(lenB-Next[lenB]),其中最小循环节长度是lenB-Next[lenB] #include <iostream> #include <cstdio> #include <cstring> #include <algorithm> using n…
Cyclic Nacklace Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1946    Accepted Submission(s): 854 Problem Description CC always becomes very depressed at the end of this month, he has checked…