Boyer-Moore算法的C++实现 Post author:xfxia Post published:2023年8月7日 Post category:其他 Boyer-Moore字符串匹配算法是一个在实践当中用得比较多的字符串查找算法,作者实现了一个C++版本的算法,供以后参考。 #include <iostream> #include <algorithm> #include <string> #include <vector> #ifndef ssize_t typedef off_t ssize_t; #endif using namespace std; void compute_last_occurrence(const string& needle , vector<ssize_t>& last_occurrence) { last_occurrence.resize(256,-1); for ( size_t i = 0 ; i < needle.size() ; i++ ) { last_occurrence[needle[i]] = i; } } void compute_prefix_function(const string& needle , vector<size_t>& prefix_function) { if ( needle.size() == 0 ) { return; } prefix_function.resize( needle.size() , 0 ); size_t d = 0 ; for ( size_t i = 1 ; i < needle.size() ; i++ ) { while ( d > 0 && needle[d] != needle[i] ) { d = prefix_function[d-1]; } if ( needle[i] == needle[d] ) { d++; } prefix_function[i]=d; } } void compute_goodsuffix_heuristic( const string& needle ,vector<size_t>& goodsuffix_heristic) { string reverse_needle; copy( needle.rbegin() , needle.rend() , back_inserter( reverse_needle ) ); vector<size_t> prefix_function,reverse_prefix_function; compute_prefix_function( needle , prefix_function ); compute_prefix_function( reverse_needle , reverse_prefix_function ); size_t m = needle.size(); goodsuffix_heristic.resize( m + 1 , m – prefix_function[ m – 1 ] ); for ( size_t l = 1 ; l <= m ; l++ ) { size_t t = reverse_prefix_function[l-1]; size_t j = m – t ; if ( goodsuffix_heristic[j] > l – t ) { goodsuffix_heristic[j] = l – t ; } } } void boyer_moore_matcher(const string& haystack,const string& needle) { size_t n=haystack.size(); size_t m=needle.size(); if ( n == 0 || m == 0 || n < m ) { cout<<“Invalid input”<<endl; return; } vector<ssize_t> last_occurrence; compute_last_occurrence(needle,last_occurrence); vector<size_t> goodsuffix_heristic; compute_goodsuffix_heuristic( needle , goodsuffix_heristic ); size_t offset = 0 ; while ( offset <= n – m ) { ssize_t j = m – 1 ; while ( j >= 0 && needle[j] == haystack[offset+j] ) { j–; } if ( j < 0 ) { cout<<“Find a match at offset “<<offset<<endl; offset += goodsuffix_heristic[0]; } else { offset += max<size_t>( goodsuffix_heristic[j+1] , j – last_occurrence[haystack[offset+j]] ); } } } int main() { string haystack,needle; while ( true ) { cout<<“Input the haystack:”<<endl; getline(cin,haystack); cout<<“Input the needle:”<<endl; getline(cin,needle); cout<<“Match results:”<<endl; boyer_moore_matcher(haystack,needle); } } 版权声明:本文为oyster2008原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。 原文链接:https://blog.csdn.net/oyster2008/article/details/5673752 你可能也喜欢 单链表逆转 推荐算法3—基于用户的协同过滤算法 Facts vs. Dimensions—–Understanding Key Business Intelligence Terms elementui TaBle表格表头添加icon,以及hover上去的提示文字 浅析大数据与人工智能 大数据量微服务传输_数据传输说明| 8086微处理器 error LNK2005的可能可行解决方案 Type-c手机OTG取电充电边充边传数据方案知晓 ubuntu安装tensorflow2.0、nvidia驱动、cuda、cuDNN、anaconda Android BluetoothLeScanner.startScan()方法与传统BluetoothAdapter.startLeScan()方法使用 spark内存划分 孩子反复看一本绘本到底要不要干涉 【方案篇】——解决git clone 速度慢的问题 pytorch中transpose的用法 Eclipse配置运行SpringBoot(Spring Tool Suite) 浏览器回退出现ERR_CACHE_MISS 解决方案 什么是网络通信协议?(七层功能介绍) 关于计算机网络中超网聚合问题的计算 【CCF】ISBN号码(C++小白版) 什么叫明文,什么叫密文,为什么不允许在数据库里明文保存密码?