字符串匹配算法
高考历史、文综历史专题复习【全部完成】
用语言实现蛮力法、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;
}
#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;
else
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)
{
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++ )
{
你可能喜欢
- 微机课程设计
- C语言字符串
- 模糊算法
- KMP算法
- 算法合集
- 微机原理课程设计
- 入侵检测系统
- 快速匹配算法
- 微机原理加法器课程设计314页
- 微机原理课程设计18页
- 微机原理课程设计9页
- 《微机原理与应用》课程设计指导书4页
- 微机课程设计报告33页
- 电子琴 常熟理工微机课程设计4页
- C语言电子教案第六章字符数组和字符串4页
- 常用字符串处理函数C语言4页
- 关于C语言字符串函数使用的一点心得3页
- C语言水仙数与大小字符串转换3页
- C语言-字符串函数大全和详解3页
- 简单C语言文件操作,读入一个文件内所有字符串,写出一个文件内的所有字符串3页
- 模糊算法 控制系统 硕士论文58页
- 模糊聚类算法12页
- 模糊算法14页
- 基于模糊聚类算法中FCM算法的36页
- 基于模糊算法的专家系统26页
- 模糊算法在企业设备资产管理决策中应用[论文]5页
- KMP算法详解5页
- KMP算法详解4页
- KMP算法思想汇总18页
- KMP算法详解14页
- 一种改进的KMP算法6页
- 对KMP算法的一个改进3页
- 经典ACM算法合集经典ACM算法合集14页
- 遗传算法合集3页
- 算法合集之《信息学竞赛中的思维方法》8页
- 算法合集之《偶图的算法及应用》13页
- 算法合集之《遗传算法的特点及其应用》21页
- 算法合集之《论对题目中算法的选择》4页
- 微机原理加法器课程设计314页
- 微机原理课程设计18页
- 微机原理课程设计9页
- 《微机原理与应用》课程设计指导书4页
- 8086简易计算器的设计 计算机硬件 微机原理 课程设计16页
- 微机原理与技术课程设计8页


