-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkmp.c
60 lines (47 loc) · 1007 Bytes
/
kmp.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void buildPrefixTable(const char *pattern, int *lps, size_t m) {
int j = 0;
for (int i = 1; i < m; ++i) {
while (j > 0 && pattern[i] != pattern[j]) {
j = lps[j - 1];
}
if (pattern[i] == pattern[j]) {
++j;
}
lps[i] = j;
}
}
int kmpSearch(char *text, const char *pattern) {
if (text == NULL || pattern == NULL) {
return -1;
}
size_t n = strlen(text);
size_t m = strlen(pattern);
if (m == 0) {
return -1;
}
int *lps = (int *)malloc(sizeof(int) * m);
if (lps == NULL) {
return -1;
}
memset(lps, 0, sizeof(int) * m); // Initialize with zeros
buildPrefixTable(pattern, lps, m);
int j = 0;
for (int i = 0; i < n; ++i) {
while (j > 0 && text[i] != pattern[j]) {
j = lps[j - 1];
}
if (text[i] == pattern[j]) {
++j;
}
if (j == m) {
int index = i - j + 1;
free(lps);
return index;
}
}
free(lps);
return -1;
}