//基于线性表的定义所做的更改
#include
using namespace std;
#include//存放exit
#include//OVERFLOW,exittypedef int Status;
#define MAXLEN 255struct SString//Sequence String
{char ch[MAXLEN + 1]; //存储串的一维数组int length; //串的当前长度长度
};
//链串的定义及其基础操作
#include
using namespace std;
#include//存放exit#define CHUNKSIZE 80//块的大小可由用户定义
typedef int Status; //函数调用状态struct Chunk//厚块; 厚片; 大块; 组块;
{char ch[CHUNKSIZE];Chunk* next;
};struct LString
{Chunk* head, * tail;int curlen;//current length:当前长度
};int main()
{}
(1) :StrAssign (&T,chars)
串赋值
(2): StrCompare(S,T)
串比较
(3) :Strlength (S)
求串长
(4) :Concat(&T,S1,S2)
串连结
(5): SubString(&Sub,S,pos,len)
求子串
(6): StrCopy(&T,s)
串拷贝
(7) :StrEmpty(S)
//串判空(8) :ClearString (&S)
清空串
(9) :Index(S,T,pos)//子串的位置
(11) :Replace(&S,T,V)串替换
(12) :Strlnsert(&S,pos,T)
//子串插入(12) :StrDelete(&S,pos,len)
//子串删除
(13) :DestroyString(&S)
//串销毁
brute force:蛮力;暴力;强力
int Index_BF(SString S, SString T, int 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;//模式匹配不成功}
}
注解:
(1):
index:序号,编号
(物价和工资等的)指数;指标;索引;标志;
vt.:为…编索引;将…编入索引;
(2):
pos:从位置pos(第pos位)开始(向后)检索
(3):
判断(主串和子串有一个串)检索完了跳出循环的语句不是判断内容是否为空
(实际上电脑系统内部可能随机分配数值,即使我们造了一个该类型的空值也无法实际奏效)
而是: while (i <= S.length && j <= T.length)
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[j] = j;}elsej = next[j];}
}int Index_KMP(SString S, SString T, int pos)
{int i = pos, j = 1;while (i <= S.length && j <= T.length){if (S.ch[i] == T.ch[j]){++i; ++j;}//主串和子串依次匹配下一个字符else//get_next(T, next[j]);j = next[j]; }if (j > T.length) return i - T.length; //匹配成功else return 0;
}
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;elsenextval[i] = nextval[j];}else j = nextval[j];}
}
该算法详细研究过程后续将重新整理,详见: