Chinaunix首页 | 论坛 | 博客
  • 博客访问: 3651563
  • 博文数量: 880
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 6155
  • 用 户 组: 普通用户
  • 注册时间: 2016-11-11 09:12
个人简介

To be a better coder

文章分类

全部博文(880)

文章存档

2022年(5)

2021年(60)

2020年(175)

2019年(207)

2018年(210)

2017年(142)

2016年(81)

分类: C/C++

2016-11-23 09:07:02

原文地址:字符串匹配算法(一) 作者:txgc_wm

一、BF算法

      BF算法是普通的模式匹配算法,其基本思想就是将目标串的第一个字符与模式串的第一个字符进行匹配。若相等,则继续比较第二个字符;若不相等,则比较目标串的第二个字符和模式串的第一个字符。依次比较下去,直到得出最后的匹配结果。

      示例代码:

  1. static int bf(const char *src, const char *des)
  2. {
  3.     int i, pos;
  4.     int len_s, len_d;

  5.     if(src == NULL || des == NULL)
  6.         return -1;

  7.     len_s = strlen(src);
  8.     len_d = strlen(des);

  9.     for(pos = 0; pos < len_s - len_d; pos++) {
  10.         for(i = pos; i - pos < len_d; i++) {
  11.             if(src[i] != des[i - pos])
  12.                 break;
  13.         }
  14.         
  15.         if((i - pos) == len_d)
  16.             return pos;
  17.     }

  18.     return -1;
  19. }



二、Sunday算法

      Sunday算法是Daniel M.Sunday于1990年提出的一种比BM算法搜索速度更快的算法。其核心思想是:在匹配过程中,模式串并不被要求一定要按从左向右进行比较还是从右向左进行比较,它在发现不匹配时,算法能跳过尽可能多的字符以进行下一步的匹配,从而提高了匹配效率。Sunday算法思想跟BM算法很相似,在匹配失败时关注的是文本串中参加匹配的最末位字符的下一位字符。如果该字符没有在匹配串中出现则直接跳过,即移动步长= 匹配串长度+1;否则,同BM算法一样,即移动步长=匹配串中最右端的该字符到末尾的距离+1。


     例如我们要在"substring searching"查找"search",刚开始时,把子串与文本左边对齐:

s

u

b

s

t

r

i

n

g

 

s

e

a

r

c

h

i

n

g

s

e

a

r

c

h

 

 

 

 

 

 

 

 

 

 

 

 

 

     结果在第二个字符处发现不匹配,于是要把子串往后移动。但是该移动多少呢?这就是各种算法各显神通的地方了,最简单的做法是移动一个字符位置;KMP是利用已经匹配部分的信息来移动;BM算法是做反向比较,并根据已经匹配的部分来确定移动量。这里要介绍的方法是看紧跟在当前子串之后的那个字符(上图中的'i')。

      显然,不管移动多少,这个字符是肯定要参加下一步的比较的,也就是说,如果下一步匹配到了,这个字符必须在子串内。所以,可以移动子串,使子串中的最右边的这个字符与它对齐。现在子串'search'中并不存在'i',则说明可以直接跳过一大片,从'i'之后的那个字符开始作下一步的比较,如下图:

s

u

b

s

t

r

i

n

g

 

s

e

a

r

c

h

i

n

g

 

 

 

 

 

 

 

s

e

a

r

c

h

 

 

 

 

 

 

      比较的结果,第一个字符就不匹配,再看子串后面的那个字符是'r',它在子串中出现在倒数第三位,于是把子串向前移动三位,使两个'r'对齐,如下:

s

u

b

s

t

r

i

n

g

 

s

e

a

r

c

h

i

n

g

 

 

 

 

 

 

 

 

 

 

s

e

a

r

c

h

 

 

 

     这次匹配成功了!回顾整个过程,我们只移动了两次子串就找到了匹配位置。可以证明:用这个算法,每一步的移动量都比BM算法要大,所以肯定比BM算法更快。

以上Sunday算法文字描述摘自 这里

代码实现:

  1. #include<stdlib.h>
  2. #include<unistd.h>
  3. #include<string.h>
  4. #include<stdio.h>


  5. #define LETTER_MAX_LEN 256
  6. #define MAXLINE 1024

  7. static int sunday(const char *src, const char *des)
  8. {
  9.     int i, pos = 0;
  10.     int len_s, len_d;
  11.     int alphabet[LETTER_MAX_LEN] = {0};

  12.     if(src == NULL || des == NULL)
  13.         return -1;

  14.     len_s = strlen(src);
  15.     len_d = strlen(des);

  16.     for(i = 0; i < LETTER_MAX_LEN; i++)
  17.         alphabet[i] = len_d;

  18.     for(i = 0; i < len_d; i++)
  19.         alphabet[des[i]] = len_d - i - 1;

  20.     for(pos = 1; pos <= len_s - len_d; ) {
  21.         for(i = pos - 1; i - pos + 1 < len_d; i++) {
  22.             if(src[i] != des[i - pos + 1])
  23.                 break;
  24.         }
  25.         
  26.         if((i - pos + 1) == len_d)
  27.             return pos;
  28.         else
  29.             pos += alphabet[src[pos + len_d - 1]] + 1;
  30.     }

  31.     return -1;
  32. }

  33. int main(int argc, char **argv)
  34. {
  35.     FILE *fp = NULL;
  36.     char line[MAXLINE] = {0};
  37.     int linesNum;

  38.     fp = fopen(argv[1], "r");
  39.     if(fp == NULL) {
  40.         printf("can not open file %s\n", argv[1]);
  41.         return -1;    
  42.     }

  43.     while ((fgets(line, MAXLINE, fp)) != NULL) {
  44.         linesNum++;
  45.         if (sunday(line, "chinaunix") >= 0)
  46.             printf("find match at line %d\n", linesNum);
  47.     }

  48.     fclose(fp);
  49.     fp = NULL;

  50.     return 0;
  51. }
阅读(1122) | 评论(0) | 转发(0) |
0

上一篇:sed

下一篇:DNS解析过程详解

给主人留下些什么吧!~~