【模式匹配】之 —— Z-BOX算法
- 一 Z-BOX的概念
- 二 Z-BOX算法的计算过程
- 三 Z-BOX算法的代码实现C语言版
- 四 Z-BOX算法在具体的模式匹配字符串查找中的应用
- 五 总结
在字符串的模式匹配中,有单模匹配和多模匹配之分。本系列文章将对单模匹配和多模匹配逐一进行讲解。其中单模匹配中将会讲解以下几个算法:
- Z-BOX算法
- KMP算法
- BM算法
- sunday算法
一 Z-BOX的概念
i | Zi(p)值 | 说明 |
0 | 略过 | |
1 | 1 | p[1] = p[0] |
2 | 0 | p[2] != p[0] |
3 | 2 | p[3, 4] = p[0, 1] |
4 | 3 | p[4,5,6] = p[0,1,2] |
5 | 1 | p[5] = p[0] |
6 | 0 | p[6] != p[0] |
7 | 0 | p[7] != p[0] |
二 Z-BOX算法的计算过程
三 Z-BOX算法的代码实现(C语言版)
#include <string.h>
#include <stdio.h>void ZBox(const char* pattern, unsigned int length, unsigned int zbox[])
{zbox[0] = 0;unsigned int left = 0;unsigned int right = 0;for (unsigned int i = 1; i < length; i++){if (i > right)//情况3{int n = 0;for ( ; pattern[n] == pattern[i+n]; n++);if (0 != n){right = i+n-1;left = i;}zbox[i] = n;}else{if (zbox[i-left] < right-i+1)//情况1{zbox[i] = zbox[i-left];}else//情况2{int n = 1;for (;pattern[right-i+n] == pattern[right+n]; n++);zbox[i] = right-i+n;right += n-1;left = i;}}printf(" zbox[%d] = %d, left is %d, right is %d\r\n", i, zbox[i], left, right);}
}int main(int argc, char* argv[])
{const char pattern[] = "aabaaab";unsigned int zbox[100];ZBox(pattern, strlen(pattern), zbox);return 0;
}
上面的程序输出结果为:
zbox[2] = 0, left is 1, right is 1
zbox[3] = 2, left is 3, right is 4
zbox[4] = 3, left is 4, right is 6
zbox[5] = 1, left is 4, right is 6
zbox[6] = 0, left is 4, right is 6
四 Z-BOX算法在具体的模式匹配、字符串查找中的应用
#include <string.h>
#include <stdio.h>int SearchWithZBox(const char* dest, const char* pattern)
{int nDlen = strlen(dest);int nPlen = strlen(pattern);char *str = new char[nDlen+nPlen];if (!str){goto Exit0;}int *Z = new int[nDlen+nPlen];if (!Z){goto Exit0;}memcpy(str, pattern, nPlen);memcpy(str+nPlen, dest, nDlen);//create z boxZ[0] = 0;int l = 0;int r = 0;for (int i = 1; i < nDlen+nPlen; i++){if (i > r){int n = 0;for (; str[i+n] == str[n]; n++);if (n > 0){l = i;r = i+n-1;}Z[i] = n;}else{if (Z[i-l] < r-i+1){Z[i] = Z[i-l];} else{int n = 1;for (int s = r-i; str[s+n] == str[r+n]; n++);Z[i] = r-i+n;l = i;r = r+n-1; }}if (i >= nPlen && Z[i] >= nPlen){printf("Search With ZBox Find at %d\r\n", i - nPlen);}}
Exit0:if (str){delete str;str = NULL;}if (Z){delete Z;Z = NULL;}return -1;
}int main(int argc, char* argv[])
{// 0 1 2 3 4 5// 012345678901234567890123456789012345678901234567890123456789const char dest[] = "demoxdemoaaabaaaxdembbaaaddemobaaababdemoooabcxbaabaaadddemo";const char pattern[] = "aabaaab";unsigned int zbox[100];//ZBox(pattern, strlen(pattern), zbox);SearchWithZBox(dest, "demo");return 0;
}
程序运行结果如下:
Search With ZBox Find at 5
Search With ZBox Find at 26
Search With ZBox Find at 37
Search With ZBox Find at 56
五 总结
六 参考资料
这是一本全面介绍字符串处理算法的书籍,我没有找到中文版,这是英文版
【模式匹配】之 —— Z-BOX算法相关推荐
- 【模式匹配】之 —— KMP算法详解及证明
一 RevisionsHistory 1 一 Revisions History 二 前言 三 关于算法学习 四 KMP算法始末 KMP算法是用来 ...
- 字符串的模式匹配(KMP)算法
一.背景 给定一个主串(以 S 代替)和模式串(以 P 代替),要求找出 P 在 S 中出现的位置,此即串的模式匹配问题. Knuth-Morris-Pratt 算法(简称 KMP)是解决这一问题的常 ...
- 【算法视频】字符串模式匹配--布鲁特.福斯算法
2.4.字符串模式匹配 资讯网址:www.qghkt.com 腾讯课堂:https://qghkt.ke.qq.com/20个常用算法 模式串(或子串)在主串中的定位操作通常称为串的模式匹配,它是各种 ...
- 【数据结构与算法基础】模式匹配问题与KMP算法
前言 数据结构,一门数据处理的艺术,精巧的结构在一个又一个算法下发挥着他们无与伦比的高效和精密之美,在为信息技术打下坚实地基的同时,也令无数开发者和探索者为之着迷. 也因如此,它作为博主大二上学期最重 ...
- 【模式匹配】之 —— Sunday算法
本文代码下载地址 http://download.csdn.net/detail/sun2043430/5273911 Sunday算法思路 Sunday算法的思想和BM算法中的坏字符思想非常类似. ...
- 算法:模式匹配之KMP算法
前言: 昨天看到<算法导论>里的第32章:字符串匹配,说到一个关于字符串匹配的很好的算法--KMP.关于KMP的内存含意以及KMP的来源,不是本文讲述的范畴,请感兴趣的读者自行查阅相关资料 ...
- 字符串模式匹配——最长公共子序列与子串 KMP 算法
最长公共子序列 最长公共子序列的问题很简单,就是在两个字符串中找到最长的子序列,这里明确两个含义: 子串:表示连续的一串字符 . 子序列:表示不连续的一串字符. 所以这里要查找的是不连续的最长子序列, ...
- 【《Real-Time Rendering 3rd》 提炼总结】(十一) 第十四章 : 游戏开发中的渲染加速算法总结
本文由@浅墨_毛星云 出品,转载请注明出处. 文章链接: http://blog.csdn.net/poem_qianmo/article/details/78884513 导读 这是一篇1万3千 ...
- 【ABC三维路径规划】基于matlab人工蜂群算法多无人机三维路径规划【含Matlab源码 170期】
一.无人机简介 0 引言 随着现代技术的发展,飞行器种类不断变多,应用也日趋专一化.完善化,如专门用作植保的大疆PS-X625无人机,用作街景拍摄与监控巡察的宝鸡行翼航空科技的X8无人机,以及用作水下 ...
- 【数据结构与算法基础】哈夫曼树与哈夫曼编码(C++)
前言 数据结构,一门数据处理的艺术,精巧的结构在一个又一个算法下发挥着他们无与伦比的高效和精密之美,在为信息技术打下坚实地基的同时,也令无数开发者和探索者为之着迷. 也因如此,它作为博主大二上学期最重 ...
最新文章
- Matplitlib绘图入门1,这一篇就够了
- Java 常用类 -Arrays
- adb logcat查看手机端日志
- MySQL的学习--触发器
- node linux脚本,用Shell脚本快速搭建Ubuntu下的Nodejs开发环境
- HTML5实现Word中文字全环绕图片效果
- java 接口表单提交_Java http 调用接口提交表单以及文件
- 80后偷偷“变老”的20种表现
- OpenSSL windows 下编译
- 从技术角度看人人网,互联网营销
- android之list排序
- Azure Storage架构介绍
- Nginx配置文件(作为Web服务器)
- 毛世怀HPDS2017公路路面设计程序系统
- 中科院阿里云联合发布11比特云接入超导量子计算服务
- abaqus python提取楼层剪力_用Python提取ABAQUS中节点集合的反力
- CDN是什么?与DNS有什么关系?及其原理
- mac+微信打开连接到服务器,MAC OS系统 ,微信接收到的文件,打开wo… - Apple 社区...
- getchar()函数的作用
- 使用ffmpeg 提取视频关键帧