串匹配算法之BM算法
参考资料:
http://blog.csdn.net/eric491179912/article/details/6210009
http://blog.163.com/pengfeicui@yeah/blog/static/106403250201092681158650/
这里,我们仅仅用了坏字符规则
定义一个滑动距离函数:
dist(C)= 1) m – j j = max{j| tj = c && 1<= j <= m - 1}
2) m 若C不出现在模式中或tm = c
比如 T = pattern dist(a)= 7 – 2 ; dist(t) = 7 – 4 (算最后出现的) ; dist(e) = 7 – 5 ; dist(r) = 7 - 6;
最后一个dist(n) = 7 (m ,模式串T的总长度) ,其他的未出现的字符, dist(ch) = 7;
程序如下:
1: void BMDist(string str,int dist[])
2: {
3: int i;
4: for (i = 0; i < 256; i++)
5: {
6: dist[i] = str.length();
7: }
8: for (i = 0; i < str.length() - 1; i++)//最后一个字符取不到
9: {
10: dist[str[i]] = str.length() - i - 1;
11: }
12: dist[str[str.length()-1]] = str.length();
13:
14: }
15:
16: int BM(string s, string t,int dist[])
17: {
18: int i = t.length();
19: while(i <= s.length())
20: {
21: int j = t.length();
22: while(j>0 && s[i-1]==t[j-1])
23: {
24: j--;
25: i--;
26: }
27: if (j == 0)
28: {
29: return i + 1;
30: }
31: else
32: {
33: i = i + dist[s[i-1]];
34: }
35: }
36: return -1;
37: }
.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }
测试代码:
1: int main()
2:
3: {
4:
5: while(1)
6: {
7: clock_t start,stop;
8: string Str,Tsr;
9:
10: int flag1,flag2,flag3;
11: int dist[256];
12:
13: int next[1000]={0,};
14: cout <<"请输入S串与T串:" <<endl;
15: cin >> Str >> Tsr;
16: cout << endl;
17:
18: start = clock();
19: for (int i=0; i< 1000; i++)
20: {
21: flag1 = BF(Str,Tsr);
22: }
23: stop = clock();
24: cout << "BF算法的执行时间是:"<< stop - start << endl;
25:
26:
27: start = clock();
28: for (int i=0; i< 1000; i++)
29: {
30: kmpNext(Tsr,next);
31: flag2 = kmp(Str,Tsr,next);
32: }
33: stop = clock();
34: cout << "KMP算法的执行时间是:"<< stop - start << endl;
35:
36:
37: start = clock();
38: for (int i=0; i< 1000; i++)
39: {
40: BMDist(Tsr,dist);
41: flag3 = BM(Str,Tsr,dist);
42: }
43: stop = clock();
44: cout << "BM算法的执行时间是:"<< stop - start << endl;
45:
46: if (flag1 == -1)
47: {
48: cout << "没有找到子串"<<endl;
49: }
50: else
51: {
52: cout << "找到子串的位置为"<< flag1 <<endl;
53: }
54: if (flag2 == -1)
55: {
56: cout << "没有找到子串"<<endl;
57: }
58: else
59: {
60: cout << "找到子串的位置为"<< flag2 <<endl;
61: }
62: if (flag3 == -1)
63: {
64: cout << "没有找到子串"<<endl;
65: }
66: else
67: {
68: cout << "找到子串的位置为"<< flag3 <<endl;
69: }
70: }
71:
72: return 0;
73: }
.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }
串匹配算法之BM算法的更多相关文章
- 字符串匹配的Boyer-Moore(BM)算法
各种文本编辑器的"查找"功能(Ctrl+F),大多采用Boyer-Moore算法. Boyer-Moore算法不仅效率高,而且构思巧妙,容易理解.1977年,德克萨斯大学的Robe ...
- 【算法】BM算法
目录 BM算法 一. 字符串比较的分析 二.BM算法的思想 三.算法实现 BM算法 @ 一. 字符串比较的分析 如果要判定长度为\(n\)两个字符串相等,比较中要进行\(n\)比较,但是如果要判定两个 ...
- hrbustoj 1551:基础数据结构——字符串2 病毒II(字符串匹配,BM算法练习)
基础数据结构——字符串2 病毒IITime Limit: 1000 MS Memory Limit: 10240 KTotal Submit: 284(138 users) Total Accepte ...
- 算法——字符串匹配之BM算法
前言 Boyer-Moore算法是一种基于后缀匹配的模式串匹配算法(简称BM算法),后缀匹配就是模式串从右到左開始比較,但模式串的移动依旧是从左到右的.在实践中.BM算法效率高于前面介绍的<KM ...
- 如何在文本编辑器中实现时间复杂度O(n/m)的搜索功能? BM算法
//字符串匹配 public class StringCmp { //约定:A主串长 n ,B模式串 长m.要求:在A串中找到B串匹配的下标 //BM算法:从B串和A串尾部开始比较,希望一次将B串向后 ...
- 字符串匹配算法(二)-BM算法详解
我们在字符串匹配算法(一)学习了BF算法和RK算法,那有没更加高效的字符串匹配算法呢.我们今天就来聊一聊BM算法. BM算法 我们把模式串和主串的匹配过程,可以看做是固定主串,然后模式串不断在往后滑动 ...
- LG5487 【模板】线性递推+BM算法
[模板]线性递推+BM算法 给出一个数列 \(P\) 从 \(0\) 开始的前 \(n\) 项,求序列 \(P\) 在\(\bmod~998244353\) 下的最短线性递推式,并在 \(\bmod~ ...
- 字符串匹配之horspool算法(简化的BM算法)
前面介绍在BF,KMP这些算法的时候老是提到BM这个东西,究竟这什么东西,有啥高深的,这些问题我们如今不去考虑.不知道,认真读前几篇文章的读者有没有发现前面的算法都是从模式串的前面開始匹配的,那我们就 ...
- Boyer-Moore(BM)算法,文本查找,字符串匹配问题
KMP算法的时间复杂度是O(m + n),而Boyer-Moore算法的时间复杂度是O(n/m).文本查找中“ctrl + f”一般就是采用的BM算法. Boyer-Moore算法的关键点: 从右遍历 ...
随机推荐
- Cookie和Session的作用和工作原理
一.Cookie详解 (1)简介 因为HTTP协议是无状态的,即服务器不知道用户上一次做了什么,这严重阻碍了交互式Web应用程序的实现.在典型的网上购物场景中,用户浏览了几个页面,买了一盒饼干和两饮料 ...
- [git 学习篇] git commit原理 --实践体会
1 现对readme.txt作出修改,增加一行内容: Git has a mutable index called stage. Git is a distributed version contro ...
- ACM-ICPC北京赛区2017网络同步赛
E: Cats and Fish 时间限制:1000ms 单点时限:1000ms 内存限制:256MB 描述 There are many homeless cats in PKU campus. T ...
- C语言总结(3)
1.字符输入函数getchar 输入一个字符 char ch; ch=getchai(); 字符输出函数putchar 输出一个字符 putchar(输出参数): 2.调用scanf和printf输入 ...
- [小技巧]使用set对列表去重,并保持列表原来顺序
- Java生产者消费者模式
为什么要使用生产者和消费者模式 在线程世界里,生产者就是生产数据的线程,消费者就是消费数据的线程.在多线程开发当中,如果生产者处理速度很快,而消费者处理速度很慢,那么生产者就必须等待消费者处理完,才能 ...
- SparkStreaming 编程指南
摘要:学习SparkStreaming从官网的编程指南开始,由于Python编码修改方便不用打包,这里只整理python代码! 一.概述 Spark Streaming 是 Spark Core AP ...
- 给某个li标签家样式
HTML: <div class="tabs clearfix"> <ul id="der"> <li ><a hre ...
- bzoj 2797 [Poi2012]Squarks 枚举一个,推出所有
题目大意 设有n个互不相同的正整数{X1,X2,...Xn},任取两个Xi,Xj(i≠j),能算出Xi+Xj. 现在所有取法共n*(n-1)/2个和,要你求出X1,X2,...Xn. 输出所有满足条件 ...
- 【BZOJ4555】【TJOI2016】【HEOI2016】求和 (第二类斯特林数+NTT卷积)
Description 在2016年,佳媛姐姐刚刚学习了第二类斯特林数,非常开心. 现在他想计算这样一个函数的值: $$f(n)=\sum_{i=0}^n\sum_{j=0}^i S(i,j)\tim ...