-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsubstring_kmp1.cpp
More file actions
248 lines (211 loc) · 6.38 KB
/
substring_kmp1.cpp
File metadata and controls
248 lines (211 loc) · 6.38 KB
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
//
// [mst] kmp_substring1.cpp
// practicing a substring searching with a Knuth-Morris-Pratt(KMP) Pattern Matching algorithm
// based on: https://youtu.be/GTJr8OvyEVQ
//
// gains:
// - KMP algorithm
// - console prints coloring :)
//
// features, changelog:
// - initial. c style, naive implementation
// - 2020.12.25 full kmp implementation
// - added assisting color-coded debug prints
////////////////// LIBS
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <iostream>
#include <windows.h> // WinApi header, for console recoloring
#define DEBUG 1
// color console prints like a boss
#define CLR_DEF 7
#define CLR_RED 12
#define CLR_GRN 10
#define SET_CLR_DEF SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), CLR_DEF);
#define SET_CLR_RED SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), CLR_RED);
#define SET_CLR_GRN SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), CLR_GRN);
#define printf_red(X) SET_CLR_RED printf(#X); SET_CLR_DEF
#define printf_grn(X) SET_CLR_GRN printf(X); SET_CLR_DEF
////////////////// DECL_IMPL
// a defined printf modified
//
void printf_redd() {
//printf("---iterating:\nstr: %s\n", str_ptr);
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), CLR_GRN);
//printf("pat: %*s\n", pat_len + i, patt);
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), CLR_DEF);
}
// print an array
//
void print_arr(const int* arr, int n) {
for (int k = 0; k < n; k++){
printf("%d ", arr[k]);
}
putchar('\n');
}
// print an array and signal given char in color
//
void print_arr_point(const int* arr, int n, int i, int clr) {
for (int k = 0; k < n; k++){
if (k == i) { SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), clr); }
printf("%d ", arr[k]);
if (k == i) { SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), CLR_DEF); }
}
putchar('\n');
}
// print a char array string
//
void print_str(const char* str) {
for (int k = 0; k < strlen(str); k++){
printf("%c ", str[k]);
}
putchar('\n');
}
// print a char array string and signal given chars range in color
//
void print_str_point(const char* arr, int i, int j, int clr) {
for (int k = 0; k < strlen(arr); k++){
if ((k >= i) && (k<=j)) { SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), clr); }
printf("%c", arr[k]);
if ((k >= i) && (k <= j)) { SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), CLR_DEF); }
}
putchar('\n');
}
// a naive substring pattern search. simply iterate strcmp at each index.
// targeted at O(n*m) time
//
int substr_naive(const char *str, const char *patt)
{
const char *str_ptr = str; // record for full string visibility
int i = 0;
int pat_len = strlen(patt);
printf("searching substring. naive method\n");
while (*str)
{
// compare for each char index of the searched text
if (0 == strncmp(str, patt, pat_len)) {
if (DEBUG){
printf("---iterating:\nstr: %s\n", str_ptr);
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), CLR_GRN);
printf("pat: %*s\n", pat_len + i, patt);
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), CLR_DEF);
}
return i;
}
else
{
if (DEBUG){
printf("---iterating:\nstr: %s\n", str_ptr);
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), CLR_RED);
printf("pat: %*s\n", pat_len + i, patt);
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), CLR_DEF);
}
i++;
str++;
}
}
return -1;
}
// a KMP (Knuth-Morris-Pratt algorithm) substring pattern search:
// targeted at O(n+m) time
//
int substr_kmp(const char *str, const char *patt)
{
int result = -1;
const char *str_ptr = str; // record for full string visibility
int str_len_s = strlen(str);
int str_len_p = strlen(patt);
if (str_len_s < str_len_p) return -1;
// building the pattern's temp array
printf("kmp array for the pattern:\n", patt);
print_str(patt);
int * occurences;
occurences = new int[str_len_p]{}; // initted to zeroes
int i = 1;
int j = 0;
for (; i < str_len_p; i++) {
if (patt[j] == patt[i]) {
occurences[i] = j + 1;
j++;
}
else if (j != 0) {
j = occurences[j - 1];
i--; // realign i to compare again
}
}
print_arr(occurences, str_len_p);
// now lets go and match that pattern
i = j = 0;
if (DEBUG){ printf("\nstr|pat\n---|---\n"); } // header
while ((i < str_len_s) || (j < str_len_p)){
// match found
if (str[i] == patt[j]) {
if (DEBUG) {
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), CLR_GRN);
printf(" %c = %c \n", str[i], patt[j]);
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), CLR_DEF);
}
j++;
i++;
// once we found the pattern once, we exit and return the index
if (j == str_len_p) {
result = i - str_len_p;
break;
}
} // match found
// no match
else {
if (DEBUG) {
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), CLR_RED);
printf(" %c ! %c ", str[i], patt[j]);
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), CLR_DEF);
if (j > 0) {
printf(" ==> pattern ptr moves to index: ");
print_arr_point(occurences, str_len_p, j - 1, CLR_RED);
}
else{
putchar('\n');
}
}
if (j > 0) { j = occurences[j - 1]; } // continue matching from a prefix
else { i++; }
} // no match
}
// cleanup
delete (occurences);
// report if pattern found
return result;
}
////////////////// DRIVER
int main(int argc, char *argv[])
{
printf("[mst] Knuth-Morris-Pratt(KMP) Pattern Matching algorithm doodle\n");
int res = -1;
// naive run
char text1[] = "abckkkabcxkkkabc";
char substr1[] = "abcx";
printf("\nnaive pattern search (nested loop): %s in string: %s\n", substr1, text1);
res = substr_naive(text1, substr1);
if (-1 != res) printf("found a substring at index %d\n", res);
// some substrings to test the occurence array building
//char *substr = "abcdabca";
//char *substr = "aabaabaaa";
char text[] = "abxabcabcabcaby";
char substr[] = "abcaby";
printf("\n\nKMP-matching pattern: %s in string: %s\n", substr, text);
res = substr_kmp(text, substr);
if (-1 != res) {
printf("found a substring at index %d:\n", res);
print_str_point(text, res, res + strlen(substr), CLR_GRN);
}
char text2[] = "abcxabcdabxabcdabcdabcy";
char substr2[] = "abcdabcy";
printf("\n\nKMP-matching pattern: %s in string: %s\n", substr2, text2);
res = substr_kmp(text2, substr2);
if (-1 != res) {
printf("found a substring at index %d:\n", res);
print_str_point(text2, res, res + strlen(substr2), CLR_GRN);
}
getchar();
}