博客
关于我
26:算法--以自动机实现的字符串匹配
阅读量:735 次
发布时间:2019-03-21

本文共 2578 字,大约阅读时间需要 8 分钟。

字符匹配

性质

字符串匹配是一种常见的数据处理任务,旨在在源字符串中查找与给定模式完全匹配的位置。在这个过程中,算法需要同时考虑模式的长度以及源字符串的长度。

接口设计

类 CharacterMatch

template
class CharacterMatch {public: CharacterMatch(); ~CharacterMatch();public: DataStruct::Array::DynArray
& RunInAutoMachine( const DataStruct::Array::DynArray
& arrPattern_, const DataStruct::Array::DynArray
& arrSource_ );};

实现

构造函数

template
CharacterMatch
::CharacterMatch() {}

析构函数

template
CharacterMatch
::~CharacterMatch() {}

算法运行

template
DataStruct::Array::DynArray
&CharacterMatch
::RunInAutoMachine( const DataStruct::Array::DynArray
& arrPattern_, const DataStruct::Array::DynArray
& arrSource_) { int _nPatternLen = arrPattern_.GetSize(); int _nSourceLen = arrSource_.GetSize(); int _nPreMaxMatchLen = 0; int _nCurMaxMatchLen = 0; DataStruct::Array::DynArray
_arrRet; for (int _i = 0; _i < _nSourceLen; _i++) { if (_nPreMaxMatchLen == _nPatternLen) { int _nCurMayMaxMatchLen = _nPreMaxMatchLen; while (_nCurMayMaxMatchLen > 0) { bool _bSuccess = true; int _k = 0; while (_k < _nCurMayMaxMatchLen) { if (arrSource_(_i - _k) == arrPattern_(_nCurMayMaxMatchLen - 1 - _k)) { _k++; } else { break; } } if (_k == _nCurMayMaxMatchLen) { _bSuccess = true; } else { _bSuccess = false; } if (_bSuccess) { _nCurMaxMatchLen = _nCurMayMaxMatchLen; break; } } } else { if (arrSource_(_i) == arrPattern_(_nPreMaxMatchLen)) { _nCurMaxMatchLen = _nPreMaxMatchLen + 1; } else { int _nCurMayMaxMatchLen = _nPreMaxMatchLen; while (_nCurMayMaxMatchLen > 0) { bool _bSuccess = true; int _k = 0; while (_k < _nCurMayMaxMatchLen) { if (arrSource_(_i - _k) == arrPattern_(_nCurMayMaxMatchLen - 1 - _k)) { _k++; } else { break; } } if (_k == _nCurMayMaxMatchLen) { _bSuccess = true; } else { _bSuccess = false; } if (_bSuccess) { _nCurMaxMatchLen = _nCurMayMaxMatchLen; break; } } } if (_nCurMaxMatchLen == _nPatternLen) { _arrRet.Add(_i - _nPatternLen + 1); } _nPreMaxMatchLen = _nCurMaxMatchLen; } } return _arrRet;}

算法目标与正确性证明

算法目标

通过线性扫描源字符串,找到与模式匹配的所有起始位置。本算法结合了暴力匹配和回溯机制,以确保在最佳匹配点停止搜索。

正确性证明

循环不变式

在每次迭代开始时,当前位置之前的所有字符与模式的最大匹配长度保持不变。

证明过程

  • 初始化阶段:在首次迭代前,_nPreMaxMatchLen = 0,循环不变式成立。
  • 迭代过程:假设在第 $k`` 次迭代开始时,_nPreMaxMatchLen` 表示上一次匹配结束后可以与模式达到最大匹配长度。当检查当前字符时:
    • 如果当前字符与模式的下一个字符匹配,则当前最大匹配长度增加。
    • 如果当前字符与模式的下一个字符不匹配,则回溯匹配长度,寻求更短的子序列。
  • 终止条件:一旦当前匹配长度达到模式长度,则记录当前位置作为匹配起点。
  • 循环不变式维持:通过逐步增加或回退匹配长度,确保了循环不变式的持久性。
  • 通过上述步骤,可以确保每次迭代都能正确更新最大匹配长度,从而完成所有匹配起始位置的搜索。

    转载地址:http://lxmgz.baihongyu.com/

    你可能感兴趣的文章
    Python化身“安全老中医”:一键搞定工控系统漏洞,从零基础到精通,收藏这篇就够了!
    查看>>
    Python加速运行技巧
    查看>>
    python办公自动化基础遍历文件夹
    查看>>
    python办公自动化基础输出文件和文件夹
    查看>>
    python办公自动化基础搜索文件
    查看>>
    python知识:@classmethod和@staticmethod的异同
    查看>>
    python前端之css
    查看>>
    Python制作进度条,原来有这么多方法
    查看>>
    Python制作简单的学生成绩管理系统
    查看>>
    python制作甘特图的基本知识(附Demo)
    查看>>
    python制作一个简单的服务器,【Python】 做一个简单的 http 服务器
    查看>>
    Python到底能做什么?它的优点在哪?
    查看>>
    python利用pytorch库导出图像分割算子
    查看>>
    python利用pyshark监听网卡来抓包其中pyshark中摸索的一些可用参数
    查看>>
    python利用excel分析过杀漏失
    查看>>
    python判断汉字数目
    查看>>
    python判断文件是空的,如果是空的,就删除
    查看>>
    python判断密码是否正确_python密码判断是否符合要求的方法
    查看>>
    python判断字符串包含中文_Python 判断字符串是否包含中文
    查看>>
    python删除第一行_Python 乱码指北:一行删掉根目录
    查看>>