hdu 3336 kmp+next数组应用】的更多相关文章

分析转自: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…
题意:求每一个前缀,跟前缀相同的每个子串. 此题:网上很多都是假程序,不过也AC了,的确我测试几个案例之后的的确确是存在这个问题. 分析:每一个前缀,可以考虑KMP,f失配指针,如何求得它出现了多少次呢? 如果f > 0 ,至少这个前缀是符合的,但是,你会少算一些,例如: 你会少算子串a,怎么弥补回来呢? 继续递归下去(其实不是递归啦),找这个子串,是否还可以找出子串和前缀相同. 完美解决了~~~ #include <bits/stdc++.h> using namespace std;…
直接上传送门好了,我觉得他分析得非常透彻. http://972169909-qq-com.iteye.com/blog/1114968 #include <cstdio> #include <cstring> + ; ; char s[maxn]; int next[maxn], l; void get_next() { , j = ; next[] = -; while(j < l) { || s[k] == s[j]) { k++; j++; next[j] = k;…
Count the string Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 8845    Accepted Submission(s): 4104 Problem Description It is well known that AekdyCoin is good at string problems as well as n…
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…
hdu 3336 题意:输入一个字符串求每个前缀在串中出现的次数和 sol:只要稍微理解下next 数组的含义就知道只要把每个有意义的next值得个数加起来即可 PS:网上有dp解法orz,dp[i]表示以i为前缀串结尾的前缀串的总和,方程很容易写出 ; ; ]=next[]=;         ;i<n;i++)         {             ]=j+;             ]=;         }         ,cnt=;         ;i<n;i++)    …
// hdu 1686 KMP模板 // 没啥好说的,KMP裸题,这里是MP模板 #include <cstdio> #include <iostream> #include <cstring> #include <algorithm> using namespace std; ; ; char T[MAX_N]; char p[MAX_M]; int f[MAX_M]; int n,m; void getfail(){ f[] = f[] = ; ;i&l…
Group Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1959    Accepted Submission(s): 1006 Problem Description There are n men ,every man has an ID(1..n).their ID is unique. Whose ID is i and i…
Rabbit Kingdom Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1999    Accepted Submission(s): 689 Problem Description Long long ago, there was an ancient rabbit kingdom in the forest. Every ra…
Cyclic Nacklace HDU 3746 KMP 循环节 题意 给你一个字符串,然后在字符串的末尾添加最少的字符,使这个字符串经过首尾链接后是一个由循环节构成的环. 解题思路 next[len]-len的差即是循环部分的长度. 这个是重点.这个题目自己开始没有想明白,看的博客,推荐这个. 代码实现 #include<cstdio> #include<cstring> #include<algorithm> using namespace std; const i…