数据结构实现顺序串的各种模式匹配算法
注意:本代码为了测试运行默认含有操作所需数据,如有需要可自己增删改相关数据
涉及基本运算流程
- 建立目标串s=abcabcdabcdeabcdeabcdefabcdefg 和模式串 t =abcdeabcdefab
- 采用简单匹配算法求t在s中的位置
- 由模式串t求出next数组值和nextval数组值
- 采用KMP算法求t在s中的位置
- 采用改进的KMP算法t在s中的位置
GitHub地址(包含.cpp文件和可执行程序exe)
源代码(经VS2015、devC++编译器运行通过)
#include "string.h"
#include "stdio.h"
#include "stdlib.h"
#include "io.h"
#include "math.h"
#include "time.h"
#define OK 1
#define ERROR 0
#define TRUE 1
#define FALSE 0
#define MAXSIZE 100 /* 存储空间初始分配量 */
typedef int Status; /* Status是函数的类型,其值是函数结果状态代码,如OK等 */
typedef int ElemType; /* ElemType类型根据实际情况而定,这里假设为int */
typedef char String[MAXSIZE + 1]; /* 0号单元存放串的长度 */
/* 生成一个其值等于chars的串T */
Status StrAssign(String T, char *chars)
{
int i;
if (strlen(chars)>MAXSIZE)
return ERROR;
else
{
T[0] = strlen(chars);
for (i = 1; i <= T[0]; i++)
T[i] = *(chars + i - 1);
return OK;
}
}
Status ClearString(String S)
{
S[0] = 0;/* 令串长为零 */
return OK;
}
/* 输出字符串T。 */
void StrPrint(String T)
{
int i;
for (i = 1; i <= T[0]; i++)
printf(