串匹配算法之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算法的关键点: 从右遍历 ...
随机推荐
- Leetcode 416.分割等和子集
分割等和子集 给定一个只包含正整数的非空数组.是否可以将这个数组分割成两个子集,使得两个子集的元素和相等. 注意: 每个数组中的元素不会超过 100 数组的大小不会超过 200 示例 1: 输入: [ ...
- /mnt/sdcard 是什么东西
关于/mnt/sdcard和sdcard的区别,可以这样理解:其实,安卓系统是从Linux而衍生出来的,而mnt是unix/Linux传统系统下挂载外部设备的专用目录,Linux默认挂载外部设备都会挂 ...
- 指定特殊的安装目录用configure进行配置
linux - Make install, but not to default directories? - Stack Overflow I want to run 'make install' ...
- Collection类及常用API
Collection类及常用API Collection—类集工具类,定义了若干用于类集和映射的算法,这些算法被定义为静态方法,具体查看api文档; a) public static void so ...
- tcpdump 进行抓包
tcpdump 进行抓包是怎么回事? tcp抓包是怎么搞的?
- 【bzoj1040】[ZJOI2008]骑士 并查集+基环树dp
题目描述 Z国的骑士团是一个很有势力的组织,帮会中汇聚了来自各地的精英.他们劫富济贫,惩恶扬善,受到社会各界的赞扬.最近发生了一件可怕的事情,邪恶的Y国发动了一场针对Z国的侵略战争.战火绵延五百里,在 ...
- [HDNOIP2017提高组]题解
(送给外省的同学们:HD = 海淀) [HDNOIP201701]小鱼干 试题描述 小喵喵有 n 个小鱼干排成一列,其中第 i 个小鱼干有两种属性,美味度 ai 和特殊度 bi. 现在小喵喵要吃掉一些 ...
- docker 镜像阿里加速器
1.登录 https://cr.console.aliyun.com/#/imageList 2.点击加速器tab,获取自己的加速器地址,然后执行黑框内的命令. .sudo mkdir -p /etc ...
- COGS728. [网络流24题] 最小路径覆盖问题
算法实现题8-3 最小路径覆盖问题(习题8-13) ´问题描述: 给定有向图G=(V,E).设P是G的一个简单路(顶点不相交)的集合.如果V中每个顶点恰好在P的一条路上,则称P是G的一个路径覆盖.P中 ...
- linux 程序自动运行总结
1.开机启动时自动运行程序Linux加载后, 它将初始化硬件和设备驱动, 然后运行第一个进程init.init根据配置文件继续引导过程,启动其它进程.通常情况下,修改放置在 /etc/rc或 /etc ...