-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmyers.cpp
More file actions
79 lines (66 loc) · 1.79 KB
/
myers.cpp
File metadata and controls
79 lines (66 loc) · 1.79 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
#include "myers.h"
#include <QString>
#include <QStringList>
#include <qdebug.h>
#include <qelapsedtimer.h>
#include <vector>
Myers::Myers() {}
QList<QStringList> Myers::getDiff(const QString& string1, const QString& string2) {
QStringList lines1 = string1.split("\n");
QStringList lines2 = string2.split("\n");
const int N = lines1.length();
const int M = lines2.length();
// Use vector instead of QList for better performance
std::vector<std::vector<int>> dp(N + 1, std::vector<int>(M + 1, INT_MAX));
std::vector<std::vector<std::pair<int, int>>> prev(N + 1, std::vector<std::pair<int, int>>(M + 1));
// Initialize base cases
dp[0][0] = 0;
// Pre-compute string equality
std::vector<std::vector<bool>> equal(N, std::vector<bool>(M));
for (int i = 0; i < N; i++) {
for (int j = 0; j < M; j++) {
equal[i][j] = (lines1[i] == lines2[j]);
}
}
// Dynamic programming approach
for (int j = 0; j <= M; j++) {
for (int i = 0; i <= N; i++) {
if (i < N && j < M && equal[i][j]) {
if (dp[i][j] + 1 < dp[i + 1][j + 1]) {
dp[i + 1][j + 1] = dp[i][j] + 1;
prev[i + 1][j + 1] = {i, j};
}
}
if (j < M) {
if (dp[i][j] + 1 < dp[i][j + 1]) {
dp[i][j + 1] = dp[i][j] + 1;
prev[i][j + 1] = {i, j};
}
}
if (i < N) {
if (dp[i][j] + 1 < dp[i + 1][j]) {
dp[i + 1][j] = dp[i][j] + 1;
prev[i + 1][j] = {i, j};
}
}
}
}
// Reconstruct the path
QList<QStringList> finalChanges;
int i = N, j = M;
while (i > 0 || j > 0) {
QStringList changeEntry;
auto [pi, pj] = prev[i][j];
if (pi < i && pj < j) {
changeEntry << "=" << lines1[pi];
} else if (pi < i) {
changeEntry << "-" << lines1[pi];
} else {
changeEntry << "+" << lines2[pj];
}
finalChanges.prepend(changeEntry);
i = pi;
j = pj;
}
return finalChanges;
}