Brute-Force经典算法和KMP算法的优劣

写程序比较Brute-Force算法和KMP算法的效果。

SeqString.h

#include<stdio.h>
#include<malloc.h>
#include<stdlib.h>
typedef char DataType;
#define MAXLEN 	100
typedef struct Squeue{
	DataType str[MAXLEN];
	int len;
}SeqString;
//串的赋值
void StrAssign(SeqString *S,DataType cstr[]){
	int i=0;
	for(i=0;cstr[i]!='\0';i++){
		S->str[i]=cstr[i];
	}
	S->len=i;
} 
//判断串是否为空
int StrEmpty(SeqString S){
	if(S.len==0)
		return 1;
	else
		return 0; 
} 
//串的长度
int StrLength(SeqString S){
	return S.len;
} 
//串的复制
void StrCopy(SeqString *T,SeqString S){
	int i;
	for(i=0;i<S.len;i++){
		T->str[i]=S.str[i];
	}
	T->len=S.len;
} 
//串的比较操作
int StrCompare(SeqString S,SeqString T){
	int i;
	for(i=0;i<S.len;i++){
		if(S.str[i]!=T.str[i]){
			return (S.str[i]-T.str[i]);
		}
	}
	return (S.len-T.len);
} 
//串的插入操作
int StrInsert(SeqString *S,int pos,SeqString T){
	int i;
	if(pos<0||pos-1>S->len){
		printf("插入位置不正确\n");
		return 0; 
	}
	//第一种情况,插入子串后串长<=MAXLEN,即子串T完整地插入到串S中 
	if(S->len+T.len<=MAXLEN){
		for(i=S->len+T.len-1;i>=pos+T.len-1;i--){
			S->str[i]=S->str[i-T.len];
		}
		for(i=0;i<T.len;i++){
			S->str[pos+i-1]=T.str[i];
		}
		S->len=S->len+T.len;
		return 1;
	}
	//第2种情况,子串可以完全插入到S中,但是S中的字符将会被截掉 
	else if(pos+T.len<=MAXLEN){
		for(i=MAXLEN-1;i>T.len+pos-1;i--){
			S->str[i]=S->str[i-T.len];
		}
		for(i=0;i<T.len;i++){
			S->str[pos+i-1]=T.str[i];
		}
		S->len=MAXLEN;
		return 0;
	}
	//第3种情况,子串T不能被完全插入到S中,T中将会有字符被舍弃 
	else{
		for(i=0;i<MAXLEN-pos;i++){
			S->str[pos+i-1]=T.str[i];
		}
		S->len=MAXLEN;
		return 0;
	}
} 
//串的删除
int StrDelete(SeqString *S,int pos,int len){
	int i;
	if(pos<0||len<0||pos+len-1>S->len){
		printf("删除位置不正确\n");
		return 0; 
	}else{
		for(i=pos+len-1;i<S->len-1;i++){
				S->str[i-len]=S->str[i];
		}
		S->len=S->len-len;
		return 1;
	}
} 
//串的连接操作
int StrConcat(SeqString *T,SeqString S){
	int i,flag;
	//第一种情况,连接后的串长小于等于 MAXLEN,将S直接连接在串T的末尾
	if(T->len+S.len<=MAXLEN){
		for(i=T->len;i<T->len+S.len;i++){
			T->str[i]=S.str[i-T->len];
		}
		T->len=T->len+S.len;
		flag=1;
	}
	//第二种情况,连接后的串长大于MAXLEN,S部分被连接在串T的末尾 
	else{
		for(i=T->len;i<MAXLEN;i++){
			T->str[i]=S.str[i-T->len];
		}
		T->len=MAXLEN;
		flag=0;
	}
	return flag; 
} 
//求子串操作
int SubString(SeqString *Sub,SeqString S,int pos,int len){
	int i;
	if(pos<0||len<0||pos+len-1>S.len){
		printf("参数不合法\n");
		return 0; 
	}else{
		for(i=0;i<len;i++){
			Sub->str[i]=S.str[i+pos-1];
		}
		Sub->len=len;
		return 1;
	}
} 
//串的定位操作
int  StrIndex(SeqString S,int pos,SeqString T){
	int i,j;
	if(StrEmpty(T))
		return 0;
	i=pos;
	j=0;
	while(i<S.len&&j<T.len){
		if(S.str[i]==T.str[j]){
			i++;
			j++;
		}else{
			i=i-j+1;
			j=0;
		}
	}
	if(j>=T.len)
		return i-j+1;
	else 
		return -1;
}
//串的替换操作
int StrReplace(SeqString *S,SeqString T,SeqString V){
	int i=0;
	int flag;
	if(StrEmpty(T))
		return 0;
	do{
		i=StrIndex(*S,i,T);
		if(i){
			StrDelete(S,i,StrLength(T));
			flag=StrInsert(S,i,V);
			if(!flag){
				return 0;
			}
			i+=StrLength(V);
		}
	}while(i);
	return 1;
} 
//清空串
void StrClear(SeqString *S){
	S->len=0;
}

算法.cpp

#include<stdio.h>
#include<malloc.h>
#include<stdlib.h>
#include"SeqString.h"
int B_FIndex(SeqString S,int pos,SeqString T,int *count);
int KMP_Index(SeqString S,int pos,SeqString T,int next[],int *count);
int GetNext(SeqString T,int next[]);
int GetNextVal(SeqString T,int nextval[]);
void PrintArray(SeqString T,int next[],int nextval[],int len);
int main(){
	SeqString S,T;
	int count1=0,count2=0,count3=0,find;
	int next[40],nextval[40];
	//第一个比较例子
	StrAssign(&S,"abaababaddecab");//给主串S赋值 
	StrAssign(&T,"abad");//给模式串T赋值 
	GetNext(T,next);//将next的函数值保存在next数组中
	GetNextVal(T,nextval);//将改进后的next函数值保存在nextval数组
	printf("模式串T的next和改进后的next值:\n");
	PrintArray(T,next,nextval,StrLength(T));//输出模式串T的next值和nextval值
	find=B_FIndex(S,1,T,&count1);//传统的模式串匹配 
	if(find>0){
		printf("Brue-Force算法的比较次数为:%2d\n",count1);
	} 
	find=KMP_Index(S,1,T,next,&count2);
	if(find>0){
		printf("利用next的KMP算法的比较次数为:%2d\n",count2);
	}
	find=KMP_Index(S,1,T,nextval,&count3);
	if(find>0){
		printf("利用nextval的KMP算法的比较次数为:%2d\n",count3);
	}
	
	//第二个比较例子
	 StrAssign(&S,"cbccccbcacbccbacbccbcbcbc");//给主串S赋值 
	StrAssign(&T,"cbccbcbc");//给模式串T赋值 
	GetNext(T,next);//将next的函数值保存在next数组中
	GetNextVal(T,nextval);//将改进后的next函数值保存在nextval数组
	printf("模式串T的next和改进后的next值:\n");
	PrintArray(T,next,nextval,StrLength(T));//输出模式串T的next值和nextval值
	find=B_FIndex(S,1,T,&count1);//传统的模式串匹配 
	if(find>0){
		printf("Brue-Force算法的比较次数为:%2d\n",count1);
	} 
	find=KMP_Index(S,1,T,next,&count2);
	if(find>0){
		printf("利用next的KMP算法的比较次数为:%2d\n",count2);
	}
	find=KMP_Index(S,1,T,nextval,&count3);
	if(find>0){
		printf("利用nextval的KMP算法的比较次数为:%2d\n",count3);
	}
	return 1;
}
void PrintArray(SeqString T,int next[],int nextval[],int len){
	int j;
	printf("j:\t\t");
	for(j=0;j<len;j++){
		printf("%3d",j);
	}
	printf("\n");
	printf("模式串:\t\t");
	for(j=0;j<len;j++){
		printf("%3c",T.str[j]);
	}
	printf("\n");
	printf("next[j]:\t");
	for(j=0;j<len;j++){
		printf("%3d",next[j]);
	} 
	printf("\n");
	printf("nextval[j]:\t");
	for(j=0;j<len;j++){
		printf("%3d",nextval[j]);
	} 
	printf("\n");
}
//Brute-Force算法 
int B_FIndex(SeqString S,int pos,SeqString T,int *count){
	int i,j;
	i=pos-1;
	j=0;
	*count=0;
	while(i<S.len&&j<T.len){
		if(S.str[i]==T.str[i]){
			i++;
			j++;
		}else{
			i=i-j+1;
			j=0;
		}
		(*count)++;
	}
	if(j>=T.len){
		return i-j+1;
	}else{
		return -1;
	}
}
//KMP算法 
int KMP_Index(SeqString S,int pos,SeqString T,int next[],int *count){
	int i,j;
	i=pos-1;
	j=0;
	*count=0;
	while(i<S.len&&j<T.len){
		if(j==-1||S.str[i]==T.str[j]){
			i++;
			j++;
		}else{
			j=next[j];
		}
		(*count)++;
	}
	if(j>=T.len){
		return i-T.len+1;
	}else{
		return -1;
	}
}
//改进算法
int GetNext(SeqString T,int next[]){
	int j,k;
	j=0;
	k=-1;
	next[0]=-1;
	while(j<T.len){
		if(k==-1||T.str[j]==T.str[k]){
			j++;
			k++;
			next[j]=k;
		}else{
			k=next[k];
		}
	}
}
int GetNextVal(SeqString T,int nextval[]){
	int j,k;
	j=0;
	k=-1;
	nextval[0]=-1;
	while(j<T.len){
		if(k==-1||T.str[j]==T.str[k]){
			j++;
			k++;
			if(T.str[j]!=T.str[k]){
				nextval[j]=k;
			}else{
				nextval[j]=nextval[k];
			}
		}else{
			k=nextval[k];
		}
	}
}

运行结果
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值