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 ...
随机推荐
- SSH(Struts2 + Hibernate + Spring)嵌入 KindEditor(KE)
1. kindeditor 灌水 (下载地址,demo) 经过两天的折磨,又源代码,又官网学习,又网络搜索的,终于成功的嵌入KindEditor了. KindEditor确 ...
- 更换 mac时 开发证书
更换 mac时 开发证书,先导出, 后导入,如果出现错误,把projects自动签名改为手动
- RTC实时时钟驱动
RTC(Real-Time Clock)实时时钟为操作系统提供了一个可靠的时间,并且在断电的情况下,RTC实时时钟也可以通过电池供电,一直运行下去. RTC通过STRB/LDRB这两个ARM指令向CP ...
- 使用 mybatis + flying + 双向相关建模 的电商后端
代码地址如下:http://www.demodashi.com/demo/12468.html mybatis.flying 众所周知,mybatis 虽然易于上手,但放到互联网环境下使用时,不可避免 ...
- Android · 获取网络图片
import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileOutputStream; import j ...
- Oracle 中for update和for update nowait的区别
http://www.cnblogs.com/quanweiru/archive/2012/11/09/2762223.html 1.for update 和 for update nowait 的区 ...
- Allegro skill
https://blog.csdn.net/wyu0725/article/details/52367199 Allegro skill二次开发和更改菜单页面 简单的使用skill;能够使Aleggr ...
- Spring学习五----------Bean的配置之Bean的生命周期
© 版权声明:本文为博主原创文章,转载请注明出处 Bean的生命周期 1.定义 2.初始化 3.使用 4.销毁 初始化和销毁的三种方式 1.实现org.springframework.beans.fa ...
- 说说我的web前端之路,分享些前端的好书(转)
WEB前端研发工程师,在国内算是一个朝阳职业,这个领域没有学校的正规教育,大多数人都是靠自己自学成才.本文主要介绍自己从事web开发以来(从大二至今)看过的书籍和自己的成长过程,目的是给想了解Java ...
- 【Python + Selenium】初次用IE浏览器之报错:selenium.common.exceptions.WebDriverException: Message: Unexpected error launching Internet Explorer. Protected Mode settings are not the same for all zones.
初次用IE浏览器运行自动化程序时,报错:selenium.common.exceptions.WebDriverException: Message: Unexpected error launchi ...