字符串匹配

http://www.cnblogs.com/jingmoxukong/p/4343770.html

模式匹配是数据结构中字符串的一种基本运算,给定一个子串,要求在某个字符串中找出与该子串相同的所有子串,这就是模式匹配

假设P是给定的子串,T是待查找的字符串,要求从T中找出与P相同的所有子串,这个问题成为模式匹配问题。P称为模式,T称为目标。如果T中存在一个或多个模式为P的子串,就给出该子串在T中的位置,称为匹配成功;否则匹配失败。

KMP 算法

http://kb.cnblogs.com/page/176818/

字符串匹配是计算机的基本任务之一。

  举例来说,有一个字符串"BBC ABCDAB ABCDABCDABDE",我想知道,里面是否包含另一个字符串"ABCDABD"?

许多算法可以完成这个任务,Knuth-Morris-Pratt算法(简称KMP)是最常用的之一。它以三个发明者命名,起头的那个K就是著名科学家Donald Knuth。

----- 我的理解 ----

此算法的思想是, 当出现模式匹配未完全时候, 利用已经匹配的部分模式中的字符串(蕴含的)信息, 尽量将模式匹配的开始位置向右边移动。

----- 已经匹配的部分模式中字符串蕴含的信息 ----

一般将这个信息叫部分匹配表, 所谓部分, 就是未完全匹配模式字符串的含义, 未匹配命中, 只匹配了模式串的前面一部分子串。

正如上面所说,部分匹配串, 就是 模式串的一个前缀,

如果此部分匹配串中, 如果存在 一个子串, 此子串既是部分模式串的 前缀, 同时也是 部分模式串的 后缀, 同时这个子串是 同类子串中最长的一个, 则称 此子串为 此部分匹配串的 最大前缀。

当模式匹配过程发生字符匹配失败, 则将 模式字符串 对应主串起始位置(A), 移动到 模式字符串中已经部分匹配子串中 最大前缀的对应的后缀开始 位置(B), 并从失败的位置(C)继续开始比对。

主串中从A到B的位置(不包括B), 对于模式字符串, 已经不需要再进行比较, 因为 这些位置, 按照最大前缀的定义,不能满足部分匹配串的 最大前缀的, 更何谈匹配整个模式串!

主串中从B到C的位置(不包括C), 对于模式字符串, 对于B位置, B-C正好对应 部分匹配字符串的 最大前缀, 所以也不需要 进行匹配。

--------  阮一峰 实例化解释 部分匹配表的生成 --------------

下面介绍《部分匹配表》是如何产生的。

  首先,要了解两个概念:"前缀"和"后缀"。 "前缀"指除了最后一个字符以外,一个字符串的全部头部组合;"后缀"指除了第一个字符以外,一个字符串的全部尾部组合。

  "部分匹配值"就是"前缀"和"后缀"的最长的共有元素的长度。以"ABCDABD"为例,

  - "A"的前缀和后缀都为空集,共有元素的长度为0;

  - "AB"的前缀为[A],后缀为[B],共有元素的长度为0;

  - "ABC"的前缀为[A, AB],后缀为[BC, C],共有元素的长度0;

  - "ABCD"的前缀为[A, AB, ABC],后缀为[BCD, CD, D],共有元素的长度为0;

  - "ABCDA"的前缀为[A, AB, ABC, ABCD],后缀为[BCDA, CDA, DA, A],共有元素为"A",长度为1;

  - "ABCDAB"的前缀为[A, AB, ABC, ABCD, ABCDA],后缀为[BCDAB, CDAB, DAB, AB, B],共有元素为"AB",长度为2;

  - "ABCDABD"的前缀为[A, AB, ABC, ABCD, ABCDA, ABCDAB],后缀为[BCDABD, CDABD, DABD, ABD, BD, D],共有元素的长度为0。

  

----- 理解 ------

如果按照这种实例所示, 采用列举比对, 计算最大前缀, 则会导致很耗时, 属于穷举法。

模式字符串 为 s[1, n]

对于部分匹配子串 s[1, m], 其中 m = [1, n]

for i=m-1,1,-1 do

if compare(s[1, i],  s[m-i+1,m]) == 0 then

// find max prefix len

end

end

最坏时间为  n*n

------- 使用归纳法计算 部分匹配表 则更加有效率。--------

假设 s[1, q] 的 最大前缀为 k == f(q), 则 s[1, k] == s[q-k+1, q]

则 对于 s[1, q+1], 我们来求 其最大前缀f(q+1)

if s[q+1] == s[k+1] then

f(q+1) = f(q) + 1 = k + 1

else

//s[1, k] 是不行了, 继续从s[1, k]中找到其最大前缀, 用此最大前缀后的字符与s[q+1]比较

if s[q+1] == s[f(k)+1] then

f(q+1) = f(k) + 1 = f(f(q)) + 1

else

// s[1, f(k)] 也不行了, 。。。。

end

end

C代码实现

https://github.com/fanqingsong/code-snippet/blob/master/C/kmp_string_matcher/kmp_string_matcher.c

E_BOOL_TYPE string_is_head_of_string(char* headStr, char* string, int* pfailPos)
{
char* pHeadStr = NULL;
char* pString = NULL;
char chHead = ;
char chString = ;
int index = ; if (!headStr || !string)
{
MyPrintf("arg is null");
return FALSE;
} pHeadStr = headStr;
pString = string; while( TRUE )
{
chHead = *pHeadStr;
chString = *pString; // headStr is over, now result is true
if ( chHead == )
{
return TRUE;
} // string is over firstly, return false
if ( chString == )
{
*pfailPos = index;
return FALSE;
} // headStr is not a head of string
if ( chHead != chString )
{
*pfailPos = index;
return FALSE;
} pHeadStr++;
pString++; index++;
}
} void calcPrefixlenByIndex(char* substr, int substrPrefix[], int iNum)
{
int PrefixLen = ; MyPrintf("iNum = %d", iNum); if ( iNum == )
{
substrPrefix[iNum] = ;
MyPrintf("iNum = %d substrPrefix[iNum]=%d", iNum, substrPrefix[iNum]);
return;
} // calc [0, iNum-1] string prefix len, saving to substrPrefix[iNum-1]
calcPrefixlenByIndex(substr, substrPrefix, iNum-); // according to [0, iNum-1] string prefix, we deduce [0, iNum] string prefix
PrefixLen = substrPrefix[iNum-];
do
{
// if the char after the [0, iNum-1] string prefix is EQUAL to the char at substr[iNum],
// then the the [0, iNum] string prefix len = the [0, iNum-1] string prefix len + 1
// PrefixLen+1-1 notation mean index from 0, while PrefixLen+1 mean index from 1
if ( substr[PrefixLen+-] == substr[iNum] )
{
substrPrefix[iNum] = PrefixLen + ;
break;
}
// else calc from the prefix of the [0, iNum-1] string prefix
else
{
PrefixLen = substrPrefix[PrefixLen];
}
}while ( PrefixLen > ); MyPrintf("iNum = %d substrPrefix[iNum]=%d", iNum, substrPrefix[iNum]);
} void compute_string_prefix(char* substr, int substrPrefix[], int maxPrefixEleNum)
{
int substrlen = ; substrlen = strlen(substr);
if (substrlen > maxPrefixEleNum)
{
return;
} calcPrefixlenByIndex(substr, substrPrefix, substrlen-);
} #define MAX_PREFIX_ELE_NUM 1024 int kmp_matcher(char* string, char* substr)
{
char* cursor = NULL;
int index = ;
int subLen = ;
int stringLen = ;
int maxPos = ; int substrPrefix[MAX_PREFIX_ELE_NUM] = {};
int failPos = ;
int partialMatchedPos = ;
int maxPrefixLen = ; // pointer to the inner postion of cursor and substr
char* pCursor = NULL;
char* pSubstr = NULL; if (!string || !substr)
{
MyPrintf("arg is null");
} subLen = strlen(substr);
stringLen = strlen(string);
maxPos = stringLen - subLen + ; // substrPrefix is string prefix of substr
// scope : 1-substrlen
// the index i element is the max prefix length of substr[1, i]
compute_string_prefix(substr, substrPrefix, MAX_PREFIX_ELE_NUM); index = ;
maxPrefixLen = ;
while ( index < maxPos )
{
cursor = string + index; pCursor = cursor + maxPrefixLen;
pSubstr = substr + maxPrefixLen; if ( string_is_head_of_string(pSubstr, pCursor, &failPos) )
{
return index;
}
else
{
// failPos scope: 0-substrlen-1
// failPos is substr comparing char postion that do not match cursor string
// then substr[0, partialMatchedPos] is the matched part
partialMatchedPos = maxPrefixLen + failPos - ; // substr[0] is not matched
if ( failPos == )
{
// string compare from next position
index ++; // next comparation have not to consider prefix
maxPrefixLen = ;
}
else
{
// the max prefix length of partial matched string, ie substr[0, partialMatchedPos]
maxPrefixLen = substrPrefix[partialMatchedPos]; index += partialMatchedPos - maxPrefixLen;
}
}
} return -;
}

aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAqUAAAG7CAIAAAB1jnsTAAAgAElEQVR4nO3d3c9s110f8OfPee59eXxtydRtTxXUmFamdq9c5CJxw0VlqGhdILhAeROENjUJpaAUUiW0IjgkfpIUVQUiYRrT0qgVphJIjd/ipCQpF6cX53g8s9dav/Xbe2aevWedz0dL1pw9a6+XPXvmu/bMPsdX3/E9H1MURVEUZexy9R3f87FnXvyyoiiKoiiLy3++e3cLpTU8ea8oiqIoJyirJ728VxRFUZSzl9WTXt4riqIoytnL6kkv7xVFURTl7GX1pJf3iqIoinL2snrSy3tFURRFmVGuQ629Vk96ea8oiqIoM8ot5/29e/fu3bsXb7m9vP/eD/329/3IJzLlH37o00//2B+t/mopiqIoyrIShPqZru/3A/6YsD9B3v+7T/zmH/zha3/wh18uymtfevW1L7362qd++wuf+vTnP/Xpz//bj3/i7/zQ767+at1+ufvE9fUTL91SR3deeHLt+Z522MuO3kqH4uXH7nite8O+lePzMB/kWzvml/Vqnqrcft7vYv7IsD9B3n/mc1/81re//c1vdcq3vv3tz3zu8x/8gVcqjTz/wiPX148+9cIj19ePPvdg45NPPT75nmT31DMvfvmZF196dLplu+W23hUvP3bnyI+nVY5qZ9inzvuzzvHW8v7413puiY/bS49O36r1+heVELd/kM9STnHMB3g1T1ZWyfvjr+xPk/evfP4//ff/8b9/9COf+9BLn/2xj/7Oi//mMz/+qy//8R//r7fefOPPXn/9z15//c033/jFT/7+L37y98O8f/yx5w9OqSefevz6+tm77X4vaOk9812RD6TTR9cGjup0Uie/vj/nHG/v+v72X4vWcbu/ND98yR5/7Pl6/S0lxCVdMxwz/pMc882/mrdXHurr+1duvvgnX/nzZ3/iU//gpz/53C984ns/8uvf97GPv/rqV9564//s5/1Hfuu/JPL+8ceef7Cxm/fv7bX+y98tF5T3GziqZ8/7c85x5LyvH7fnnr1uHcxa/S0lhLyfU7b+akZl7r113dYWPHWqK/uVf79/+Xde+ZOv/PkPfOy3v+cXfv2pn/ml7/q5D3/Xh3/21Ve/8tYbX3399ddff/1P33zzjY994Yu/+vufa+b9iy89ev3s3Rczeb//DdvLj925fuSpl8vz7/3fAu688OTBTwMPGizD4P6Wxw633y3/uDu5n3/hkcZvDff3unu/0zsvPHm4490nrq+vd8N+/1vQ+y1MfsV4r9rLj93ZbXswhWrN6byag3wQS+WROcdRfbDluWf3RvvSfi7uqjUnVR/qS49eHwx1v7sHj/ePwPsfTJU5Tl6O3GGczuv6iRemeb+/b+dLhenJEL5M+cl2nm0NsvZalMetdSQ751L5XkieY6npJI5t8PaZvnP3Pm1ar0V8eldLfr75t3/rHG6/g6JTtDga23k1Z5frhtO2Frd5wq/x17w//37ef+DFjzz+oz/z13/yX9z9uX/+gV/80fvX96+//qf3r+9/47/+1if/26faeV8piby//x47qHOQprtz6MEJ9NKju8fTi5L3VsoH2++/c3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gBclkneP/3005N0L7fcat4Hwb8f/4vzPriAbl1STwK7rFAdz+Sp28n7XV/VueQbLCcIwGUp834/4Cd/PH3ex4E0cZNbFpTtX7Xzfq7uGJJx3h1Da0b5cWZaSKa4vAe4dNW8v5/x+4/PkvezIm2/fjLvWxnfWky0BNXKMcRxnl92BONZnL4CG+ChVf5+//Shs/x+X4ZxN2WvwpivpnLQeKuLUnf7pMGT5H31qePTWt4DPLSq9+sFYX9s3k9ycScIwmq6TzJ78tSs9oON8fbqKiTI+3JjdUu5FgmG0ZVc7iQbWTYGAFbXuj+/Ffan/D5/MpR8TgcPWu1n8j6Ti+X20+Z9d/DHOH7RcJJhAHD7NvH38eKNmWfjKM3EZzJ6r4pEn6wAWnmfb/9q/uCTBDbAQ2tDeR9cGbd26bZWffbIvJ9sLx8ke8wMZsGqKCDvAR5aK+R9Nde7YRkkX1w/eal901Y9cK28L4fUTdlg6TN3VMkZzWoHgAGsc30/icNq4CUjsxXemWe7GzPb97O53CUZqK3onTsqAKi67bxXFEVRFOVSysK8VxRFURRlgCLvFUVRFGX8Iu8VRVEUZfwi7xVFURRl/CLvFUVRFGX8Iu8VRVEUZfwi7xVFURRl/CLvFUVRFGX88iDvFUVRFEUZu/x/Q+u3uHyZ/7QAAAAASUVORK5CYII=" alt="" />

算法-KMP串匹配的更多相关文章

  1. KMP串匹配算法解析与优化

    朴素串匹配算法说明 串匹配算法最常用的情形是从一篇文档中查找指定文本.需要查找的文本叫做模式串,需要从中查找模式串的串暂且叫做查找串吧. 为了更好理解KMP算法,我们先这样看待一下朴素匹配算法吧.朴素 ...

  2. [C++] [算法] KMP算法

    KMP串匹配算法是一个经典的算法. 传统BF算法是传统的字符串匹配算法.很好理解.叶实现.但时间复杂度太高. 本文将从字符串模式字符串被称为.为了匹配字符串被称为主弦. KMP配时能够少移动从串的位置 ...

  3. 值得花费一周研究的算法 -- KMP算法(indexOf)

    KMP算法是由三个科学家(kmp分别是他们名字的首字母)创造出来的一种字符串匹配算法. 所解决的问题: 求文本字符串text内寻找第一次出现字符串s的下标,若未出现返回-1. 例如 text : &q ...

  4. [每天默写一个算法]KMP

    [每天默写一个算法]KMP 作业要求:默写String的KMP算法. KMP是经典的字符串匹配算法.复杂度为O(n+m) public static class StringKMP { /// < ...

  5. 数据结构与算法--KMP算法查找子字符串

    数据结构与算法--KMP算法查找子字符串 部分内容和图片来自这三篇文章: 这篇文章.这篇文章.还有这篇他们写得非常棒.结合他们的解释和自己的理解,完成了本文. 上一节介绍了暴力法查找子字符串,同时也发 ...

  6. 经典算法 KMP算法详解

    内容: 1.问题引入 2.暴力求解方法 3.优化方法 4.KMP算法 1.问题引入 原始问题: 对于一个字符串 str (长度为N)和另一个字符串 match (长度为M),如果 match 是 st ...

  7. 笔记-算法-KMP算法

    笔记-算法-KMP算法 1.      KMP算法 KMP算法是一种改进的字符串匹配算法,KMP算法的关键是利用匹配失败后的信息,尽量减少模式串与主串的匹配次数以达到快速匹配的目的.具体实现就是实现一 ...

  8. KMP算法 字符串匹配(看猫片)

    前言 此篇笔记根据自己的理解和练习心得来解释算法,只代表个人观点,如有不足请指出(我刚学QWQ) 浅谈字符串匹配 设想一个场景,假设你是一个净化网络语言环境的管理员,每天需要翻阅大量的文章和帖子来查找 ...

  9. KMP算法——字符串匹配

    正直找工作面试巅峰时期,有幸在学校可以听到July的讲座,在时长将近三个小时的演讲中,发现对于找工作来说,算法数据结构可以算是程序员道路的一个考量吧,毕竟中国学计算机的人太多了,只能使用这些方法来淘汰 ...

随机推荐

  1. 12. 求简单交错序列前N项和

    求简单交错序列前N项和 #include <stdio.h> int main() { int denominator, flag, i, n; double item, sum; whi ...

  2. eclipse常用窗口和功能总结

    1.左上角File按钮功能:最重要的导入import第三方项目,maven,git,general等. File->import File->properties按钮,显示项目的存储位置, ...

  3. 代理和block反向传值

    代理传值: // SendViewController.h #import <UIKit/UIKit.h> @protocol SendInFor <NSObject> -(v ...

  4. MySQL DATE_FORMAT() 函数

    定义和用法 DATE_FORMAT() 函数用于以不同的格式显示日期/时间数据. 语法 DATE_FORMAT(date,format) date 参数是合法的日期.format 规定日期/时间的输出 ...

  5. JS采用正则表达式简单获取URL地址栏参数

    GetUrlParam:function GetUrlParam(param){ var reg = new RegExp("(^|&)"+ param +"=( ...

  6. PHP常用正则表达式汇总 [复制链接]

    PHP常用正则表达式汇总 [复制链接] 上一主题下一主题   离线我是小猪头   法师     发帖 539 加关注 发消息 只看楼主 倒序阅读 使用道具楼主  发表于: 2011-06-22 更多 ...

  7. Connection Management and Security

    High Performance My SQL  THIRD EDITION Each client connection gets its own thread within the server ...

  8. locality

    Computer Systems A Programmer's Perspective Second Edition Well-written computer programs tend to ex ...

  9. 转:php 获取时间今天明天昨天时间戳

    <?php echo "今天:".date("Y-m-d")."<br>"; echo "昨天:".d ...

  10. .Net程序员安卓学习之路1:登陆界面

    任何编程学习起步均是HelloWorld,作为稍有>net编程经验的我们来说就跳过这步吧,咱们且从简单登录界面开始.先看看效果: 一.准备知识: 1. 安卓环境:安装好JDK,直接去官网下载AD ...