vault backup: 2026-02-03 18:06:58

This commit is contained in:
2026-02-03 18:06:58 +08:00
parent 23adf96c96
commit 1859a0eb18
2 changed files with 28 additions and 42 deletions

View File

@@ -41,14 +41,15 @@
"id": "ba9747c8bf5f1e5f", "id": "ba9747c8bf5f1e5f",
"type": "leaf", "type": "leaf",
"state": { "state": {
"type": "webviewer", "type": "markdown",
"state": { "state": {
"url": "https://share.note.youdao.com/ynoteshare/index.html?id=a66c778ce8c6e4ee03608e493e59ba7a&type=notebook&_time=1768473840238#/WEB4f51ebc698a96e5cc1a682b2fb7b683d", "file": "Collection/KMP算法.md",
"title": "CQ2605", "mode": "source",
"mode": "webview" "source": false,
"backlinks": false
}, },
"icon": "globe-2", "icon": "lucide-file",
"title": "CQ2605" "title": "KMP算法"
} }
} }
], ],
@@ -119,12 +120,13 @@
"state": { "state": {
"type": "outline", "type": "outline",
"state": { "state": {
"file": "Collection/KMP算法.md",
"followCursor": true, "followCursor": true,
"showSearch": false, "showSearch": false,
"searchQuery": "" "searchQuery": ""
}, },
"icon": "lucide-list", "icon": "lucide-list",
"title": "大纲" "title": "KMP算法 的大纲"
} }
}, },
{ {

View File

@@ -23,27 +23,18 @@ KMP算法的时间复杂度为O(n+m)其中n是主串的长度m是模式串
```c ```c
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
// 函数原型声明 // 函数原型声明
void computeLPSArray(char *pat, int M, int *lps); void computeLPSArray(char *pat, int M, int *lps);
void KMPSearch(char *pat, char *txt); void KMPSearch(char *pat, char *txt);
int main() { int main() {
char text[] = "ABABDABACDABABCABAB"; char text[] = "ABABDABACDABABCABAB";
char pattern[] = "ABABCABAB"; char pattern[] = "ABABCABAB";
printf("Text: %s\n", text); printf("Text: %s\n", text);
printf("Pattern: %s\n", pattern); printf("Pattern: %s\n", pattern);
KMPSearch(pattern, text); KMPSearch(pattern, text);
return 0; return 0;
} }
@@ -68,13 +59,15 @@ 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;
@@ -117,25 +110,16 @@ i++;
} }
if (j == M) { if (j == M) {
printf("Pattern found at index %d \n", i - j); printf("Pattern found at index %d \n", i - j);
j = lps[j - 1]; j = lps[j - 1];
}
} else if (i < N && pat[j] != txt[i]) { else if (i < N && pat[j] != txt[i]) {
if (j != 0) if (j != 0)
j = lps[j - 1]; j = lps[j - 1];
else else
i = i + 1; i = i + 1;
} }
} }
} }
``` ```