随笔分类 -  KMP算法

摘要:题目链接http://poj.org/problem?id=3461题目大意 以A为子串,查找B中有几个A。思路。KMP算法View Code 1 #include<stdio.h> 2 #include<string.h> 3 #define N 1000001 4 int nextt[N]; 5 char str1[N],str2[N]; 6 void next(char s[]) 7 { 8 int i=1,j=0; 9 int len=strlen(s);10 nextt[0]=-1;11 while(i<len)12 {13 ... 阅读全文
posted @ 2012-08-03 21:02 时光旅行的懒猫 阅读(183) 评论(0) 推荐(0)
摘要:题目链接http://acm.hdu.edu.cn/showproblem.php?pid=1867题目大概意思就是 有重复的就覆盖掉 然后输出。不过这个得比较字符串str1 str2还得再比较str2 str1.思路 KMP算法 掌握的还是不熟。View Code 1 #include<stdio.h> 2 #include<string.h> 3 #include<stdlib.h> 4 #define N 100001 5 int nextt[N]; 6 void next(char s[]) 7 { 8 int i=1,j=0; 9 int len= 阅读全文
posted @ 2012-08-03 18:48 时光旅行的懒猫 阅读(198) 评论(0) 推荐(0)
摘要:题目链接http://poj.org/problem?id=1961该题的题意是这样的,给若干个字符串,判断该字符串的前n位最多重复了几次,比如,给ababab,结果是前4位重复了2次,前6位重复了3次,忽略重复一次的情况.现在我们将注意力放在一个给定的字符串重复了多少次,然后做一个循环就可以求出所有的结果。 我们要根据kmp算法中的next函数来解决这个问题,以ababab为例加以说明: String:ababab Next: -1001234 这里根据后面的需要多计算了一位next值。 我们用ababab即作为主串有作为模式串来进行匹配,假设匹配到第7为时不匹配了(下标中1开始)... 阅读全文
posted @ 2012-07-21 11:30 时光旅行的懒猫 阅读(200) 评论(0) 推荐(0)