-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlcs.cpp
330 lines (257 loc) · 8.43 KB
/
lcs.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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
#include "lcs.hpp"
#include <iostream>
#include <algorithm>
using namespace std;
//! Id de chamada da função, usado para plotagem dos gráficos no GraphView
int callid = 0;
//! Nome do arquivo do GraphView
string gv_file;
//! Tamanho em linhas e colunas do memo
size_t memo_r, memo_c;
/**
* @brief Aloca uma matriz de memo e inicializa com -1
*
* @param m Altura da matriz
* @param n Largura da matriz
* @return int** Matriz de memo
*/
int** alloc_memo(size_t m, size_t n) {
int **memo = new int*[m + 1];
for (int i = 0; i <= m; i++) {
memo[i] = new int[n + 1];
fill_n(memo[i], n + 1, -1);
}
return memo;
}
/**
* @brief Libera uma matriz de memo
*
* @param memo Matriz
* @param m Altura da matriz
*/
void free_memo(int** memo, size_t m) {
for (int i = 0; i <= m; i++)
delete [] memo[i];
delete [] memo;
}
/**
* @brief Escreve um log do memo no arquivo de log
*
* @param memo Matriz de memorização
*/
void log_memo(int** memo, size_t m, size_t n) {
ofstream memo_file("memo_log", ios::app);
memo_file << m << " " << n << endl;
for (int i = 1; i < memo_r; i++) {
for (int j = 1; j < memo_c; j++) {
if (memo[i][j] >= 0)
memo_file << memo[i][j];
memo_file << ";";
}
memo_file << endl;
}
memo_file << endl;
memo_file.close();
}
/**
* @brief Define propriedades de um nó por número no gráfico
*
* @param number Número do nó
* @param attr Atributos do nó
*/
void gv_node(int number, const string& attr = "") {
ofstream call_graph(gv_file, ios::app);
call_graph << "node" << number;
if (!attr.empty())
call_graph << " [" << attr << "]";
call_graph << endl;
call_graph.close();
}
/**
* @brief Define a label de um nó a partir dos parâmetros da função `lcs`
*
* @param number Número do nó
* @param a String A
* @param b String B
* @param m Tamanho da string A
* @param n Tamanho da string B
*/
void gv_label(int number, const string& a, const string& b, size_t m, size_t n) {
gv_node(
number,
"label=\"LCS("
+ a.substr(0, m)
+ ", "
+ b.substr(0, n)
+ ")\""
);
}
/**
* @brief Liga dois nós
*
* @param from Nó de saída
* @param to Nó de entrada
*/
void gv_edge(int from, int to) {
ofstream call_graph(gv_file, ios::app);
call_graph << "node" << from
<< " -> "
<< "node" << to
<< endl;
call_graph.close();
}
// LCS ruinzão, sem memo
string lcs_bad(const string& a, const string& b, size_t m, size_t n) {
gv_file = "call_graph_bad.gv";
gv_label(callid, a, b, m, n);
// Se o tamanho é zero, a resposta é vazia
if (m == 0 || n == 0) {
gv_node(callid, "color=white");
return "";
} else {
// Se o último caractere das duas strings é igual, tira ele e põe no LCS
if (a[m - 1] == b[n - 1]) {
callid++;
gv_edge(callid - 1, callid);
return lcs_bad(a, b, m - 1, n - 1) + a[m - 1];
// Se não, pega o maior LCS:
// - da string A sem o último caractere e B
// - ou da string B sem o último caractere e A
} else {
int ci = callid;
gv_node(callid, "color=red");
callid++;
gv_edge(callid - 1, callid);
string sa = lcs_bad(a, b, m - 1, n);
callid++;
gv_edge(ci, callid);
string sb = lcs_bad(a, b, m, n - 1);
return sa.length() >= sb.length() ? sa : sb;
}
}
}
// LCS bonzão, com memo
int lcs_memo(const string& a, const string& b, size_t m, size_t n, int** memo) {
gv_file = "call_graph_memo.gv";
gv_label(callid, a, b, m, n);
int result = 0;
// Se o tamanho é zero, a resposta é vazia
if (m == 0 || n == 0) {
gv_node(callid, "color=white");
return 0;
} else {
// Se já existe no memo, nem faz sentido calcular
if (memo[m][n] != -1) {
gv_node(callid, "color=green");
return memo[m][n];
}
// Se o último caractere das duas strings é igual, tira ele e põe no LCS
if (a[m - 1] == b[n - 1]) {
callid++;
gv_edge(callid - 1, callid);
result = lcs_memo(a, b, m - 1, n - 1, memo) + 1;
// Se não, pega o maior LCS:
// - da string A sem o último caractere e B
// - ou da string B sem o último caractere e A
} else {
int ci = callid;
gv_node(ci, "color=red");
callid++;
gv_edge(ci, callid);
int lcs_a = lcs_memo(a, b, m - 1, n, memo);
callid++;
gv_edge(ci, callid);
int lcs_b = lcs_memo(a, b, m, n - 1, memo);
result = max(lcs_a, lcs_b);
}
}
memo[m][n] = result;
log_memo(memo, m, n);
return result;
}
// Backtracking do LCS a partir das strings e do memo
string lcs_backtrack(const string& a, const string& b, size_t m, size_t n, int** memo) {
// Se alguma está vazia, o lcs é vazio
if (m == 0 || n == 0)
return "";
// Se o último caractere das duas é igual, o lcs é o lcs do que vem antes
// mais esse caractere no final
if (a[m - 1] == b[n - 1])
return lcs_backtrack(a, b, m - 1, n - 1, memo) + a[m - 1];
// Se o LCS pra esquerda na matriz é maior ou igual, usa ele
else if (memo[m][n - 1] >= memo[m - 1][n])
return lcs_backtrack(a, b, m, n - 1, memo);
// Se não, usa o LCS pra cima na matriz
else
return lcs_backtrack(a, b, m - 1, n, memo);
}
// LCS bonzão com memo, mas sem ele na assinatura. Serve pra chamar sem ter
// que se preocupar em alocar a matriz de memo e tal...
string lcs_memo(const string& a, const string& b) {
ofstream memo_file("memo_log");
memo_file << a << endl
<< b << endl;
memo_file.close();
int m = a.length(), n = b.length();
memo_r = m + 1;
memo_c = n + 1;
// Matriz de memo
int** memo = alloc_memo(m, n);
// Calcula o LCS
lcs_memo(a, b, m, n, memo);
string lcs = lcs_backtrack(a, b, m, n, memo);
// Libera a matriz de memo
free_memo(memo, m);
return lcs;
}
// Diff recursivo com backtracking
diff_node* diff(const string& a, const string& b, int m, int n, int** memo) {
diff_node *result, *last;
// Se os caracteres são iguais, está no LCS e pontanto não tem diferença
if (m > 0 && n > 0 && a[m - 1] == b[n - 1]) {
last = diff(a, b, m - 1, n - 1, memo);
result = new diff_node();
result->operation = ' ';
result->value = a[m - 1];
// Se esse é um caractere que está na string B e não na string A, ele foi
// adicionado
} else if (n > 0 && (m == 0 || memo[m][n - 1] >= memo[m - 1][n])) {
last = diff(a, b, m, n - 1, memo);
result = new diff_node();
result->operation = '+';
result->value = b[n - 1];
// Se esse é um caractere que está na string A e não na string B, ele foi
// removido
} else if (m > 0 && (n == 0 || memo[m - 1][n] > memo[m][n - 1])) {
last = diff(a, b, m - 1, n, memo);
result = new diff_node();
result->operation = '-';
result->value = a[m - 1];
} else
return nullptr;
result->next = nullptr;
// Se tinha algum nó antes, adiciona o atual ao fim da lista (as chamadas
// acontecem de trás pra frente)
if (last) {
diff_node* node = last;
while (node->next)
node = node->next;
node->next = result;
return last;
// Se não retorna esse nó mesmo
} else
return result;
}
// Diff
diff_node* diff(const string& a, const string& b) {
size_t m = a.length(), n = b.length();
// Matriz de memo
int** memo = alloc_memo(m, n);
// Calcula o LCS
lcs_memo(a, b, m, n, memo);
// Calcula a lista diff das strings
diff_node* result = diff(a, b, m, n, memo);
// Libera a matriz de memo
free_memo(memo, m);
return result;
}