数据结构03
3.串
串:0个或者多个字符组成的有限序列
3.1 串的顺序存储
第一个位置:存放串的长度
3.1.1 StrAssign(char dest[],char *ps)
生成一个 = 字符串常量ps的串 dest
Status StrAssign (char dest[],char *ps)
{
int i = 1;
int dest[0] = strlen(ps);
while (i <=dest[0])
{
dest[i] = ps[i-1];
i++;
}
return OK;
}
3.1.2 StrCopy(char dest[],char src[])
将src中的内容copy到dest中
Status StrCpy (char dest[],char src[])
{
dest[0] = src[0];
int i = 1;
while (i<dest[0])
{
dest[i]=src[i];
i++;
}
return OK;
}
3.1.3 ClearString(char dest[])
清空串
Status ClearString (char dest[])
{
dest[0] = dest[1] = 0;
return OK;
}
3.1.4 StringIsEmpty
Status StringIsEmpty (char dest[])
{
return dest[0]==0;
}
3.1.5 StringLength
int StringLength (char dest[])
{
return dest[0];
}
3.1.6 StrCompare(char dest[],char src[])
比较dest和src中的字符大小
int StrCompare (char dest[],char src[])
{
int i = 1;
for (i;i<=dest[0]&&i<=src[0];i++)
{
if (dest[i]!=src[i])
{
return dest[i] - src[i];
}
}
if (i>src[0] || i>dest[0])
{
return dest[0] - src[0];
}
}
3.1.7 Concat(char dest[],char src1[],char src2[])
将src1和src2中的内容连接起来,再放入dest
Status Concat (char dest[],char src1[],char src2)
{
int i = 1;
if (src1[0] + src2[0] <= MAXSIZE) // 完全嵌入
{
dest[0] = src1[0] + src2[0];
for (i;i<=src1[0];i++)
{
dest[i] = src1[i];
}
for (i;i<=dest[0];i++)
{
dest[i] = src2[i+src1[0]];
}
return OK;
}
else // 部分嵌入
{
dest[0] = MAXSIZE;
for (i;i<=stc1[0];i++)
{
dest[i] = src1[i];
}
for (i;i<=MAXSIZE;i++)
{
dest[i] = src2[i-src1[0]];
}
return ERROR;
}
}
3.1.8 SubString(char dest[],char src[],int pos,int len)
dest中存放src中第pos个字符起长度为len的字符串
Status SubString (char dest[],char src[],int pos,int len)
{
if (StringIsEmpty(src)) // src中无字符
{
return ERROR;
}
if (pos > 1 || pos >src[0] || len > src[0] - pos +1) // 异常处理
{
return ERROR;
}
int i = pos;
dest[0] = len;
while (i <= src[0])
{
dest[i - pos +1] = src[i];
}
return OK;
}
3.1.9 Index(char dest[],char src[],int pos)
返回src在dest中pos个字符之后第一次出现的位置,如果不存在返回0
Status Index(char dest[],char src[],int pos)
{
if (pos < 1 || pos > dest[0])
{
return ERROR;
}
int i = pos;
int j = 1;
for (i;i<=dest[0];i++)
{
for (j;j<=src[0];j++)
{
if (dest[i] = src[j])
{
i++;
j++;
}
else
{
i = i - j + 2;
j = 1;
}
}
}
if (j > src[0])
{
return i - src[0];
}
else
{
return 0;
}
}
3.1.10 StrInsert(char dest[], int pos ,char src[])
在dest的(pos,pos + src[0])位置插入src
Status StrInsert(char dest[], int pos ,char src[])
{
int i;
if (pos < 1 || pos > dest[0])
{
return ERROR;
}
if (pos + src[0] <= MAXSIZE) // 完全插入
{
i = pos;
dest [0] = pos + src[0];
for (i;i<=dest[0];i++)
{
dest[i] = src[i-pos];
}
return OK;
}
else // 部分插入
{
i = pos;
dest[0] = MAXSIZE;
for (i;i<=dest[0];i++)
{
dest[i] = src[i-pos];
}
return ERROR;
}
}
// 部分插入 完全插入的代码虽然一样 但是含义却不同
3.1.11 StrDelete(char dest[],int pos,int len)
从dest删除第pos位置起长度为len的字符串
Starus StrDelete(char dest[],int pos,int len)
{
int i = pos;
if (pos < 1 || pos > dest[0])
{
return ERROR;
}
dest[0] = dest[0] -len;
for (i;i <= pos + len; i++)
{
dest[i] = dest[i+len];
}
return OK;
}
3.2 KMP
3.1.9中匹配字符采用BF算法:
i回溯:i = i - j + 2
j回溯:j = 1
KMP : 避免BF算法中不必要的回溯,节省空间 时间
3. 2. 1 next数组
void get_next (char T[],int *next)
{
int i = 1;
int 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];
}
}
}
3 . 2. 2 Index_KMP
int index_KMP (char dest[],char src[],int pos)
{
int next[255];
get_next(src,next);
int i = pos;
int j = 1;
while (i<=dest[0]&&j<=src[0])
{
if (j == 0 || dest[i]==src[j])
{
i++;
j++;
}
else
{
j = next[j];
}
}
if (j>src[0])
{
return i - src[0];
}
else
{
return -1;
}
}