-
Notifications
You must be signed in to change notification settings - Fork 1
/
src.cc
41 lines (34 loc) · 813 Bytes
/
src.cc
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
// The algorithm design manual [Book]
// [2.5.3] String Pattern Matching
#include <iostream>
#include <cstring>
#include "tracer/array.h"
int main() {
char t[] = "Amr Salama", p[] = "Salama";
int n = strlen(t), m = strlen(p);
int i, j;
tracer::ArrayTracer<char> tTracer(t, n, 0.5);
tracer::ArrayTracer<char> pTracer(p, m, 0.5);
for (i = 0; i <= (n-m); i++) {
j = 0;
while ((j < m) && (t[i+j] == p[j])) {
tTracer.select(i+j, 0.2);
pTracer.select(j, 0.2);
j++;
}
if (j == m) {
std::cout << i << std::endl;
break;
} else {
tTracer.notify(i+j, 0.2);
pTracer.notify(j, 0.2);
}
j = 0;
while ((j < m) && (t[i+j] == p[j])) {
tTracer.deselect(i+j, 0.01);
pTracer.deselect(j, 0.01);
j++;
}
}
return 0;
}