在计算机上,除了数据类型,其余皆是字符串,可以说,在开发当中用到的最多的就是字符串的处理,串有很多基本操作,剔除一些简单的基操,稍有含量的就是串的匹配算法了,也就是我们经常使用的index函数,我们今天介绍常用的两种串匹配算法(暴力法跟KMP算法)以及KMP的优化
废话不多说,讲一下思路:
一:暴力匹配法
思路分析
暴力模式匹配算法就是逐个比较,一步一步回溯比较的过程。
最坏时间复杂度为O(mn) ,其中n和m分别为主串和模式串的长度。
过程图
代码
#include<iostream>
#include<stdlib.h>
using namespace std;
#define maxsize 1000
typedef struct {
char ch[maxsize];
int length;
}SString;
void showStr(SString S){
for(int i=1;i<=S.length;i++){
cout<<S.ch[i];
}
}
void init(SString &s){
int n;
cin>>n;
// s=(SString)malloc(sizeof(int));
s.length=0;
for(int i=1;i<=n;i++){
cin>>s.ch[++s.length];
}
}
int index(SString S,SString T){
int i=1;int j=1;
while(i<=S.length&&j<=T.length){
if(S.ch[i]==T.ch[j]){
i++;j++;
}
else{
i=i-j+2;
j=1;
}
}
if(j>T.length){
return i-T.length;
}
else{
return -1;
}
}
main(){
SString str1;
SString str2;
init(str1);
init(str2);
showStr(str1);
cout<<"中";
showStr(str2);
cout<<"的位置在"<<index(str1,str2);
}
运行结果
在这里插入图片描述 第一次写博客,有不完备的地方欢迎指责哟在这里插入图片描述
二:KMP算法
思路分析
KMP算法就是在回溯之前得到所谓的next数组,利用空间换时间的方式减少回溯的次数,next数组对应元素的值就是j下标在匹配失败之后回溯的目标,得到next数组之后的算法与暴力法相差无几,KMP的重点就是求next数组
过程图
代码
#include<iostream>
#include<stdlib.h>
using namespace std;
#define maxsize 1000
typedef struct {
char ch[maxsize];
int length;
}SString;
int next[maxsize];
void showStr(SString S){
for(int i=1;i<=S.length;i++){
cout<<S.ch[i];
}
}
void init(SString &s){
int n;
cin>>n;
// s=(SString)malloc(sizeof(int));
s.length=0;
for(int i=1;i<=n;i++){
cin>>s.ch[++s.length];
}
}
void getNext(SString T,int next[]){
int i=1;
int j=0;
next[1]=0;//规定
while(i<=T.length){
if(j==0||T.ch[i]==T.ch[j]){
i++;
j++;
next[i]=j;//若pi=pj 则next[j+1]=next[j]+1
}
else{
j=next[j];//否则令j=next[j]循环继续
}
}
}
int indexKMP(SString S,SString T,int next[]){
int i=1,j=1;
while(i<=S.length&&j<=T.length){
//多了一个j==0
if(S.ch[i]==T.ch[j]||j==0){
i++;
j++;
}
else{
//i不动,j不回溯,j=next[j]
j=next[j];
}
}
//匹配成功
if(j>T.length){
return i-T.length;
}
return 0;
}
main(){
SString str1;
SString str2;
init(str1);
init(str2);
getNext(str2,next);
showStr(str1);
cout<<"中";
showStr(str2);
cout<<"的位置在"<<indexKMP(str1,str2,next);
}
运行结果
三:KMP的优化
思路分析
KMP的优化就是根据next数组计算出nextVal数组,进一步减少回溯次数,对于模式串中重复的字符,在next数组基础上将值变为该字符第一次出现对应的next元素值
过程图
代码
读者注意,优化之后的代码与KMP代码不同之处有二
1、把所有next替换成了nextVal
2、getNext函数中多了一步for循环,用于next向nextVal的转换
#include<iostream>
#include<stdlib.h>
using namespace std;
#define maxsize 1000
typedef struct {
char ch[maxsize];
int length;
}SString;
int nextVal[maxsize];
void showStr(SString S){
for(int i=1;i<=S.length;i++){
cout<<S.ch[i];
}
}
void init(SString &s){
int n;
cin>>n;
// s=(SString)malloc(sizeof(int));
s.length=0;
for(int i=1;i<=n;i++){
cin>>s.ch[++s.length];
}
}
void getNext(SString T,int nextVal[]){
//先求next数组
int i=1;
int j=0;
nextVal[1]=0;//规定
while(i<=T.length){
if(j==0||T.ch[i]==T.ch[j]){
i++;
j++;
nextVal[i]=j;//若pi=pj 则nextVal[j+1]=nextVal[j]+1
}
else{
j=nextVal[j];//否则令j=nextVal[j]循环继续
}
}
//将求得的next数组转换成nextVal 进一步优化算法
// 从前向后递推即可
for(int x=2;x<=T.length;x++){
if(T.ch[nextVal[x]]==T.ch[x]){
nextVal[x]=nextVal[nextVal[x]];
}
}
}
int indexKMP(SString S,SString T,int nextVal[]){
int i=1,j=1;
while(i<=S.length&&j<=T.length){
//多了一个j==0
if(S.ch[i]==T.ch[j]||j==0){
i++;
j++;
}
else{
//i不动,j不回溯,j=nextVal[j]
j=nextVal[j];
}
}
//匹配成功
if(j>T.length){
return i-T.length;
}
return 0;
}
main(){
SString str1;
SString str2;
init(str1);
init(str2);
getNext(str2,nextVal);
showStr(str1);
cout<<"中";
showStr(str2);
cout<<"的位置在"<<indexKMP(str1,str2,nextVal);
}
有关串的模式匹配大概就这么多,错的地方还请读者指点,不胜感激。
加油,为了你的老鼠