vault backup: 2026-02-03 18:06:58
This commit is contained in:
16
.obsidian/workspace.json
vendored
16
.obsidian/workspace.json
vendored
@@ -41,14 +41,15 @@
|
||||
"id": "ba9747c8bf5f1e5f",
|
||||
"type": "leaf",
|
||||
"state": {
|
||||
"type": "webviewer",
|
||||
"type": "markdown",
|
||||
"state": {
|
||||
"url": "https://share.note.youdao.com/ynoteshare/index.html?id=a66c778ce8c6e4ee03608e493e59ba7a&type=notebook&_time=1768473840238#/WEB4f51ebc698a96e5cc1a682b2fb7b683d",
|
||||
"title": "CQ2605",
|
||||
"mode": "webview"
|
||||
"file": "Collection/KMP算法.md",
|
||||
"mode": "source",
|
||||
"source": false,
|
||||
"backlinks": false
|
||||
},
|
||||
"icon": "globe-2",
|
||||
"title": "CQ2605"
|
||||
"icon": "lucide-file",
|
||||
"title": "KMP算法"
|
||||
}
|
||||
}
|
||||
],
|
||||
@@ -119,12 +120,13 @@
|
||||
"state": {
|
||||
"type": "outline",
|
||||
"state": {
|
||||
"file": "Collection/KMP算法.md",
|
||||
"followCursor": true,
|
||||
"showSearch": false,
|
||||
"searchQuery": ""
|
||||
},
|
||||
"icon": "lucide-list",
|
||||
"title": "大纲"
|
||||
"title": "KMP算法 的大纲"
|
||||
}
|
||||
},
|
||||
{
|
||||
|
||||
@@ -23,27 +23,18 @@ KMP算法的时间复杂度为O(n+m),其中n是主串的长度,m是模式串
|
||||
```c
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
#include <string.h>
|
||||
|
||||
// 函数原型声明
|
||||
|
||||
void computeLPSArray(char *pat, int M, int *lps);
|
||||
|
||||
void KMPSearch(char *pat, char *txt);
|
||||
|
||||
int main() {
|
||||
|
||||
char text[] = "ABABDABACDABABCABAB";
|
||||
|
||||
char pattern[] = "ABABCABAB";
|
||||
|
||||
printf("Text: %s\n", text);
|
||||
|
||||
printf("Pattern: %s\n", pattern);
|
||||
|
||||
KMPSearch(pattern, text);
|
||||
|
||||
return 0;
|
||||
|
||||
}
|
||||
@@ -60,25 +51,27 @@ int i = 1;
|
||||
|
||||
while (i < M) {
|
||||
|
||||
if (pat[i] == pat[len]) {
|
||||
if (pat[i] == pat[len]) {
|
||||
|
||||
len++;
|
||||
len++;
|
||||
|
||||
lps[i] = len;
|
||||
lps[i] = len;
|
||||
|
||||
i++;
|
||||
i++;
|
||||
|
||||
} else {
|
||||
}
|
||||
else {
|
||||
|
||||
if (len != 0) {
|
||||
if (len != 0) {
|
||||
|
||||
len = lps[len - 1];
|
||||
len = lps[len - 1];
|
||||
|
||||
} else {
|
||||
}
|
||||
else {
|
||||
|
||||
lps[i] = 0;
|
||||
lps[i] = 0;
|
||||
|
||||
i++;
|
||||
i++;
|
||||
|
||||
}
|
||||
|
||||
@@ -117,25 +110,16 @@ i++;
|
||||
}
|
||||
|
||||
if (j == M) {
|
||||
|
||||
printf("Pattern found at index %d \n", i - j);
|
||||
|
||||
j = lps[j - 1];
|
||||
|
||||
} else if (i < N && pat[j] != txt[i]) {
|
||||
|
||||
if (j != 0)
|
||||
|
||||
j = lps[j - 1];
|
||||
|
||||
printf("Pattern found at index %d \n", i - j);
|
||||
j = lps[j - 1];
|
||||
}
|
||||
else if (i < N && pat[j] != txt[i]) {
|
||||
if (j != 0)
|
||||
j = lps[j - 1];
|
||||
else
|
||||
|
||||
i = i + 1;
|
||||
|
||||
i = i + 1;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
```
|
||||
|
||||
Reference in New Issue
Block a user