数据结构之顺序串(整理严蔚敏数据结构)

//顺序串可以依据顺序表来理解,只不过将处理单个元素改为处理一个串
#include<iostream>
using namespace std;
#define MAXSIZE 255
typedef unsigned char
SSTring[MAXSIZE + 1];//0号单元存储串长
void StrAssgin(SSTring &S, char *pA)
{
	int i = 1;
	S[0] = strlen(pA);//调用库函数
	while (i <= S[0])
	{
		S[i] = pA[i - 1];
		++i;
	}
}
int StrLength(SSTring S)
{
	return S[0];
}
void StrClear(SSTring &S)
{
	int i = 1;
	while (i <= S[0])
		S[i] = '/0';
	S[0] = 0;
}
bool StrEmpty(SSTring S)
{
	if (!S[0])
		return true;
	else
		return false;
}
void StrCopy(SSTring &T, SSTring S)
{
	int i = 0;
	while (i <= S[0])
	{
		T[i] = S[i];
		++i;
	}
}
int StrCompare(SSTring S, SSTring T)
{
	int i = 1;
	while (i <= S[0] && i <= T[0])
	{
		if (S[i] != T[i])
			return (T[i] - S[i]);
		else
			++i;
	}
	/*	if (i <= S[0])        如果加上的话该函数和StrEqual()等价
	return 1;
	else if (i <= T[0])
	return -1;
	else
	*/
	return 0;
}
bool StrEqual(SSTring S, SSTring T)
{
	int i = 1;
	if (S[0] != T[0])
		return false;
	else
	{
		while (i <= S[0])
		{
			if (S[i] != T[i])
				return false;
			else
				++i;
		}
		return true;
	}
}
bool StrEqual1(SSTring S, SSTring T)
{
	int i = 1;
	if (S[0] != T[0])
		return false;
	else
	{
		while (i <= S[0])
		{
			if (S[i] == T[i])
				++i;
			else
				return false;
		}
		return true;
	}
}
void StrConcat(SSTring &ST, SSTring S, SSTring T)
{
	int i;
	if (S[0] + T[0] <= MAXSIZE)
	{
		for (i = 1; i <= S[0]; ++i)
			ST[i] = S[i];
		for (i = 1; i <= S[0]; ++i)
			ST[i + S[0]] = T[i];
		ST[0] = S[0] + T[0];
	}
	else
	{
		if (S[0] = MAXSIZE)
		{
			StrCopy(ST, S);
		}
		else
		{
			StrCopy(ST, S);//后面应对ST[0]做修正
			for (i = 1; i <= MAXSIZE - S[0]; ++i)
				S[i + S[0]] = T[i];
			ST[0] = MAXSIZE;//对ST[0]做修正
		}
	}
}//涉及截断问题
bool SubStr(SSTring &Sub, SSTring S, int pos, int len)
{
	int i = 1;
	if (pos<1 || pos>S[0])
		return false;
	if (len<0 || len>S[0] - pos + 1)
		return false;
	Sub[0] = len;
	while (i <= Sub[0])
	{
		Sub[i] = S[i];
		++i;
	}
	return true;
}
int StrIndexBF(SSTring S, SSTring T, int pos)//简单匹配
{//i,j都有可能回缩
	int i, j;
	if (pos<1 || pos>S[0])//不合格,返回0
		return 0;
	else
	{
		i = pos; j = 1;
		while (i <= S[0] && j <= T[0])
		{
			if (S[i] == T[j])
			{
				++i; ++j;
			}
			else
			{
				i = i - j + 2; j = 1;
			}
		}
		if (j > T[0])
			return i - T[0];
		else
			return 0;
	}
}
int StrIndexBF1(SSTring S, SSTring T, int pos)
{
	int i = pos;
	SSTring Sub;
	if (pos<1 || pos>S[0])
		return 0;
	while (i <= S[0] - T[0] + 1)
	{
		SubStr(Sub, S, i, T[0]);
		if (!StrEqual(Sub, T))
			++i;
		else
			return i;
	}
	return 0;
}
int StrIndexSE(SSTring S, SSTring T, int pos)//首尾匹配法
{//i不回缩,j,k可能回缩
	int i = pos, j, k;
	if (pos<1 || pos>S[0])
		return 0;
	char st = T[1]; char en = T[T[0]];
	while (i <= S[0] - T[0] + 1)
	{
		if (S[i] != st)
			++i;
		else if (S[i + T[0] - 1] != en)
			++i;
		else
		{
			k = 1; j = 2;
			while (j < T[0] && S[i + k] == T[j])
			{
				++k; ++j;
			}
			if (j == T[0])
				return i;
			else
				++i;
		}
	}
	return 0;
}
/*******************KMP(模式匹配)*********************/
void GetNext(SSTring T, int Next[])
{
	int i = 1, j = 0;
	Next[1] = 0;
	while (i < T[0])
	{
		if (j == 0 || T[i] == T[j])
		{
			++i; ++j;
			Next[i] = j;
		}
		else
			j = Next[j];
	}
}
int StrIndexKMP(SSTring S, SSTring T, int pos)
{
	int Next[255];
	int i = pos, j = 1;
	if (pos<1 || pos>S[0])
		return 0;
	GetNext(T, Next);
	while (i <= S[0] && j <= T[0])
	{
		if (j == 0 || S[i] == T[j])
		{
			++i; ++j;
		}
		else
			j = Next[j];
	}
	if (j > T[0])
		return i - T[0];
	else
		return 0;
}
/*******************************************************/
/******************KMP(改进后)**************************/
void GetNextN(SSTring T, int NextN[])
{

	int i = 1;
	int j = 0;
	NextN[1] = 0;
	while (i<T[0])
	{
		if (j == 0 || T[i] == T[j])
		{
			++i;  ++j;
			if (T[i] != T[j])
				NextN[i] = j;
			else
				NextN[i] = NextN[j];
		}
		else j = NextN[j];
	}
}
int Index_KMP(SSTring S, SSTring T, int pos)
{
	int NextN[255];
	int i = pos;
	int j = 1;
	GetNextN(T, NextN);
	while (i <= S[0] && j <= T[0])
	{
		if (j == 0 || S[i] == T[j])
		{
			++i;  ++j;
		}
		else
			j = NextN[j];
	}
	if (j>T[0])
		return i - T[0];
	else
		return 0;
}
/********************************************************/
bool StrInsert(SSTring &S, int pos, SSTring T)//涉及截断问题
{//把各种可能性考虑进去
	int i;
	if (pos<1 || pos> S[0] + 1)
		return false;
	if (S[0] + T[0] <= MAXSIZE)//完全插入
	{
		for (i = S[0]; i >= pos; --i)//将S[pos]->S[S[0]]置后
			S[i + T[0]] = S[i];
		for (i = pos; i < pos + T[0]; ++i)//插入T
			S[i] = T[i - pos + 1];
		S[0] = S[0] + T[0];
		return true;
	}
	else//部分插入
	{
		for (i = MAXSIZE; i >= pos; --i)
			S[i] = S[i - T[0]];
		for (i = pos; i < pos + T[0]; ++i)
			S[i] = T[i - pos + 1];
		S[0] = MAXSIZE;
		return false;
	}
}
bool StrDelete(SSTring &S, int pos, int len)
{
	int i;
	if (pos < 1 || len<0 || pos>S[0] - len + 1)
		return false;
	for (i = pos + len; i < S[0]; ++i)
		S[i - len] = S[i];
	//加上将S[pos+len]至S[0]抹去的算法为好!
	S[0] -= len;
	return true;
}
bool StrReplace(SSTring &S, SSTring T, SSTring V)
{
	int i = 1;
	if (!T[0])
		return false;
	do
	{
		i = StrIndexBF(S, T, i);
		if (i)//S中存在T
		{
			StrDelete(S, i, StrLength(T));
			StrInsert(S, i, V);
			i += StrLength(V);
		}
	} while (i);
	return true;
}
void StrPrint(SSTring S)
{
	int i = 1;
	while (i <= S[0])
		cout << S[i++];
	cout << endl;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值