From eaf28f09e57fd8f4647b2ac83b1e820c765296fc Mon Sep 17 00:00:00 2001 From: Kim Yang Date: Sun, 23 Aug 2020 04:06:16 +0800 Subject: [PATCH] :heavy_plus_sign: finish SSTring --- DataStructure/DS_3_String/DS_3_0_SString.cpp | 71 ++++++++++++++------ DataStructure/DS_3_String/DS_3_1_HString.cpp | 13 ++++ DataStructure/DS_3_String/DS_3_2_LString.cpp | 13 ++++ DataStructure/DS_3_String/DS_3_3_LString.cpp | 12 ++++ 4 files changed, 87 insertions(+), 22 deletions(-) diff --git a/DataStructure/DS_3_String/DS_3_0_SString.cpp b/DataStructure/DS_3_String/DS_3_0_SString.cpp index 083d0d7..1d8a510 100644 --- a/DataStructure/DS_3_String/DS_3_0_SString.cpp +++ b/DataStructure/DS_3_String/DS_3_0_SString.cpp @@ -125,7 +125,7 @@ int Index(SString S, SString T) { int Index_Simple(SString S, SString T) { int k = 1;//k记录当前主串指针 int i = k, j = 1;//j记录模式串指针,i记录主串中与模式串进行对比的子串的起始地址 - while (i < S.length && j < T.length) { + while (i <= S.length && j <= T.length) { if (S.ch[i] == T.ch[j]) { ++i; ++j;//继续比较后续字符 @@ -146,7 +146,7 @@ int Index_Simple(SString S, SString T) { int Index_Simple_CSKaoYan(SString S, SString T) { int i = 1;//i记录当前主串指针 int j = 1;//j记录模式串指针 - while (i < S.length && j < T.length) { + while (i <= S.length && j <= T.length) { if (S.ch[i] == T.ch[j]) { ++i; ++j;//继续比较后续字符 @@ -162,23 +162,6 @@ int Index_Simple_CSKaoYan(SString S, SString T) { } } -//KMP算法 -int Index_KMP(SString S, SString T, int next[]) { - int i = 1, j = 1; - while (i <= S.length && j <= T.length) { - if (j == 0 || S.ch[i] == T.ch[j]) { - ++i; - ++j;//继续比较后继字符 - } else { - j = next[j];//模式串向右移动 - } - } - if (j > T.length)//匹配成功 - return i - T.length; - else - return 0; -} - //求模式串T的next数组 void getNext(SString T, int *next) { int i = 1, j = 0; @@ -196,8 +179,8 @@ void getNext(SString T, int *next) { } } -//KMP2 -int Index_KMP1(SString S, SString T) { +//KMP1 +int Index_KMP(SString S, SString T) { int i = 1, j = 1; int next[T.length + 1]; getNext(T, next); @@ -216,7 +199,7 @@ int Index_KMP1(SString S, SString T) { } //优化next数组 -void Get_BetterNext(SString T, int betterNext[]) { +void Get_BetterNext(SString T, int *betterNext) { int i = 1, j = 0; int next[T.length + 1]; getNext(T, next);//先求出next数组 @@ -228,6 +211,24 @@ void Get_BetterNext(SString T, int betterNext[]) { betterNext[j] = next[j]; } } +//KMP1 +int Index_KMP1(SString S, SString T,int next[]) { + int i = 1, j = 1; +// int next[T.length + 1]; +// getNext(T, next); + while (i <= S.length && j <= T.length) { + if (j == 0 || S.ch[i] == T.ch[j]) { + ++i; + ++j;//继续比较后继字符 + } else { + j = next[j];//模式串向右移动 + } + } + if (j > T.length)//匹配成功 + return i - T.length; + else + return 0; +} //清空操作 void ClearStr(SString &S) { @@ -297,12 +298,14 @@ void testModule() { } else { printf("两个字符串不一样!\n"); } + int n = Index(T, S3); if (0 == n) { printf("主串T中不含子串S3\n"); } else { printf("主串T中含有S3,其下标为:%d\n", n); } + int n1 = Index_Simple(T, S3); if (0 == n1) { printf("主串T中不含子串S3\n"); @@ -310,6 +313,30 @@ void testModule() { printf("主串T中含有S3,其下标为:%d\n", n1); } + int n2 = Index_Simple_CSKaoYan(T, S3); + if (0 == n2) { + printf("主串T中不含子串S3\n"); + } else { + printf("主串T中含有S3,其下标为:%d\n", n2); + } + + int n3 = Index_KMP(T, S3); + if (0 == n3) { + printf("主串T中不含子串S3\n"); + } else { + printf("主串T中含有S3,其下标为:%d\n", n3); + } + + int betterNext[S3.length+1]; + Get_BetterNext(S3,betterNext); + int n4=Index_KMP1(T,S3,betterNext); + if (0 == n4) { + printf("主串T中不含子串S3\n"); + } else { + printf("主串T中含有S3,其下标为:%d\n", n4); + } + + printf("测试结束!\n"); } diff --git a/DataStructure/DS_3_String/DS_3_1_HString.cpp b/DataStructure/DS_3_String/DS_3_1_HString.cpp index 0b8a063..65e545f 100644 --- a/DataStructure/DS_3_String/DS_3_1_HString.cpp +++ b/DataStructure/DS_3_String/DS_3_1_HString.cpp @@ -5,6 +5,8 @@ //顺序存储——动态数组实现方式(堆分配存储) #include + +/**定义模块**/ #define MAXLEN 255 //预定义最大串长为255 typedef struct { @@ -12,3 +14,14 @@ typedef struct { int length; //串的实际长度 }HString; +/**定义模块**/ + +/**实现模块**/ +//坐等填坑 + +/**实现模块**/ + +/**测试模块**/ + + +/**测试模块**/ diff --git a/DataStructure/DS_3_String/DS_3_2_LString.cpp b/DataStructure/DS_3_String/DS_3_2_LString.cpp index 4911f1b..28d0b6a 100644 --- a/DataStructure/DS_3_String/DS_3_2_LString.cpp +++ b/DataStructure/DS_3_String/DS_3_2_LString.cpp @@ -6,8 +6,21 @@ //链式存储——单结点单字符 #include +/**定义模块**/ typedef struct StringNode{ char ch;//每个结点存1个字符,存储密度低,每个字符1B,每个指针4B struct StringNode *next; }StringNode,*String; + +/**定义模块**/ + +/**实现模块**/ +//坐等填坑 + +/**实现模块**/ + +/**测试模块**/ + + +/**测试模块**/ \ No newline at end of file diff --git a/DataStructure/DS_3_String/DS_3_3_LString.cpp b/DataStructure/DS_3_String/DS_3_3_LString.cpp index b6a1b69..4987435 100644 --- a/DataStructure/DS_3_String/DS_3_3_LString.cpp +++ b/DataStructure/DS_3_String/DS_3_3_LString.cpp @@ -6,8 +6,20 @@ //链式存储——单结点多字符 #include +/**定义模块**/ typedef struct StringNode{ char ch[4];//每个结点存多个个字符,存储密度提高,每个字符1B,每个指针4B struct StringNode *next; }StringNode,*String; +/**定义模块**/ + +/**实现模块**/ +//坐等填坑 + +/**实现模块**/ + +/**测试模块**/ + + +/**测试模块**/ \ No newline at end of file