-
Notifications
You must be signed in to change notification settings - Fork 3
/
approximate-string-matching.cpp
232 lines (204 loc) · 4.98 KB
/
approximate-string-matching.cpp
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
/**
* @file approximate-string-matching.cpp
* @author prakash ([email protected])
* @brief
* @version 0.1
* @date 2021-08-15
*
* @copyright Copyright (c) 2021
*
*/
#include <ostream>
#define underline "\033[4m"
namespace Color {
enum Code {
FG_RED = 31,
FG_GREEN = 32,
FG_BLUE = 34,
FG_DEFAULT = 39,
BG_RED = 41,
BG_GREEN = 42,
BG_BLUE = 44,
BG_DEFAULT = 49
};
std::ostream &operator<<(std::ostream &os, Code code) {
return os << "\033[" << static_cast<int>(code) << "m";
}
} // namespace Color
#include <iostream>
#include <string>
using namespace std;
#define MATCH 0
#define INSERT 1
#define SWAPS 2
#define DELETE 3
typedef struct cell {
int cost;
int parent;
} cell;
/*costt of insertionor deletion */
int indelCost(char a) { return 1; }
/*cost of match or mismatch */
int matchCost(char &a, char &b) {
if (a == b) {
return 0;
}
return 1;
}
int swapCost(string &a, string &b, int i, int j) {
if (a[i] == b[j - 1] && a[i - 1] == b[j])
return 1;
return 10;
}
void match_out(string &a, string &b, int i, int j) {
if (a[i] == b[j])
cout << 'M';
else {
cout << 'S';
}
}
void insert_out(string &a, string &b, int i, int j) { cout << 'I'; }
void delete_out(string &a, string &b, int i, int j) { cout << 'D'; }
void reconstruct_path(string &s, string &t, int i, int j, cell *dp[]) {
if (dp[i][j].parent == -1) {
return;
}
if (dp[i][j].parent == MATCH) {
reconstruct_path(s, t, i - 1, j - 1, dp);
match_out(s, t, i, j);
return;
}
if (dp[i][j].parent == INSERT) {
reconstruct_path(s, t, i, j - 1, dp);
insert_out(s, t, i, j);
return;
}
if (dp[i][j].parent == DELETE) {
reconstruct_path(s, t, i - 1, j, dp);
delete_out(s, t, i, j);
return;
}
}
/**
* @brief recursive string compare algorithm with Time complexity O(3^n)
*
* @param S
* @param T
* @param i
* @param j
* @return int
*/
int string_compare_recursive(string S, string T, int i, int j) {
int k; /*counter*/
int opt[4]; /* cost of three options*/
int lowest_cost;
// base case
if (i == 0) {
return j * indelCost(' ');
}
if (j == 0) {
return i * indelCost(' ');
}
// recursive case
opt[MATCH] =
string_compare_recursive(S, T, i - 1, j - 1) + matchCost(S[i], S[j]);
opt[INSERT] = string_compare_recursive(S, T, i, j - 1) + indelCost(T[j]);
opt[DELETE] = string_compare_recursive(S, T, i - 1, j) + indelCost(S[i]);
lowest_cost = opt[MATCH];
for (k = MATCH; k <= DELETE; k++) {
if (opt[k] < lowest_cost) {
lowest_cost = opt[k];
}
}
return lowest_cost;
}
int string_compare_dp(string s, string T) {
cell dp[s.length() + 1][T.length() + 1];
int i, j, k;
int opt[4];
// init row
for (i = 0; i <= s.length(); i++) {
dp[i][0].cost = i;
if (i > 0) {
dp[i][0].parent = INSERT;
} else {
dp[i][0].parent = -1;
}
}
for (i = 0; i <= T.length(); i++) {
dp[0][i].cost = i;
if (i > 0) {
dp[0][i].parent = DELETE;
} else {
dp[0][i].parent = -1;
}
}
// fill dp
for (i = 1; i <= s.length(); i++) {
for (j = 1; j <= T.length(); j++) {
opt[MATCH] = (dp[i - 1][j - 1]).cost + matchCost(s[i - 1], T[j - 1]);
opt[INSERT] = (dp[i][j - 1]).cost + indelCost(T[j - 1]);
if (i > 1 && j > 1) {
opt[SWAPS] = (dp[i - 2][j - 2]).cost + swapCost(s, T, i - 1, j - 1);
} else {
opt[SWAPS] = INT_MAX;
}
opt[DELETE] = (dp[i - 1][j]).cost + indelCost(s[i - 1]);
(dp[i][j]).cost = opt[MATCH];
(dp[i][j]).parent = MATCH;
for (k = INSERT; k <= DELETE; k++) {
if (opt[k] < (dp[i][j]).cost) {
(dp[i][j]).cost = opt[k];
(dp[i][j]).parent = k;
}
}
}
}
return dp[s.length()][T.length()].cost;
}
/*visulaize dp*/
void visulaize_dp_and_recosntruct_path(string S, string T) {
int n = S.length(), m = T.length();
cell dp[n + 1][m + 1];
int i, j, k;
int opts[4];
int lowest_cost;
for (i = 0; i <= n; i++) {
dp[i][0].cost = i;
if (i > 0) {
dp[i][0].parent = INSERT;
} else {
dp[i][0].parent = -1;
}
}
for (i = 0; i <= m; i++) {
dp[0][i].cost = i;
if (i > 0) {
dp[0][i].parent = DELETE;
} else {
dp[0][i].parent = -1;
}
}
for (i = 1; i <= n; i++) {
for (j = 1; j <= m; j++) {
opts[MATCH] = dp[i - 1][j - 1].cost + matchCost(S[i - 1], T[j - 1]);
opts[INSERT] = dp[i][j - 1].cost + indelCost(T[j - 1]);
opts[DELETE] = dp[i - 1][j].cost + indelCost(S[i - 1]);
dp[i][j].cost = opts[MATCH];
dp[i][j].parent = MATCH;
for (k = INSERT; k <= DELETE; k++) {
if (opts[k] < dp[i][j].cost) {
dp[i][j].cost = opts[k];
dp[i][j].parent = k;
}
}
}
}
lowest_cost = dp[n][m].cost;
cout << Color::FG_DEFAULT << "lowest cost is " << lowest_cost << endl;
}
int main(int argc, const char **argv) {
cout << string_compare_dp("thou shalt", "you should") << endl;
cout << string_compare_dp("steve", "setve") << endl;
return 0;
}