一、字符串暴力匹配
要注意的就是i与j的回溯,通过不断移动主串的指针,时间复杂度高
#include <stdio.h>
#include <stdlib.h>
typedef struct String {
char* data;
int len;
}String;
String* initString() {
String* s = (String*)malloc(sizeof(String));
s->data = NULL;
s->len = 0;
return s;
}
void stringAssign(String* s, char* data) {
if (s->data) {
free(s->data);
}
int len = 0;
char* temp = data;
while (*temp) {
len++;
temp++;
}
if (len == 0) {
s->data = NULL;
s->len = 0;
}
else {
temp = data;
s->len = len;
s->data = (char*)malloc(sizeof(char) * (len + 1));
for (int i = 0; i < len; i++,temp++) {
s->data[i] = *temp;
}
}
}
void printString(String* s) {
for (int i = 0; i < s->len; i++) {
printf(i == 0 ? "%c" : "->%c", s->data[i]);
}
printf("\n");
}
void forceMatch(String* master, String* sub) {
int i = 0;
int j = 0;
while (i < master->len && j < sub->len) {
if (master->data[i] == sub->data[j]) {
i++;
j++;
}
else {
i = i - j + 1;
j = 0;
}
}
if (j == sub->len) {
printf("force match success.\n");
}
else {
printf("force match fail.\n");
}
}
int main() {
String* s = initString();
String* s1 = initString();
stringAssign(s, "ababad");
stringAssign(s1, "bad");
printString(s);
forceMatch(s, s1);
return 0;
}
二、KMP算法
kmp算法,主串指针没有回溯,并且快速达到了匹配状态。
kmp是一种高效的模式匹配算法,它牺牲了一定的空间去保存next数组,提高了我们的匹配效率。kmp算法还能更加智能的移动字符串,让字符串达到匹配状态。
1、何为next数组?
是当该字符与主串发生不匹配之后,值对应索引的字符要移动到跟主串不匹配的字符对齐。
算法:公共前后缀 前面和后面一样的
对于一串字符 ABAB
从最后面的B开始(即ABA):前缀为:A AB 对应的后缀为:A BA 公共为1
再到后面的A(即AB):为0
中间的B(即为A):为0
第一个A(无前值):记为 -1
next值 = 公共前后缀 + 1;
所以ABAB的next数组为:0,1,1,2
2、KMP
代码如下:
#include <stdio.h>
#include <stdlib.h>
typedef struct String {
char* data;
int len;
}String;
String* initString() {
String* s = (String*)malloc(sizeof(String));
s->data = NULL;
s->len = 0;
return s;
}
void stringAssign(String* s, char* data) {
if (s->data) {
free(s->data);
}
int len = 0;
char* temp = data;
while (*temp) {
len++;
temp++;
}
if (len == 0) {
s->data = NULL;
s->len = 0;
}
else {
temp = data;
s->len = len;
s->data = (char*)malloc(sizeof(char) * (len + 1));
for (int i = 0; i < len; i++,temp++) {
s->data[i] = *temp;
}
}
}
void printString(String* s) {
for (int i = 0; i < s->len; i++) {
printf(i == 0 ? "%c" : "->%c", s->data[i]);
}
printf("\n");
}
int* getNext(String* s) {
int* next = (int*)malloc(sizeof(int) * s->len);
int i = 0;
int j = -1;
next[i] = j;
while (i < s->len) {
//这里-1好像更规范,但我不能理解i = len-1时,他出来了,怎么继续写入next = len-1的情况
if (j == -1 || s->data[i] == s->data[j]) {
i++;
j++;
next[i] = j;
}
else {
j = next[j];
}
}
return next;
}
void printNext(int* next, int len) {
for (int i = 0; i < len; i++) {
printf(i == 0 ? "%d" : "->%d", next[i] + 1);
}
printf("\n");
}
void kmpMatch(String* master, String* sub,int* next) {
int i = 0;
int j = 0;
while (i < master->len && j < sub->len) {
if (j == -1 || master->data[i] == sub->data[j]) {
i++;
j++;
}
else {
j = next[j];
}
}
if (j == sub->len) {
printf("kmp match success.\n");
}
else {
printf("kmp match fail.\n");
}
}
int main() {
String* s = initString();
String* s1 = initString();
stringAssign(s, "abddabde");
stringAssign(s1, "abde");
int* next = getNext(s1);
printNext(next, s1->len);
kmpMatch(s, s1, next);
return 0;
}
这里我解释下吧:
总之:理解大于代码(安慰我自己的)
三、堆串和块链串
1、堆串
就是给每个字符串分配其长度空间,并进行空间的更改,记录其长度len,进行其他操作
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define false 0
#define true 1
typedef struct {
char* ch; //字符数组,若是非空则指向起始地址,若为空则NULL
int len; //长度
}HString;
//初始化
int HInit(HString* s) {
s->ch = NULL;
s->len = 0;
}
//串赋值
int HStrAssign(HString* s, const char* chars) {
int i = 0;
while (chars[i] != '\0') { //确定串长
i++;
}
s->len = i;
if (s->ch != NULL) {
free(s);
}
else {
s->ch = (char*)malloc((s->len + 1) * sizeof(char));//0号单元不用
if (s == NULL) {
printf("空间申请失败!");
return false;
}
for (i = 1; i <= s->len; i++) { //依次赋值
s->ch[i] = chars[i - 1];
}
}
}
//串遍历
int HSbianli(HString* s) {
if (s->len == 0) {
printf("串空!");
return false;
}
else {
int i;
for (i = 1; i <= s->len; i++) {
printf("%c", s->ch[i]);
}
return true;
}
}
//串插入
int HStrInsert(HString* s, int pos, const HString t) {
if (pos<1 || pos>s->len) {
printf("位置不合法!");
return false;
}
char* temp;
temp = (char*)malloc((s->len + t.len + 1) * sizeof(char));
int i;
for (i = 1; i < pos; i++) { //将s串pos之前(不含pos)的字符赋给temp
temp[i] = s->ch[i];
}
for (i = pos; i < pos + t.len; i++) { //将t串的元素赋给s
temp[i] = t.ch[i - pos + 1];
}
for (i = 0; i <= s->len - pos; i++) {
temp[pos + t.len + i] = s->ch[i + pos];
}
free(s->ch);
s->ch = temp;
s->len = s->len + t.len;
return true;
}
//串删除
int HStrDelete(HString* s, int pos, int len) {
if (s->len == 0) {
printf("串空!");
return false;
}
else if (len >= s->len) {
printf("非法位置!");
return false;
}
else {
for (int i = pos; i < s->len - pos; i++) { //跨度为len的依次赋值
s->ch[i] = s->ch[i + len];
}
s->len = s->len - len;
return true;
}
}
//串连接
int HStrCon(HString* s, const HString t) {
s->ch = (char*)realloc(s->ch, (s->len + t.len + 1) * sizeof(char));
if (s == NULL) {
printf("空间申请失败!");
return false;
}
if (s->len == 0) {
printf("串空!");
return false;
}
else {
for (int i = 1; i <= t.len; i++) {
s->ch[i + s->len] = t.ch[i];
}
s->len = s->len + t.len;
return true;
}
}
//求字串
int HStrchild(HString* s, int pos, int len) {
if (s->len == 0) {
printf("串空!");
return false;
}
else {
for (int i = 0; i < len; i++) {
printf("%c", s->ch[i + pos]);
}
return true;
}
}
int main() {
HString s;
HString s1;
HInit(&s);
HInit(&s1);
char a[] = { "aaaaa" };
char b[] = { "bbb" };
HStrAssign(&s, a);
HStrAssign(&s1, b);
printf("-----将aaaaa赋值给s,bbb赋值给s1-----\n");
printf("此时串s为:");
HSbianli(&s);
printf("\n此时串s1为:");
HSbianli(&s1);
printf("\n将s1连接到s为:");
HStrCon(&s, s1);
HSbianli(&s);
printf("\n将串s1插入到s串的第二个位置:");
HStrInsert(&s, 2, s1);
HSbianli(&s);
printf("\n再将插入的s1串删除:");
HStrDelete(&s, 2, 3);
HSbianli(&s);
printf("\ns1在第二个元素长度为2的子串为:");
HStrchild(&s, 2, 2);
}
2、块链串
代码:
#include <stdio.h>
#include <stdlib.h>
#define MaxSize 3
typedef struct Chuck {
char chars[MaxSize];
struct Chuck* next;
int count;
}Chuck;
typedef struct Node {
Chuck* head, * tail;
int length;
}*ChlNode, Node;
//初始化一个块链式串
void initChuckLS(ChlNode* chlnode) {
Chuck* chucks = (Chuck*)malloc(sizeof(Chuck));
(*chlnode) = (ChlNode)malloc(sizeof(Node));
chucks->next = NULL;
chucks->count = 0;
(*chlnode)->head = chucks;
(*chlnode)->tail = chucks;
(*chlnode)->length = 1;
}
//添加数据
void pushValue(char value, ChlNode chlnode) {
if (chlnode->head == chlnode->tail) {
chlnode->head->chars[chlnode->head->count] = value;
++chlnode->head->count;
}
else {
chlnode->tail->chars[chlnode->tail->count] = value;
++chlnode->tail->count;
}
if (chlnode->head->count == MaxSize) {
Chuck* chuck = (Chuck*)malloc(sizeof(Chuck));
chuck->count = 0;
chuck->next = NULL;
chlnode->tail->next = chuck;
chlnode->tail = chuck;
++chlnode->length;
}
}
//打印块链串
void printAll(ChlNode chlnode) {
if (chlnode->head == chlnode->tail && chlnode->head->count == 0) {
printf("空块链串");
return;
}
else {
int tmp;
Chuck* tmpPort = chlnode->head;
do {
for (tmp = 0; tmp < tmpPort->count; ++tmp) {
char value = tmpPort->chars[tmp];
printf("%c ", value);
}
tmpPort = tmpPort->next;
} while (tmpPort);
}
}
int main() {
ChlNode chlnode;
initChuckLS(&chlnode);
pushValue('I', chlnode);
pushValue('L', chlnode);
pushValue('O', chlnode);
pushValue('V', chlnode);
pushValue('E', chlnode);
pushValue('Y', chlnode);
pushValue('O', chlnode);
pushValue('U', chlnode);
printAll(chlnode);
system("pause");
return 0;
}
okok,扯点别的,现在2024.06.21,马上就要期末考了,能复习到哪是哪吧,我是转专业的,温馨提示:一定要多花时间,数学要好哈,离散还没开始复习捏,加油加油。
另外就是,我这个真的很草率,只是复习用的,别当真哈,能运行就行,嘿嘿!