字符串匹配算法

更新时间:2023-07-03 10:23:30 阅读: 评论:0

3、串匹配算法
用语言实现蛮力法、Horspool、Boyer-Moore、Knuth-Morris-Pratt算法,针对不同的数据规模研究它们的性能。数据规模应在100,000以上。
int BruteForceStringMatch(string str, string pattern)
{
for( int i = 0; i <= str.size()- pattern.size(); i++ )
{
int j = 0;
while( (j < pattern.size()) && (pattern[j] == str[i+j]))
{
j++;
}
喵上校
if( j == pattern.size())
return i;
}
return (-1);
}
void ShiftTable(string pattern, int table[], int l)
{
for(int i = 0; i < 127; i++)
table[i] = l;
for(int j = 0; j < l-1; j++)
table[pattern[j]] = l-1-j;
}
void HorspoolMatch(string str, int table[], string pSize)
int main()
{
string a = "abcdefg";
string mode = "z";
cout<<BruteForceStringMatch(a, mode)<<endl;
return 0;
}
catnap#include<iostream>
using namespace std;
const int HASH_SIZE=256;
int table[HASH_SIZE];//对应字符表的255个字符建哈希表,表示对不匹配字符 向右移动的距离
void ShiftTable(char pattern[]){
/*建立一个以字母表中字符为索引的Table数组*/
int m=strlen(pattern);
for(int i=0;i<HASH_SIZE;i++)
table[i]=m;
for(int j=0;j<m-1;j++)
table[ pattern[j] ]=m-1-j;
}
int HorspoolMatching(char pattern[],char text[]){//平均效率为O(n)
/
*
pre:模式pattern,文本text
post:第一个匹配字串的最左端字符下标,没有找到匹配字串返回-1
*/
ShiftTable(pattern);
int m=strlen(pattern);
int n=strlen(text);
int i=m-1;
while(i <= n-1){
int k=0;                  //匹配的字符个数
while(k<=m-1 && pattern[m-1-k] == text[i-k] )
k++;
if(k==m)
return i-m+1;
el
i=i+table[ text[i] ]; //以模式最后一个字符确定移动距离,与B.M算法的最大区别,简化形式
}
return -1;
}
int main(){
char p[20],t[1000];
while(cin>>t && t!="."){
int times=5;
while(times--){
cin>>p;
cout<<HorspoolMatching(p,t)<<endl;
}
}
return 1;
}
#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) 
size(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; 
size( 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 rever_needle; 
copy( needle.rbegin() , d() , back_inrter( rever_needle ) ); 
vector<size_t> prefix_function,rever_prefix_function; 
compute_prefix_function( needle , prefix_function ); 
compute_prefix_function( rever_needle , rever_prefix_function ); 
size_t m = needle.size(); 
size( m + 1 , m - prefix_function[ m - 1 ] ); 
for ( size_t l = 1 ; l <= m ; l++ ) 
词根词缀
size_t t = rever_prefix_function[l-1]; 
size_t j = m - t ; 
if ( goodsuffix_heristic[j] > l - t ) 
goodsuffix_heristic[j] = l - t ; 
bridgewater
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 offt = 0 ; 
while ( offt <= n - m ) 
ssize_t j = m - 1 ; 
while ( j >= 0 && needle[j] == haystack[offt+j] ) 
j--; 
if ( j < 0 ) 
cout<<"Find a match at offt "<<offt<<endl; 
人认为
offt += goodsuffix_heristic[0]; 
el 
带来英语{ 
offt += max<size_t>( goodsuffix_heristic[j+1] , 
j - last_occurrence[haystack[offt+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); 
#include <iostream> 
初中英语演讲稿#include <algorithm> 
#include <string> 
#include <iostream> 
using namespace std; 
int* GetNextVal(const char *s, int &len) 
len = strlen(s); 
int *next = new int[len]; 
int i = 0; 
int j = -1; 
next[0] = -1; 
while(i<len-1)//注意这里跟KMP函数里面的不同 
belly
if(j==-1 || s[i]==s[j]) 
++i; 
++j; 
next[i] = j; 
el 
j = next[j]; 
return next; 
int KMP(const char *s, const char *t) 
int slen,tlen; 
int i,j; 
int *next = GetNextVal(t, t
len); 
slen = strlen(s); 
i = 0; 
j = 0; 
while(i<slen && j<tlen) 
if(j==-1 || s[i]==t[j]) 
++i; 
++j; 
el 
j = next[j]; 
delete[] next; 
if(j==tlen) 
return i - tlen; 
return -1; 
int main(int argc, char *argv[]) 
char s[128],t[128]; 
cout<<"请输入字符串s,t:"<<endl; 
while(cin>>s>>t) 
one more day
int pos1 = KMP(s,t); 
cout<<"字符串"<<s<<"中第一次出现"<<t<<"的位置是"<<pos1+1<<endl;  re是什么意思
return 0; 

本文发布于:2023-07-03 10:23:30,感谢您对本站的认可!

本文链接:https://www.wtabcd.cn/fanwen/fan/90/165681.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:字符   匹配   没有   找到   规模   字串
相关文章
留言与评论(共有 0 条评论)
   
验证码:
Copyright ©2019-2022 Comsenz Inc.Powered by © 专利检索| 网站地图