mirror of
https://github.com/Didnelpsun/CS408.git
synced 2026-11-02 13:23:47 +08:00
更新树
This commit is contained in:
@@ -1,10 +1,10 @@
|
||||
#include "head.h"
|
||||
|
||||
// 顺序串
|
||||
class SequenceString{
|
||||
class SequenceString {
|
||||
private:
|
||||
// 数据
|
||||
char* _data{};
|
||||
char *_data{};
|
||||
// 长度
|
||||
int _length{};
|
||||
// 最大容量
|
||||
@@ -20,7 +20,7 @@ public:
|
||||
bool SetData(int index, char character);
|
||||
|
||||
// 获取数据
|
||||
char* GetData();
|
||||
char *GetData();
|
||||
|
||||
char GetData(int index);
|
||||
|
||||
@@ -45,6 +45,12 @@ public:
|
||||
SequenceString();
|
||||
|
||||
explicit SequenceString(int max_size);
|
||||
|
||||
// 暴力模式匹配
|
||||
int LocateSimple(SequenceString string);
|
||||
|
||||
// 销毁
|
||||
bool Destroy();
|
||||
};
|
||||
|
||||
bool SequenceString::SetData() {
|
||||
@@ -57,7 +63,7 @@ bool SequenceString::SetData(int max_size) {
|
||||
return true;
|
||||
}
|
||||
|
||||
bool SequenceString::SetData(char * character) {
|
||||
bool SequenceString::SetData(char *character) {
|
||||
this->_data = character;
|
||||
return true;
|
||||
}
|
||||
@@ -115,4 +121,32 @@ SequenceString::SequenceString(int max_size) {
|
||||
this->SetLength(0);
|
||||
}
|
||||
|
||||
int SequenceString::LocateSimple(SequenceString string) {
|
||||
int i = 0, j = 0;
|
||||
while (i < this->GetLength() && j < string.GetLength()) {
|
||||
// 匹配就继续后移
|
||||
if (this->GetData(i) == string.GetData(j)) {
|
||||
i++;
|
||||
j++;
|
||||
}
|
||||
// 不匹配就退回
|
||||
else {
|
||||
i = i - j + 1;
|
||||
j = 0;
|
||||
}
|
||||
}
|
||||
if (j >= string.GetLength()) {
|
||||
return i - string.GetLength();
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
bool SequenceString::Destroy() {
|
||||
delete (this->GetData());
|
||||
this->SetLength(0);
|
||||
this->SetMaxSize(0);
|
||||
this->SetData(nullptr);
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
#include "head.h"
|
||||
|
||||
// 链栈
|
||||
typedef struct LinkStackNode{
|
||||
// 数据
|
||||
element_type data;
|
||||
|
||||
7
Code/Code/head/link_tree.h
Normal file
7
Code/Code/head/link_tree.h
Normal file
@@ -0,0 +1,7 @@
|
||||
#include "head.h"
|
||||
|
||||
// 二叉树结点
|
||||
typedef struct BinaryTreeNode {
|
||||
element_type data;
|
||||
BinaryTreeNode *left_child, *right_child;
|
||||
} BinaryTreeNode, *BinaryTree;
|
||||
@@ -5,9 +5,9 @@ typedef struct {
|
||||
// 数据
|
||||
char *data;
|
||||
// 长度
|
||||
unsigned int length;
|
||||
int length;
|
||||
// 最大容量
|
||||
unsigned int max_size;
|
||||
int max_size;
|
||||
} SequenceString;
|
||||
|
||||
bool InitSequenceString(SequenceString &string) {
|
||||
@@ -38,4 +38,110 @@ SequenceString InitSequenceString(int max_size) {
|
||||
string->max_size = max_size;
|
||||
string->length = 0;
|
||||
return (SequenceString &) string;
|
||||
}
|
||||
|
||||
// 简单字符串匹配
|
||||
int LocateSimple(SequenceString string, SequenceString pattern) {
|
||||
int i = 0, j = 0;
|
||||
while (i < string.length && j < pattern.length) {
|
||||
// 匹配就继续后移
|
||||
if (string.data[i] == pattern.data[j]) {
|
||||
i++;
|
||||
j++;
|
||||
}
|
||||
// 不匹配就撤回
|
||||
else {
|
||||
i = i - j + 1;
|
||||
j = 0;
|
||||
}
|
||||
}
|
||||
if (j >= pattern.length) {
|
||||
return i - pattern.length;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
// 获取KMP的next数组
|
||||
int *GetNext(SequenceString string) {
|
||||
auto *next = (int *) malloc(sizeof(int) * string.length);
|
||||
next[0] = 0;
|
||||
// i为当前主串正在匹配的字符位置,也就是next数组的索引
|
||||
int i = 0, j = 0;
|
||||
while (i < string.length) {
|
||||
if (j == 0 || string.data[i] == string.data[j]) {
|
||||
next[++i] = ++j;
|
||||
} else {
|
||||
j = next[j];
|
||||
}
|
||||
}
|
||||
return next;
|
||||
}
|
||||
|
||||
// 获取KMP的nextval数组
|
||||
int *GetNextVal(SequenceString string) {
|
||||
auto *nextval = (int *) malloc(sizeof(int) * string.length);
|
||||
nextval[0] = 0;
|
||||
// i为当前主串正在匹配的字符位置,也就是next数组的索引
|
||||
int i = 0, j = 0;
|
||||
while (i < string.length) {
|
||||
if (j == 0 || string.data[i] == string.data[j]) {
|
||||
++i;
|
||||
++j;
|
||||
if (string.data[i] != string.data[j])
|
||||
nextval[i] = j;
|
||||
else
|
||||
nextval[i] = nextval[j];
|
||||
} else {
|
||||
j = nextval[j];
|
||||
}
|
||||
}
|
||||
return nextval;
|
||||
}
|
||||
|
||||
int LocateKMP(SequenceString string, SequenceString pattern, const int *next) {
|
||||
int i = 0, j = 0;
|
||||
while (i < string.length && j < pattern.length) {
|
||||
if (j == 0 || string.data[i] == pattern.data[j]) {
|
||||
// 匹配则继续比较
|
||||
++i;
|
||||
++j;
|
||||
} else {
|
||||
// 模式串右移
|
||||
j = next[j];
|
||||
}
|
||||
}
|
||||
if (j >= pattern.length) {
|
||||
// 匹配成功
|
||||
return i - pattern.length;
|
||||
} else {
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
int LocateKMP(SequenceString string, SequenceString pattern) {
|
||||
int *next = GetNext(pattern);
|
||||
int i = 0, j = 0;
|
||||
while (i < string.length && j < pattern.length) {
|
||||
if (j == 0 || string.data[i] == pattern.data[j]) {
|
||||
// 匹配则继续比较
|
||||
++i;
|
||||
++j;
|
||||
} else {
|
||||
// 模式串右移
|
||||
j = next[j];
|
||||
}
|
||||
}
|
||||
if (j >= pattern.length) {
|
||||
// 匹配成功
|
||||
return i - pattern.length;
|
||||
} else {
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
bool DestroySequenceString(SequenceString &string) {
|
||||
free(string.data);
|
||||
string.max_size = 0;
|
||||
string.length = 0;
|
||||
return true;
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
// ???????
|
||||
// 测试
|
||||
#include <iostream>
|
||||
#include "../head/sequence_list.h"
|
||||
#include "../head/link_list.h"
|
||||
@@ -11,6 +11,7 @@
|
||||
#include "../head/link_queue.h"
|
||||
#include "../head/sequence_string.h"
|
||||
#include "../head/link_string.h"
|
||||
#include "../head/link_tree.h"
|
||||
|
||||
using namespace std;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user