bf串

版权声明:lixiang666 https://blog.csdn.net/weixin_43838785/article/details/89891641
#include <iostream>
using namespace std;
#define maxlen 255
typedef struct {
	char ch[maxlen + 1];
	int length;
}sstring;
//顺序
typedef struct {
	char *ch;//若是非空串,则按串长分配存储区,否则ch为NULL
	int lenth;
}hstring;
//链式
/*
#define chunksize 80
typedef struct chunk {
	char ch[chunksize];
	chunk *next;
}chunk;
typedef struct {
	chunk *head, *tail;
	int length;
}lstring;*/
//BF i-j+2  i-t.length
int index_bf(sstring s, sstring t, int pos)
{
	int i = pos;
	int j = 1;
	while (i<=s.length&&j<=t.length)
	{
		if (s.ch[i] == t.ch[j])
		{
			i++;
			j++;
		}
		else
		{
			i = i - j + 2;
			j = 1;
		}
	}
	if (j> t.length)
		return i - t.length;
	else
		return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_43838785/article/details/89891641