Algorithm: pattern searching
kmp算法:用一个数组保存了上一个需要开始搜索的index,比如AAACAAA就是0, 1, 2, 0, 1, 2, 3, ABCABC就是0, 0, 0, 1, 2, 3,复杂度O(M+N)
#include <iostream>
#include <map>
#include <vector>
#include <algorithm>
#include <string> using namespace std; int main()
{
string pattern = "ABCABC";
string s = "ABCABCABCABCBCABC";
vector<int> lps(pattern.size());
int len = ;
int i = ;
lps[] = ;
while (i < pattern.size()) {
if (pattern[i] == pattern[len]) {
len++;
lps[i] = len;
i++;
}
else if (len != ) len = lps[len-];
else {
lps[i] = ;
i++;
}
}
//for (int i = 0; i < pattern.size(); i++) cout << lps[i] << endl;
i = ;
int j = ;
while (i < s.size()) {
if (s[i] == pattern[j]) {
j++;
i++;
}
if (j == pattern.size()) {
cout << i - j << endl;
j = lps[j-];
}
else if (pattern[j] != s[i]) {
if (j != ) j = lps[j-];
else i++;
}
}
return ;
}
robin-karp算法,给pattern做hash-value,给s前pattern.size()的子串也做hash-value,如果相同则输出当前位置,如果不相同则去掉这个子串的第一个,加上新进来的那个字符。复杂度最好是O(M+N),最差O(M*N)。
http://www.geeksforgeeks.org/searching-for-patterns-set-3-rabin-karp-algorithm/
FA算法:http://www.geeksforgeeks.org/searching-for-patterns-set-5-finite-automata/
Boyer Moore Algorithm - bad character heuristic: 记录各个字母在pattern最后出现的位置,从后往前比较,如果不匹配,就向前移动s当前字母在pattern的位置与当前匹配到的位置的差值,如果这个位置在当前位置后,则只能往前移一个。复杂度最好是O(N/M),最坏是O(N*M)。
http://www.geeksforgeeks.org/pattern-searching-set-7-boyer-moore-algorithm-bad-character-heuristic/
Algorithm: pattern searching的更多相关文章
- Boyer-Moore 字符串匹配算法
字符串匹配问题的形式定义: 文本(Text)是一个长度为 n 的数组 T[1..n]: 模式(Pattern)是一个长度为 m 且 m≤n 的数组 P[1..m]: T 和 P 中的元素都属于有限的字 ...
- Study on Algorithm of Selecting Safe Landing Area on Ground During Asteroid Soft Landing (EEIC2013 +161)
OUATTARA Sie, RUAN Xiaogang, Yan yan Institute of Artificial Intelligence and Robots, School of Elec ...
- The Knuth-Morris-Pratt Algorithm in my own words(转)
origianl For the past few days, I’ve been reading various explanations of the Knuth-Morris-Pratt str ...
- 脚本AI与脚本引擎
Scripted AI and Scripting Engines 脚本AI与脚本引擎 This chapter discusses some of the techniques you can us ...
- 搜索模式| 系列2——KMP算法
给定一个文本txt [0..n-1]和一个模式pat [0..m-1],写一个搜索函数search(char pat [],char txt []),在txt中打印所有出现的pat [] [].可以假 ...
- KMP Demo
The key of Kmp is to build a look up table that records the match result of prefix and postfix. Valu ...
- Spring mvc解析
方案时间 ,写代码时间 ,解决技术难点时间 , 自测时间,解决bug时间 , 联调时间 ,数据库优化,代码走查1个接口:2个小时 把那个字段再复原回来,不然兼容性不强还有一个刷数据的接口 public ...
- cf413E Maze 2D
E. Maze 2D time limit per test 2 seconds memory limit per test 256 megabytes input standard input ou ...
- 使用Python批量下载Plus上的Podcast
Plus是一个介绍数学之美与实际应用的网络杂志,其中包含了数学知识.轶闻趣事.历史典故等许多精彩的内容.该杂志恰好有一个Podcast栏目,提供了不少采访与讲座的mp3音频.于是, 我使用Python ...
随机推荐
- Flutter接入极光推送
(1)搜索 https://pub.dartlang.org/packages/jpush_flutter ,安装插件,并且按照官方配置 /android/app/build.gradle andro ...
- (转)python装饰器二
Python装饰器进阶之二 保存被装饰方法的元数据 什么是方法的元数据 举个栗子 def hello(): print('Hello, World.') print(dir(hello)) 结果如下: ...
- 利用JS最真实的模拟鼠标点击
为了破解永乐票务登录验证码问题 http://www.228.com.cn/auth/login?logout 当然,打码的过程自然依赖第三方平台,但问题是,哪怕平台给了你需要点击的(相对)坐标.你又 ...
- Struts2学习五----------指定多个配置文件
© 版权声明:本文为博主原创文章,转载请注明出处 指定多个配置文件 - 在Struts2配置文件中使用include可指定多个配置文件 实例 1.项目结构 2.pom.xml <project ...
- netty 对 http 的实现
netty的http协议栈无论是性能还是可靠性,都表现优异,非常适合在非web容器场景 下应用,相比于tomcat.jetty等web容器,它更轻量.小巧.灵活性和定制性也好: 总结:只要实现了htt ...
- 处理字符串的一些C函数
注意:以下函数都包含在ctype.h头文件中 1.isalpha函数 用来判断得到的参数是不是字母 #include<stdio.h> #include<ctype.h> in ...
- redis配置认证密码(转)
redis配置密码 1.通过配置文件进行配置yum方式安装的redis配置文件通常在/etc/redis.conf中,打开配置文件找到 ? 1 #requirepass foobared 去掉行前的注 ...
- 06 php 单例模式
一:单例模式的三大原则 (1)构造函数需要标记为非public(防止外部使用new操作符创建对象),单例类不能在其他类中实例化,只能被自身实例化. (2)拥有一个保存类的实例的静态成员变量$_inst ...
- 模式识别之svm()---支持向量机svm 简介1995
转自:http://www.blogjava.net/zhenandaci/archive/2009/02/13/254519.html 作者:Jasper 出自:http://www.blogjav ...
- 有状态的EJB对象和无状态的EJB对象
一,定义有状态Bean和无状态Bean 有状态Bean: @Stateful @Remote public class StatefulEjbBean implements StatefulEjb{ ...