编程语言
首页 > 编程语言> > BF算法模式匹配

BF算法模式匹配

作者:互联网

#include<iostream>
using namespace std;
int findstr(char* mainstr, char* sonstr);
int main() {

	char mainstr[100], sonstr[100];
	while (1)
	{
		cin >> mainstr >> sonstr;
		int set = findstr(mainstr, sonstr);
		if (set == -1)
			cout << "匹配失败" << endl;
		else
		cout <<sonstr<<"位于"<<mainstr<<"的第"<< set+1 <<"位"<< endl;
	}
	return 0;
}
int findstr(char* mainstr, char* sonstr)
{
	int mainlen = strlen(mainstr), sonlen = strlen(sonstr), i = 0, j = 0;
	while (i < mainlen && j < sonlen)
	{
		if (mainstr[i] == sonstr[j])
		{
			i++;
			j++;
		}
		else
		{
			i = i - j + 1;
			j = 0;
		}
	}
	if (i >= mainlen)
		return -1;
	else
		return i - j;
}

标签:BF,set,sonstr,int,mainlen,mainstr,char,算法,模式匹配
来源: https://blog.csdn.net/m0_52179617/article/details/122390199