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的更多相关文章

  1. Boyer-Moore 字符串匹配算法

    字符串匹配问题的形式定义: 文本(Text)是一个长度为 n 的数组 T[1..n]: 模式(Pattern)是一个长度为 m 且 m≤n 的数组 P[1..m]: T 和 P 中的元素都属于有限的字 ...

  2. 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 ...

  3. 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 ...

  4. 脚本AI与脚本引擎

    Scripted AI and Scripting Engines 脚本AI与脚本引擎 This chapter discusses some of the techniques you can us ...

  5. 搜索模式| 系列2——KMP算法

    给定一个文本txt [0..n-1]和一个模式pat [0..m-1],写一个搜索函数search(char pat [],char txt []),在txt中打印所有出现的pat [] [].可以假 ...

  6. KMP Demo

    The key of Kmp is to build a look up table that records the match result of prefix and postfix. Valu ...

  7. Spring mvc解析

    方案时间 ,写代码时间 ,解决技术难点时间 , 自测时间,解决bug时间 , 联调时间 ,数据库优化,代码走查1个接口:2个小时 把那个字段再复原回来,不然兼容性不强还有一个刷数据的接口 public ...

  8. cf413E Maze 2D

    E. Maze 2D time limit per test 2 seconds memory limit per test 256 megabytes input standard input ou ...

  9. 使用Python批量下载Plus上的Podcast

    Plus是一个介绍数学之美与实际应用的网络杂志,其中包含了数学知识.轶闻趣事.历史典故等许多精彩的内容.该杂志恰好有一个Podcast栏目,提供了不少采访与讲座的mp3音频.于是, 我使用Python ...

随机推荐

  1. xampp添加 django支持

    apache配置文件中添加 WSGIScriptAlias /chatbot1 /Users/css/djangoprojects/chatbot1/chatbot1/wsgi.pyWSGIPytho ...

  2. 终于会用c#中的delegate(委托)和event(事件)了 [转]

    原文 : http://www.cnblogs.com/zhangchenliang/archive/2012/09/19/2694430.html 一.开篇忏悔 对自己最拿手的编程语言C#,我想对你 ...

  3. 23. Spring Boot启动加载数据CommandLineRunner【从零开始学Spring Boot】

    转:http://blog.csdn.net/linxingliang/article/details/52069503 实际应用中,我们会有在项目服务启动的时候就去加载一些数据或做一些事情这样的需求 ...

  4. centos 7 mariadb 安装

    yum install -y mariadb mariadb-server systemctl start mariadb systemctl enable mariadb #初始化 mysql_se ...

  5. C语言之基本算法32—鞍点

    //数组 /* ================================================================== 题目:求随意矩阵的全部鞍点.并统计个数.(在矩阵中 ...

  6. hdu4857 &amp; BestCoder Round #1 逃生(拓扑逆排序+优先队列)

    题目链接:http://acm.hdu.edu.cn/showproblem.php? pid=4857 ----------------------------------------------- ...

  7. Redis(六):java里常用的redis客户端(Jedis和Redisson)

    Redis的各种语言客户端列表,请参见Redis Client.其中Java客户端在github上start最高的是Jedis和Redisson.Jedis提供了完整Redis命令,而Redisson ...

  8. MySQL系列:innodb源代码分析之线程并发同步机制

    innodb是一个多线程并发的存储引擎,内部的读写都是用多线程来实现的,所以innodb内部实现了一个比較高效的并发同步机制. innodb并没有直接使用系统提供的锁(latch)同步结构,而是对其进 ...

  9. 看完这篇再不会 View 的动画框架,我跪搓衣板

    引言 众所周知,一款没有动画的 app,就像没有灵魂的肉体,给用户的体验性很差.现在的 android 在动画效果方面早已空前的发展,1.View 动画框架 2.属性动画框架 3.Drawable 动 ...

  10. debian jessie install note

    Debian支持非常多的硬件,包括arm/mips/ppc/x86,于是想安装个Debian看看,也不想总屈服在canonical的ubuntu下面. 光盘镜像太多了 纯社区版的安装总是没有想像得那么 ...