使用bm算法进行字符串匹配的例子.
2009-01-16 16:16
316 查看
使用bm算法进行字符串匹配的例子.
// BM.cpp : Defines the entry point for the console application. // #include <stdio.h> #include <string> #define XSIZE 256 #define ASIZE 256 #define MAX(x,y) (x) > (y)? (x):(y) #define OUTPUT(x) printf("匹配位置:%d/n",x) #define PatternStr "付" #define MainStr "AAAAa付付b付" void preBmBc(char *x, int m, int bmBc[]) { int i; for (i = 0; i < ASIZE; ++i) bmBc[i] = m; for (i = 0; i < m - 1; ++i) bmBc[x[i]] = m - i - 1; } void suffixes(char *x, int m, int *suff) { int f, g, i; suff[m - 1] = m; g = m - 1; for (i = m - 2; i >= 0; --i) { if (i > g && suff[i + m - 1 - f] < i - g) suff[i] = suff[i + m - 1 - f]; else { if (i < g) g = i; f = i; while (g >= 0 && x[g] == x[g + m - 1 - f]) --g; suff[i] = f - g; } } } void preBmGs(char *x, int m, int bmGs[]) { int i, j, suff[XSIZE]; suffixes(x, m, suff); for (i = 0; i < m; ++i) bmGs[i] = m; j = 0; for (i = m - 1; i >= 0; --i) if (suff[i] == i + 1) for (; j < m - 1 - i; ++j) if (bmGs[j] == m) bmGs[j] = m - 1 - i; for (i = 0; i <= m - 2; ++i) bmGs[m - 1 - suff[i]] = m - 1 - i; } void BM(char *x, int m, char *y, int n) { int i, j, bmGs[XSIZE], bmBc[ASIZE]; /* Preprocessing */ preBmGs(x, m, bmGs); preBmBc(x, m, bmBc); /* Searching */ j = 0; while (j <= n - m) { for (i = m - 1; i >= 0 && x[i] == y[i + j]; --i); if (i < 0) { OUTPUT(j); j += bmGs[0]; } else j += MAX(bmGs[i], bmBc[y[i + j]] - m + 1 + i); } } int main(int argc, char* argv[]) { BM(PatternStr,strlen(PatternStr),MainStr,strlen(MainStr)); getchar(); return 0; } // BM.cpp : Defines the entry point for the console application. // #include <stdio.h> #include <string> #define XSIZE 256 #define ASIZE 256 #define MAX(x,y) (x) > (y)? (x):(y) #define OUTPUT(x) printf("匹配位置:%d/n",x) #define PatternStr "付" #define MainStr "AAAAa付付b付" void preBmBc(char *x, int m, int bmBc[]) { int i; for (i = 0; i < ASIZE; ++i) bmBc[i] = m; for (i = 0; i < m - 1; ++i) bmBc[x[i]] = m - i - 1; } void suffixes(char *x, int m, int *suff) { int f, g, i; suff[m - 1] = m; g = m - 1; for (i = m - 2; i >= 0; --i) { if (i > g && suff[i + m - 1 - f] < i - g) suff[i] = suff[i + m - 1 - f]; else { if (i < g) g = i; f = i; while (g >= 0 && x[g] == x[g + m - 1 - f]) --g; suff[i] = f - g; } } } void preBmGs(char *x, int m, int bmGs[]) { int i, j, suff[XSIZE]; suffixes(x, m, suff); for (i = 0; i < m; ++i) bmGs[i] = m; j = 0; for (i = m - 1;
相关文章推荐
- 使用GPU进行字符串匹配--cuda编程实现
- delphi MatchesMask 使用正则表达式进行字符串匹配的函数
- Qt使用一个事件队列对所有发出的事件进行维护(QObject的event()函数相当于dispatch函数),用EventLabel 继承QLabel作为例子(简单明了) good
- 通过编码方式使用性能计数器来进行性能计数的一个简单例子 - ZT
- 使用 awk&cut 进行字符文本处理
- 使用Python做简单的字符串匹配
- 使用OpenCv中Mat进行水平投影与垂直投影并实现字符切分
- 使用KD树进行最近邻查找的例子
- 利用js 进行输入框自动匹配字符的小例子
- 字符串匹配之BM算法
- java使用UDP来进行客户端和服务器端通信的简单例子
- MySQL中字符串匹配函数LOCATE和POSITION使用方法
- 字符串匹配——C++使用Regex
- android 与struts 使用HttpsUrlConnection进行数据(文件/字符)传送 (客户端例)
- 字符串匹配的Boyer-Moore算法 BM算法详解
- 字符串匹配算法——利用有限自动机进行匹配
- 一个使用socket进行文件传输的例子
- Android 中的单元测试(使用ServiceTestCase 进行 Service测试 例子)
- 字符串匹配算法:KMP算法与BM算法比较
- 函数:MySQL中字符串匹配函数LOCATE和POSITION使用方法