horspool算法

BM算法的简化版

后缀匹配。

若不匹配,那么跳转为主串最后一个字符在模式串中出现的最右位置,若没出现,那么直接跳转模式串的长度。

int horspool(char* T,char* P)
{
	int len_p = strlen(P);
	int len_t = strlen(T);
	int B[256] = {0};
	for(int i = 0 ; i < 256 ; i++) B[i] = len_p;
	for(int i = 0 ; i < len_p ; i++)
		B[P[i]] = len_p - i - 1;
	int pos = 0;
	while(pos <= len_t - len_p)
	{
		int j = len_p - 1;
		while(j >=0 && T[pos+j] == P[j]) j--;
		if(j < 0) return pos;
		pos = pos + B[T[pos+len_p]];
	}
	return -1;
}

  

by 1957
原文地址:https://www.cnblogs.com/x1957/p/3105960.html