字符串匹配BF算法

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/isluckyguo/article/details/83743188

BF算法是朴素的字符串匹配算法,说是朴素其实就是效率低下。

#include <stdio.h>
#include <string>

using namespace std;

int index(string S, string T, int pos)
{
	int i = pos;
	int j = 0;

	while (i < S.size() && j < T.size())
	{
		if (S[i] == T[j])
		{
			i++;
			j++;
		}
		else
		{
			i++;
			j = 0;
		}
	}

	if (j >= T.length())
		return i - T.size();
	else
		return 0;
}

int main()
{
	string S = "iwillbethebest man";
	string T = "bethebest";

	int pos = 0;
	int addr = 0;
	addr = index(S, T, pos);

	return 0;
}

猜你喜欢

转载自blog.csdn.net/isluckyguo/article/details/83743188