随笔分类 - 模式匹配BF、KMP、BM、Sunday
摘要:前两天刷了几题leetcode,感觉挺简单,于是又想刷刷hduoj了。随便打开没做过的一页,找了一题通过人数最多的,就是这道6153. ①.看完题没想太多,觉得应该是后缀数组(多年没刷题的我字符串这一块对后缀数组记忆最深吧),因为S1和S2长度都一百万,n^2受不了。nlogn应该行。②.用后缀数组
阅读全文
摘要:其实很简单,只要对KMP的next数组的含义能理解的话,就没有问题了。/* * hdu3746/win.cpp * Created on: 2012-8-2 * Author : ben */#include <cstdio>#include <cstdlib>#include <cstring>#include <cmath>#include <ctime>#include <iostream>#include <algorithm>#include <queue>#include <set
阅读全文
摘要:这题纯属蹭过,按理说应该是扩展KMP的方法。我是直接用的KMP的next数组,特殊情况加以判断就过了。/* * hdu4300/win.cpp * Created on: 2012-8-1 * Author : ben */#include <cstdio>#include <cstdlib>#include <cstring>#include <cmath>#include <ctime>#include <iostream>#include <algorithm>#include <queue>
阅读全文
摘要:比较赤裸的KMP,将主串延长一倍即可达到旋转匹配的效果。/* * hdu2203/win.cpp * Created on: 2012-7-28 * Author : ben */#include <cstdio>#include <cstdlib>#include <cstring>#include <cmath>#include <algorithm>#include <iostream>#include <queue>using namespace std;const int MAX_PAR_LEN =
阅读全文
摘要:求模式串在主串中出现的次数,需要理解KMP后才能做,其实还是挺简单的,只要匹配完成一次后,j滑动到next[j]继续比较即可。刚开始一直TLE,上网找别人的代码比对,发现自己多写了一层循环,太粗心了……/* * hdu1686/linux.cpp * Created on: 2011-8-29 * Author : ben */#include <cstdio>#include <cstdlib>#include <cstring>#include <cmath>#include <algorithm>using namespace
阅读全文
摘要:网上好多说得不够清楚,不过真正理解了KMP的next数组的含义,一点就通了。非优化的next数组的含义是:next[i]=k表示模式串下标为i的字符的前k个字符与开头的前k个字符相等,那么从0到i-1的模式串必然是循环的,循环节为从0到next[i] - (2 * next[i] - i),其中 2 * next[i] - i表示的是从后往前的部分与从前往后的部分相交的长度。/* * hdu1358/linux.cpp * Created on: 2011-8-29 * Author : ben */#include <cstdio>#include <cstdlib>
阅读全文
摘要:刚学Sunday算法,都说Sunday比KMP快,于是我用Sunday打了半天,一直TLE,最后还是用KMP过了。不知道是不是我对Sunday算法的理解不正确,还是它只适合真正字符串的模式匹配。/* * hdu1711/linux.cpp * Created on: 2011-8-20 * Author : ben */#include <stdio.h>#include <stdlib.h>#include <string.h>#include <math.h>#define MAX_PAR_LEN 10005#define MAX_TXT_L
阅读全文
浙公网安备 33010602011771号