串的类型定义及存储结构及运算

#include<iostream>
using namespace std;
#define maxlen 255
#define chunksize 80
typedef struct
{
	char ch[maxlen + 1];//ch[0]不存字符
	int length;
}sstring;//串的顺序存储结构(常用)
typedef struct chunk //串的链式存储
{
	char ch[chunksize];
	struct chunk* next;
}chunk;
typedef struct
{
	chunk* head, * tail;//串的头指针和尾指针
	int curlen;//串的当前长度
}Lstring;//字符串的块链结构
//BF算法
int index_bf(sstring s, sstring t,int pos)//从pos处开始找
{
	int i = pos, 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;//(即失败) 
	}
}
//KMP算法
void get_next(sstring t, int*& next)
{
	int i = 1;
	next[1] = 0;
	int j = 0;
	while (i < t.length)
	{
		if (j == 0 || t.ch[i] == t.ch[j])
		{
			i++;j++;
			next[i] = j;
		}
		else
		{
			j = next[j];
		}	
	}
}
void get_nextval(sstring t, int*& nextval)
{
	int i = 1;
	nextval[1] = 0;
	int j = 0;
	while (i < t.length)
	{
		if (j == 0 || t.ch[i] == t.ch[j])
		{
			i++;
			j++;
			if (t.ch[i] != t.ch[j])
			{
				nextval[i] = j;
			}
			else
			{
				nextval[i] = nextval[j];
			}
		}
		else
		{
			j = nextval[j];
		}
	}
}
int index_kmp(sstring s, sstring t, int pos)//从pos处开始找
{
	int i = pos, j = 1;
	int* next = new int[t.length];
	get_next(t, next);
	while (i <= s.length && j <= t.length)
	{
		if (j == 0||s.ch[i]==t.ch[j])
		{
			i++;
			j++;
		}
		else
		{
			j = next[j];
		}
	}
	if (j > t.length)
	{
		return i - t.length;
	}
	else
	{
		return 0;//(即失败) 
	}
}












int main()
{
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值