KMP算法的实现,基于算法导论32.4节。int* generateNext(string &p){ const int m = p.length(); int *next = new int[m]; int k = -1; next[0] = -1; for (int...
分类:
编程语言 时间:
2014-09-09 11:05:18
阅读次数:
178
#include #include #include void GetNext(int *next, char *str){ int len = strlen(str); int i, k, flag = 1; next[0] = -1; for(i = 2; i < len; i++) //nex...
分类:
其他好文 时间:
2014-09-09 11:02:28
阅读次数:
184
先把2*n个数字接成一个模式串P,复制两次的P为串T,然后在T上进行KMP找对P匹配的多个终点,然后就是用Polya定理了,需要求逆元。 1 #include 2 #include 3 #include 4 #include 5 #define mod 1000000007 6 using ...
分类:
其他好文 时间:
2014-09-07 00:58:34
阅读次数:
192
# include # include # include using namespace std;char a1[1000010],a2[1000010];int next[1000010];int len1,len2,cot;void Getnext(){ int i=0,j=-1; ...
分类:
其他好文 时间:
2014-09-06 21:13:43
阅读次数:
256
KMP算法class KMP{public: vector create_prefix_function(string s) { vector next(s.size(), 0); next[0] = 0; int k = 0; f...
分类:
其他好文 时间:
2014-09-06 17:22:43
阅读次数:
301
链接:http://acm.hdu.edu.cn/showproblem.php?pid=3746
题意:在一个字符串后最少加几个字符才能使这个字符串是某个串重复两次而得。
思路:借助了这篇博文的结论:传送门
结论:len-next[i]为此字符串的最小循环节(i为字符串的结尾),另外如果len%(len-next[i])==0,此字符串的最小周期就为len/(len-next[i]...
分类:
其他好文 时间:
2014-09-05 23:51:44
阅读次数:
203
原题http://poj.org/problem?id=3461
Oulipo
Time Limit: 1000MS
Memory Limit: 65536K
Total Submissions: 23987
Accepted: 9613
Description
The French author Georges Perec (...
分类:
其他好文 时间:
2014-09-05 10:07:51
阅读次数:
169
kmp算法是一种改进的字符串匹配算法,由D.E.Knuth与V.R.Pratt和J.H.Morris同时发现,因此人们称它为克努特——莫里斯——普拉特操作(简称KMP算法)。KMP算法的关键是根据给定的模式串W1,m,定义一个next函数。next函数包含了模式串本身局部匹配的信息。基本思想假设.....
分类:
其他好文 时间:
2014-09-05 09:59:51
阅读次数:
263
题目链接:uva 11557 - Code Theft
题目大意:给定n个文本,每个文本有一个文本名,现在给出一个文本,求给定文本和n个文本中连续相同行数最大值,并且输出文本名,注意为0时不用输出其它的文本名。
解题思路:将每个字符串用映射成一个hash值,然后对匹配文本枚举后缀,建立失配数组进行KMP匹配,记录下每个文本的匹配最大值。
#include
#include
#in...
分类:
其他好文 时间:
2014-09-04 23:44:50
阅读次数:
321
题目链接:uva 1358 - Generator
题目大意:给定n,表示有n中字符,然后给定一个字符串S,一开始字符串为空,现在每次随机生成一个1~n的字符添加到字符串末尾,问说字符串包含S为子串的生成次数期望。
解题思路:首先要对S进行预处理,求出失配数组。
定义dp[i]表示末尾部分匹配了i个S串所需要的次数期望,每次枚举可能出现的字符1~n。对于S字符串,i+1肯定是确定...
分类:
其他好文 时间:
2014-09-04 22:19:10
阅读次数:
288