摘要: 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2594简单的KMP算法应用~~~代码如下: 1 #include <cstdio> 2 #include <cstring> 3 using namespace std; 4 5 const int N = 50002; 6 char str1[N]; 7 char str2[N]; 8 int next[N]; 9 10 void get_next(int len_1); 11 int kmp_search(int len_1, int len_2); 12 13 int 阅读全文
posted @ 2012-08-21 21:15 山路水桥 阅读(321) 评论(0) 推荐(0) 编辑
摘要: 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3336考查next数组的应用和理解~~~~代码如下: 1 #include <cstdio> 2 #include <cstring> 3 using namespace std; 4 5 const int N = 200002; 6 char str[N]; 7 int next[N]; 8 9 void get_next(int len)10 {11 int i = 0;12 int j = -1;13 next[i] = -1;14 while(i < ... 阅读全文
posted @ 2012-08-21 21:14 山路水桥 阅读(122) 评论(0) 推荐(0) 编辑
摘要: 2009年4月22日 12:56Slyar发表评论阅读评论文章作者:Slyar文章来源:Slyar Home (www.slyar.com) 转载请注明,谢谢合作。KMP算法是一种改进的字符串匹配算法,由D.E.Knuth与V.R.Pratt和J.H.Morris同时发现,因此人们称它为克努特—莫里... 阅读全文
posted @ 2012-08-21 19:03 山路水桥 阅读(225) 评论(0) 推荐(0) 编辑
摘要: http://acm.hdu.edu.cn/showproblem.php?pid=3336题意:求字串中【前缀+跟前缀相同的子串】的个数?Sample Input14ababSample Output6abab:包括2个a,2个ab,1个aba,1个abab这里要用到next值的意义:next[i... 阅读全文
posted @ 2012-08-21 18:45 山路水桥 阅读(174) 评论(0) 推荐(0) 编辑
摘要: socket目 录socket 1.socket() 2.bind() 3.connect() 4.listen() 5.accept() 6.send() 和recv() 7.sendto() 和recvfrom() 8.close() 和shutdown() 9.getpeername() 10... 阅读全文
posted @ 2012-08-21 14:55 山路水桥 阅读(2284) 评论(0) 推荐(0) 编辑
摘要: 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1358题意:给你一个字符串,要求将字符串的全部字符最少循环2次需要添加的字符数。 例子: abcabc 已经循环2次,添加数为0 abcac 没有循环2次,添加字符abcac。数目为5. abcabcab 已经循环过2次,但第三次不完整,需要添加数为1 分析:还是用到了next数组,这个循环节这很巧妙啊。。。 做这个题需要好好理解KMP算法,尤其是next数组。非优化的next数组的含义是:next[i]=k默示模式串下标为i的字符的前k-1个字符与开首的前k-1个字符相等,那么从1到i-1的模式串 阅读全文
posted @ 2012-08-21 12:04 山路水桥 阅读(341) 评论(0) 推荐(0) 编辑
摘要: 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1358主要考查KMP算法next数组本质的理解。len - len[len]为循环节的大小~~~代码如下: 1 #include <cstdio> 2 #include <cstring> 3 using namespace std; 4 5 const int N = 1000002; 6 char str[N]; 7 int next[N]; 8 9 void get_next(int len)10 {11 int i = 0;12 int j = -1;13 nex.. 阅读全文
posted @ 2012-08-21 12:00 山路水桥 阅读(412) 评论(0) 推荐(0) 编辑
摘要: 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1050 1 #include <cstdio> 2 #include <cstring> 3 using namespace std; 4 5 int main() 6 { 7 int mark[201]; 8 int a, b; 9 int t;10 int n;11 while(scanf("%d", &t) != EOF)12 {13 for(int i = 0; i < t; i++)14 {15 ... 阅读全文
posted @ 2012-08-21 11:55 山路水桥 阅读(411) 评论(0) 推荐(0) 编辑
摘要: 我们在一个母字符串中查找一个子字符串有很多方法。KMP是一种最常见的改进算法,它可以在匹配过程中失配的情况下,有效地多往后面跳几个字符,加快匹配速度。当然我们可以看到这个算法针对的是子串有对称属性,如果有对称属性,那么就需要向前查找是否有可以再次匹配的内容。在KMP算法中有个数组,叫做前缀数组,也有... 阅读全文
posted @ 2012-08-21 08:32 山路水桥 阅读(21212) 评论(4) 推荐(4) 编辑