1. 程式人生 > >字串匹配BF演算法

字串匹配BF演算法

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;
}