串的模式匹配,c/c++描述

很好的视频链接
懒猫老师-数据结构-(15)KMP算法2-next数组(模式匹配,字符串匹配)_哔哩哔哩_bilibili
https://www.bilibili.com/video/BV1U7411f7CB/?spm_id_from=autoNext

以下是完整代码,见名知意的程序易于阅读和理解。

#include<iostream>
using namespace std;
#define MAXSIZE 100
#include<iomanip>
struct SequenceString {
	char data[MAXSIZE];
	int lengthString;
};

extern void strAssign(SequenceString& strDestinate, const char array[]);
extern void dispalyStr(SequenceString& str);
extern int bruteForce(SequenceString &strObject,SequenceString &strMatch);
extern void getNext(SequenceString & strMatch,int next[]);
extern void getNextVal(SequenceString& strMatch, int nextVal[]);
extern int kmpNext(SequenceString& strObject, SequenceString& strMatch,int next[]);
extern int kmpNextVal(SequenceString& strObject, SequenceString& strMatch,
	int nextVal[]);




int main() {
	int next[MAXSIZE], nextVal[MAXSIZE];
	SequenceString objectStr, matchStr;

	strAssign(objectStr,"abcabcdabcdeabcdefabcdefg");
	strAssign(matchStr,"abcdeabcdefab");
	
	cout << "objectStr : ";
	dispalyStr(objectStr);
	cout << "matchStr  : ";
	dispalyStr(matchStr);

	cout<<"brute force solution,match result : "<<bruteForce(objectStr,matchStr);
	cout << endl;

	getNext(matchStr,next);
	getNextVal(matchStr,nextVal);

	cout << "next[] element    : ";
	for (int i = 0; i < matchStr.lengthString; i++)
		cout << setw(4)<<next[i];
	cout << endl;

	cout << "nextVal[] element : ";
	for (int i = 0; i < matchStr.lengthString; i++)
		cout << setw(4)<<nextVal[i];
	cout << endl;

	cout << "KMP next solution : " << kmpNext(objectStr,matchStr,next)<<endl;
	cout << "KMP nextVal solution : " << kmpNextVal(objectStr,matchStr,nextVal);


	return 0;
}

以下是各函数的所在源文件:

#include<iostream>
using namespace std;
#define MAXSIZE 100

struct SequenceString {
	char data[MAXSIZE];
	int lengthString;
};

void strAssign(SequenceString& strDestinate, const char array[]) {
	int i;
	for ( i= 0; array[i] != '\0'; i++)
		strDestinate.data[i] = array[i];
	strDestinate.lengthString = i;
}
void dispalyStr(SequenceString& str) {
	for (int i = 0; i < str.lengthString; i++)
		cout << str.data[i];
	cout << endl;
	
}
int bruteForce(SequenceString& strObject, SequenceString& strMatch) {
	int iObject = 0, iMatch = 0;
	while (iObject < strObject.lengthString && iMatch < strMatch.lengthString) 
		if (strObject.data[iObject] == strMatch.data[iMatch]) {
			iObject++;
			iMatch++;
		}
		else {
			iObject = iObject - iMatch + 1;
			iMatch = 0;
		}
	if (iMatch >= strMatch.lengthString)
		return iObject = iObject - iMatch;
	else
		return -1;
}

void getNext(SequenceString& strMatch, int next[]) {
	int i = 0, iFront = -1;
	next[0] = -1;

	while (i < strMatch.lengthString) 
		if (iFront == -1 || strMatch.data[i] == strMatch.data[iFront]) {
			i++;
			iFront++;
			next[i] = iFront;
		}
		else
			iFront = next[iFront];
	
}

void getNextVal(SequenceString& strMatch, int nextVal[]) {
	int i = 0, iFront = -1;
	nextVal[0] = -1;
	while (i < strMatch.lengthString) 
		if (iFront == -1 || strMatch.data[i] == strMatch.data[iFront]) {
			i++;
			iFront++;
			if (strMatch.data[i] == strMatch.data[iFront])
				nextVal[i] = nextVal[iFront];
			else
				nextVal[i] = iFront;
		}
		else
			iFront = nextVal[iFront];
	
}
int kmpNext(SequenceString& strObject, SequenceString& strMatch, int next[]) {
	int iObject = 0, iMatch = 0;
	while (iObject < strObject.lengthString && iMatch < strMatch.lengthString)
		if (iMatch == -1 || strObject.data[iObject] == strMatch.data[iMatch]) {
			iObject++;
			iMatch++;
		}
		else
			iMatch = next[iMatch];
	if (iMatch >= strMatch.lengthString)
		return iObject - iMatch;
	else
		return -1;
}
int kmpNextVal(SequenceString& strObject, SequenceString& strMatch, int nextVal[]) {
	int iObject = 0, iMatch = 0;
	while (iObject < strObject.lengthString && iMatch < strMatch.lengthString) 
		if (iMatch == -1 || strObject.data[iObject] == strMatch.data[iMatch]) {
			iObject++;
			iMatch++;
		}
		else
			iMatch = nextVal[iMatch];

	if (iMatch >= strMatch.lengthString)
		return iObject - iMatch;
	else
		return -1;
}


测试结果是正确的。很早了,就不上截图了。
谢谢阅读。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

zhangzhangkeji

谢谢支持

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值