BF和KMP算法
来源:互联网 发布:java程序员适合mac吗 编辑:程序博客网 时间:2024/06/03 00:56
/***字符串匹配算法***/
#include<cstring>
#include<iostream>
using namespace std;
#include<cstring>
#include<iostream>
using namespace std;
#define OK 1
#define ERROR 0
#define OVERFLOW -2
typedef int Status;
#define MAXSTRLEN 255 //用户可在255以内定义最长串长
typedef struct { //0号单元存放串的长度
char ch[MAXSTRLEN+1];
int length;
}SString;
Status StrAssign(SString &T, char *chars) { //生成一个其值等于chars的串T
int i;
if (strlen(chars) > MAXSTRLEN)
return ERROR;
else {
T.length = strlen(chars);
for (i = 1; i <= T.length; i++)
T.ch[i] = *(chars + i - 1);
return OK;
}
}
#define ERROR 0
#define OVERFLOW -2
typedef int Status;
#define MAXSTRLEN 255 //用户可在255以内定义最长串长
typedef struct { //0号单元存放串的长度
char ch[MAXSTRLEN+1];
int length;
}SString;
Status StrAssign(SString &T, char *chars) { //生成一个其值等于chars的串T
int i;
if (strlen(chars) > MAXSTRLEN)
return ERROR;
else {
T.length = strlen(chars);
for (i = 1; i <= T.length; i++)
T.ch[i] = *(chars + i - 1);
return OK;
}
}
//填写4.1 BF算法 并增加计算比较次数
int Index_BF(SString S, SString T, int pos)
{int i=pos,j=1,n=0;
while(i<=S.length&&j<=T.length)
{
if(S.ch[i]==T.ch[j]){i++;j++;}
else{i=i-j+2;j=1;}
n++;
}
cout<<"比较次数为:"<<n<<endl;
if(j>T.length) return i-T.length;
else return 0;
}
//算法4.3 计算next函数值
void get_next(SString T, int next[])
{ //求模式串T的next函数值并存入数组next
int i = 1, j = 0;
next[1] = 0;
while (i < T.length)
if (j == 0 || T.ch[i] == T.ch[j])
{
++i;
++j;
next[i] = j;
}
else
j = next[j];
}//get_next
int Index_BF(SString S, SString T, int pos)
{int i=pos,j=1,n=0;
while(i<=S.length&&j<=T.length)
{
if(S.ch[i]==T.ch[j]){i++;j++;}
else{i=i-j+2;j=1;}
n++;
}
cout<<"比较次数为:"<<n<<endl;
if(j>T.length) return i-T.length;
else return 0;
}
//算法4.3 计算next函数值
void get_next(SString T, int next[])
{ //求模式串T的next函数值并存入数组next
int i = 1, j = 0;
next[1] = 0;
while (i < T.length)
if (j == 0 || T.ch[i] == T.ch[j])
{
++i;
++j;
next[i] = j;
}
else
j = next[j];
}//get_next
//算法4.2 KMP算法,增加计算比较次数
int Index_KMP(SString S, SString T, int pos, int next[])
{ // 利用模式串T的next函数求T在主串S中第pos个字符之后的位置的KMP算法
//其中,T非空,1≤pos≤StrLength(S)
int i = pos, j = 1,n=0;
while (i <= S.length && j <= T.length)
{
if (j == 0 || S.ch[i] == T.ch[j]) // 继续比较后继字
{
++i;
++j;
}
else
j = next[j];
n++;
}// 模式串向右移动
cout<<"比较次数为:"<<n<<endl;
if (j > T.length) // 匹配成功
return i - T.length;
else
return 0;
}//Index_KMP
int main()
{
SString S;
StrAssign(S,"bbabababababba") ;
SString T;
StrAssign(T,"babb") ;
cout<<"调用BF算法:"<<endl;
cout<<"主串和子串在第"<<Index_BF(S,T,1)<<"个字符处首次匹配\n";
int *p = new int[T.length+1]; // 生成T的next数组
get_next(T,p);
cout<<"调用KMP算法:"<<endl;
cout<<"主串和子串在第"<<Index_KMP(S,T,1,p)<<"个字符处首次匹配\n";
return 0;
}
int Index_KMP(SString S, SString T, int pos, int next[])
{ // 利用模式串T的next函数求T在主串S中第pos个字符之后的位置的KMP算法
//其中,T非空,1≤pos≤StrLength(S)
int i = pos, j = 1,n=0;
while (i <= S.length && j <= T.length)
{
if (j == 0 || S.ch[i] == T.ch[j]) // 继续比较后继字
{
++i;
++j;
}
else
j = next[j];
n++;
}// 模式串向右移动
cout<<"比较次数为:"<<n<<endl;
if (j > T.length) // 匹配成功
return i - T.length;
else
return 0;
}//Index_KMP
int main()
{
SString S;
StrAssign(S,"bbabababababba") ;
SString T;
StrAssign(T,"babb") ;
cout<<"调用BF算法:"<<endl;
cout<<"主串和子串在第"<<Index_BF(S,T,1)<<"个字符处首次匹配\n";
int *p = new int[T.length+1]; // 生成T的next数组
get_next(T,p);
cout<<"调用KMP算法:"<<endl;
cout<<"主串和子串在第"<<Index_KMP(S,T,1,p)<<"个字符处首次匹配\n";
return 0;
}
阅读全文
0 0
- BF和KMP算法
- BF和KMP算法
- BF算法和KMP算法
- BF算法和 KMP 算法
- BF算法和KMP算法
- 【字符 BF 和 KMP算法】
- 字符串--BF和KMP算法
- C++ BF和KMP算法
- C++实现:BF算法和KMP算法
- BF算法和KMP算法对比
- 字符串匹配BF算法和KMP算法
- BF算法和KMP算法的简介
- 第五章 BF算法和KMP算法
- hdu 2087 BF 和 KMP算法
- 字符串匹配算法(BF和KMP)
- 模式匹配算法BF和KMP
- 字符串查找算法BF和KMP
- BF与KMP算法
- mongodb复制集安装
- 学习随记
- Spring Data Jpa实现分页查询
- net::err_unknown_url_scheme 使用ckplayer 插件播放视频报错
- Apache2.4报错: Invalid command 'Order', perhaps misspelled or defined by a module not included in the
- BF和KMP算法
- C++ Templates中文版 p61页的一个问题
- mybatis简介
- mybatis-one-many
- Android 使用类加载器原理实现热修复
- Java NIO:浅析I/O模型
- 统计串S中字符的种类和个数方法1
- Python学习笔记--基础篇1
- NLP大神推荐的机器学习入门书单(附大量百度网盘电子书)