-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkmp.c
88 lines (83 loc) · 1.77 KB
/
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#include <stdbool.h>
#include <string.h>
#include <stdlib.h>
#include "kmp.h"
static const int INITIAL_LENGTH = 8;
bool make_arr(int **arr, int len)
{
if (*arr == NULL) {
if (!(*arr = (int *)malloc(sizeof(int) * len))) {
return false;
}
} else {
if (!realloc(*arr, sizeof(int) * len)) {
free(*arr);
return false;
}
}
return true;
}
bool get_next(const char *substr, int **next)
{
if (!*substr) {
return false;
}
int i = 0;
int j = -1;
int iLen = INITIAL_LENGTH;
int *tmpNext = NULL;
if (!make_arr(&tmpNext, iLen)) {
return false;
}
tmpNext[0] = j;
while (substr[i+1]) {
if (j == -1 || substr[i] == substr[j]) {
i++;
j++;
if (i == iLen) {
iLen *= 2;
if (!make_arr(&tmpNext, iLen)) {
return false;
}
}
if (substr[i] == substr[j]) {
tmpNext[i] = tmpNext[j];
} else {
tmpNext[i] = j;
}
} else {
j = tmpNext[j];
}
}
*next = tmpNext;
return true;
}
bool KMPFindString(const char *str, const char *substr, int start, int *pos)
{
if (!*str) {
return false;
}
int i = start;
int j = 0;
int *next;
if (!get_next(substr, &next)) {
*pos = -1;
return false;
}
while (str[i]) {
if (j == -1 || str[i] == substr[j]) {
i++;
j++;
if (!substr[j]) {
*pos = i - j;
free(next);
return true;
}
} else {
j = next[j];
}
}
*pos = -1;
free(next);
return false;
}