hdu3746 KMP】的更多相关文章

这题琢磨了挺长的时间.需要理解next[]表示了什么; next[i]代表了前缀和后缀的最大匹配的值,也就是个数. len-next[len]表示循环节的长度; 比如abcab   int fl=len-next[len]=3;循环节长度为3,即cab.然后int len=strlen(s)=5: 如果len%fl==0,那就count=len/fl,不然count=len/fl+1;count表示根据长度能够循环的次数.count*fl表示能够满足循环的时候的长度, count*fl-len就…
题意:给你一个字符串,问你至少增添几个字符可以把这个字符串变成一个循环字符串(ababa的循环节是ab,不是aba): 解题思路:利用kmp中的next数组,首先在这样求next的数组的代码里: void get_next() { int j,k; j=0;k=-1;next1[0]=-1; while(j<tlen) { if(k==-1||t[j]==t[k]) next1[++j]=++k; else k=next1[k]; } } next1[tlen]=我们输入的完整字符串的相同前后缀…
题意:给出一段字符串  求最少在最右边补上多少个字符使得形成循环串(单个字符不是循环串) 自己乱搞居然搞出来了... 想法是:  如果nex[len]为0  那么答案显然是补len 否则  答案为循环节的长度-nex[len] 小于0变0 写了一个假算法居然能过    ... ababca就错了 kmp重要性质: 重要的性质len-next[i]为此字符串的最小循环节(i为字符串的结尾),另外如果len%(len-next[i])==0,此字符串的最小周期就为len/(len-next[i]);…
题意:       给你一个项链,问你最少加多少个珠子能满足整个项链是一个循环的项链(首尾相连) 思路:      KMP的简单应用只要了解next数组的意义就好说了,下面总结下  next在循环方面的常用应用 (1)i - next[i] 最小循环节(第一个字母开始) (2)next[i] 最大循环节中的第几位数(此时循环节可交叉) (3)next[i] != 0 && i % (i - next[i]) == 0,当前是循环节中的最   后一位. (4)在(3)的前提下 i / (i…
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 last days. Being inspired by the en…
题目链接:https://cn.vjudge.net/problem/HDU-3746 题意 给一串珠子,我们可以在珠子的最右端或最左端加一些珠子 问做一条包含循环珠子的项链,最少还需要多少珠子 思路 KMP的另一个用法,求最小循环节minloop=len-fail[len] 用我的观点来看KMP的fail数组,就是值域和定义域都是串的长度,返回值是这个串能够匹配后缀的最大前缀串长度 但是纯循环节构成的串中,这个返回值不包括第一个循环节 比如aabaabaab fail[9]==6 fail[6…
题意 给你个字符串,问在字符串末尾还要添加几个字符,使得字符串循环2次以上. 解法 无论这个串是不是循环串 i-next[i] 都能求出它的最小循环节 代码: /* 思路:kmp+字符串的最小循环节问题 分析: 1 题目要求的是给定一个字符串,问我们还需要添加几个字符可以构成一个由n个循环节组成的字符串. 2 可知我们应该先求出字符串的最小循环节的长度:假设字符串的长度为len,那么最小的循环节就是cir = len-next[len] ; 如果有len%cir == 0,那么这个字符串就是已经…
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 last days. Being inspired by the en…
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 last days. Being inspired by the en…
题目链接: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…