目次
文字列定義 (前提条件):
シーケンス文字列: (一般的に使用される)
//基于线性表的定义所做的更改
#include<iostream>
using namespace std;
#include<stdlib.h>//存放exit
#include<math.h>//OVERFLOW,exit
typedef int Status;
#define MAXLEN 255
struct SString
//Sequence String
{
char ch[MAXLEN + 1]; //存储串的一维数组
int length; //串的当前长度长度
};
チェーンストリング:
//链串的定义及其基础操作
#include<iostream>
using namespace std;
#include<stdlib.h>//存放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) :長さ(S)
文字列の長さを調べる
(4) :連結(&T,S1,S2)
文字列接続
(5): サブストリング(&Sub,S,pos,len)
部分文字列を検索
(6): StrCopy(&T,s)
文字列のコピー
(7) : StrEmpty(S)
//空の文字列(8) :クリアストリング(&S)
空の文字列
(9) :インデックス(S,T,pos)// 部分文字列の位置
(11) :置換(&S,T,V)文字列の置換
(12):Strlnsert(&S,pos,T)
//部分文字列の挿入(12) :StrDelete(&S,pos,len)
//部分文字列の削除
(13): DestroyString(&S)
//文字列の破壊
BFアルゴリズム:
ブルートフォース: 残忍な力; 暴力; 力ずく
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):
インデックス:シリアル番号、シリアル番号
(物価や賃金などの)指標; 指標; 指標; 符号;
vt.: インデックス作成; インデックス作成;
(2):
pos : 位置 pos から (逆方向に) 取得します (位置 pos)
(3):
判定(主文字列と部分文字列に文字列がある) 検索後にループから抜ける文は内容が空かどうかの判定ではない
(実際には、コンピュータ システムは値をランダムに割り当てる可能性があり、このタイプの null 値を作成したとしても、実際には機能しません)
つまり: while (i <= S.length && j <= T.length)
KMP (アルゴリズム):
次:
#include<iostream>
using namespace std;
#include<stdlib.h>//存放exit
#include<math.h>//OVERFLOW,exit
typedef int Status;
#define MAXLEN 255
struct SString
//Sequence String
{
char ch[MAXLEN + 1]; //存储串的一维数组
int length; //串的当前长度长度
};
void Get_next(SString T, int *next)
//给你一个子串T,教你逐个算出每个位序对应的next[]
{
int j = 0,//从头开始算起
k = -1;
next[0] = -1;//根据公式
while (j <= T.length - 1)//因为位序从0(而非1)开始
{
if (k == -1 || T.ch[k] == T.ch[j])
{
j++;
k++;
next[j] = k;
}
else
k = next[k];
}
}
int Index_KMP(SString S, SString T, int pos)
{
int next[MAXLEN];
Get_next(T, next);
int i = pos, j = 1;
while (i <= S.length && j <= T.length)
{
if (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()
{
}
次の値:
#include<iostream>
using namespace std;
#include<stdlib.h>//存放exit
#include<math.h>//OVERFLOW,exit
typedef int Status;
#define MAXLEN 255
struct SString
//Sequence String
{
char ch[MAXLEN + 1]; //存储串的一维数组
int length; //串的当前长度长度
};
void Get_nextval(SString T, int nextval[])
//给你一个子串T,教你逐个算出每个位序对应的next[]
{
int j = 0,//从头开始算起
k = -1;
nextval[0] = -1;//根据公式
while (j <= T.length)
{
if (k == -1 || T.ch[k] == T.ch[j])
{
j++;
k++;
if (T.ch[k] != T.ch[j])
nextval[j] = k;
else
nextval[j] = nextval[k];
}
else
k = nextval[k];
}
}
int Index_KMP(SString S, SString T, int pos)
{
int nextval[MAXLEN];
Get_nextval(T, nextval);
int i = pos, j = 1;
while (i <= S.length && j <= T.length)
{
if (S.ch[i] == T.ch[j])
{
++i; ++j;
}//主串和子串依次匹配下一个字符
else
j = nextval[j];
}
if (j > T.length)
return i - T.length; //匹配成功
else
return false;
}
int main()
{
}
アルゴリズムの詳細な研究プロセスは後で再整理されます。詳細については、このシリーズの 15 と 16 を参照してください。
データ構造とアルゴリズムの基礎 (Wang Zhuo) (15): KMPアルゴリズムの詳細な説明(クイック ルーチンと詳細なアイデア分析を含む)