参考资料:

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

  1. 字符串匹配的Boyer-Moore(BM)算法

    各种文本编辑器的"查找"功能(Ctrl+F),大多采用Boyer-Moore算法. Boyer-Moore算法不仅效率高,而且构思巧妙,容易理解.1977年,德克萨斯大学的Robe ...

  2. 【算法】BM算法

    目录 BM算法 一. 字符串比较的分析 二.BM算法的思想 三.算法实现 BM算法 @ 一. 字符串比较的分析 如果要判定长度为\(n\)两个字符串相等,比较中要进行\(n\)比较,但是如果要判定两个 ...

  3. hrbustoj 1551:基础数据结构——字符串2 病毒II(字符串匹配,BM算法练习)

    基础数据结构——字符串2 病毒IITime Limit: 1000 MS Memory Limit: 10240 KTotal Submit: 284(138 users) Total Accepte ...

  4. 算法——字符串匹配之BM算法

    前言 Boyer-Moore算法是一种基于后缀匹配的模式串匹配算法(简称BM算法),后缀匹配就是模式串从右到左開始比較,但模式串的移动依旧是从左到右的.在实践中.BM算法效率高于前面介绍的<KM ...

  5. 如何在文本编辑器中实现时间复杂度O(n/m)的搜索功能? BM算法

    //字符串匹配 public class StringCmp { //约定:A主串长 n ,B模式串 长m.要求:在A串中找到B串匹配的下标 //BM算法:从B串和A串尾部开始比较,希望一次将B串向后 ...

  6. 字符串匹配算法(二)-BM算法详解

    我们在字符串匹配算法(一)学习了BF算法和RK算法,那有没更加高效的字符串匹配算法呢.我们今天就来聊一聊BM算法. BM算法 我们把模式串和主串的匹配过程,可以看做是固定主串,然后模式串不断在往后滑动 ...

  7. LG5487 【模板】线性递推+BM算法

    [模板]线性递推+BM算法 给出一个数列 \(P\) 从 \(0\) 开始的前 \(n\) 项,求序列 \(P\) 在\(\bmod~998244353\) 下的最短线性递推式,并在 \(\bmod~ ...

  8. 字符串匹配之horspool算法(简化的BM算法)

    前面介绍在BF,KMP这些算法的时候老是提到BM这个东西,究竟这什么东西,有啥高深的,这些问题我们如今不去考虑.不知道,认真读前几篇文章的读者有没有发现前面的算法都是从模式串的前面開始匹配的,那我们就 ...

  9. Boyer-Moore(BM)算法,文本查找,字符串匹配问题

    KMP算法的时间复杂度是O(m + n),而Boyer-Moore算法的时间复杂度是O(n/m).文本查找中“ctrl + f”一般就是采用的BM算法. Boyer-Moore算法的关键点: 从右遍历 ...

随机推荐

  1. 如何用jquery+json来写页面

    以下是json数据表: [ { "p" : "银川市", "c" : [{"c1":"兴庆区"},{ ...

  2. 使mysql的表内容可以输入中文内容

    修改数据库的字符集mysql>use mydb mysql>alter database mydb character set utf8;

  3. linux内核代码注释 赵炯 第三章引导启动程序

    linux内核代码注释 第三章引导启动程序 boot目录中的三个汇编代码文件   bootsect.s和setup.s采用近似intel的汇编语法,需要8086汇编器连接器as86和ld86 head ...

  4. “玲珑杯”线上赛 Round #17 河南专场

    闲来无事呆在寝室打打题,没有想到还有中奖这种操作,超开心的 玲珑杯”线上赛 Round #17 河南专场 Start Time:2017-06-24 12:00:00 End Time:2017-06 ...

  5. linux各种版本查看方法

    1.linux内核版本 cat /proc/version Linux version 4.13.0-39-generic (buildd@lgw01-amd64-038) (gcc version ...

  6. Spring 4.3.11.RELEASE文档阅读(一):overview

    一.宏观概述中的体会和发现 Spring是组件式的框架,它允许我们只使用其一小部分.Spring所做的工作,就是不断的简化我们的操作.比如它的IOC容器,当我们自己应用设计模式,比如说:建造者.工厂. ...

  7. OGNL表达式详解

    OGNL表达式标签中的值有三种: 1.直接是OGNL表达式. 2.字符串需转义自OGNL表达式. 1)OGNL表达式转换为字符串显示,需要用''(单引号)引起来. 2)转为OGNL表达式的字符串,需要 ...

  8. CS231n笔记 Lecture 2 Image Classification pipeline

    距离度量\(L_1\) 和\(L_2\)的区别 一些感性的认识,\(L_1\)可能更适合一些结构化数据,即每个维度是有特别含义的,如雇员的年龄.工资水平等等:如果只是一个一般化的向量,\(L_2\)可 ...

  9. HDU——1233还是畅通工程(克鲁斯卡尔+优先队列)

    还是畅通工程 Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Sub ...

  10. VK Cup 2016 - Qualification Round 1——B. Chat Order(试手stack+map)

    B. Chat Order time limit per test 3 seconds memory limit per test 256 megabytes input standard input ...