-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdynamic.cpp
77 lines (61 loc) · 1.91 KB
/
dynamic.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
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
// print the LCS based on the table of arrow characters
void printLCS(char **b, const string &x, int i, int j) {
if(i == 0 || j == 0) return;
if(b[i][j] == '\\') {
printLCS(b, x, i - 1, j - 1);
cout << x[i - 1];
}
else if(b[i][j] == '|') printLCS(b, x, i - 1, j);
else printLCS(b, x, i, j - 1);
}
// compute the LCS of two strings
void LCS(const string &x, const string &y) {
// declare variables to hold string lengths & tables
int m = x.length(), n = y.length();
// dynamically allocate our variable length tables
int** c = new int*[m + 1];
for(int i = 0; i <= m; i++) c[i] = new int[n + 1];
char** b = new char*[m];
for(int i = 0; i <= m; i++) b[i] = new char[n];
// initialize first row & column to zeroes
for(int i = 1; i <= m; i++) c[i][0] = 0;
for(int j = 0; j <= n; j++) c[0][j] = 0;
// compare string characters and build arrow table
for(int i = 1; i <= m; i++) {
for(int j = 1; j <= n; j++) {
if(x[i - 1] == y[j - 1]) {
c[i][j] = c[i - 1][j - 1] + 1;
b[i][j] = '\\';
}
else if(c[i - 1][j] >= c[i][j - 1]) {
c[i][j] = c[i - 1][j];
b[i][j] = '|';
}
else {
c[i][j] = c[i][j - 1];
b[i][j] = '_';
}
}
}
// print the LCS
printLCS(b, x, m, n);
}
int main(int argc, const char *argv[]) {
// declare input file & string variables, then extract strings
ifstream input_file;
string string1, string2;
input_file.open(argv[1]);
input_file >> string1;
input_file.close();
input_file.open(argv[2]);
input_file >> string2;
input_file.close();
// pass strings to LCS function
LCS(string1, string2);
cout << endl;
return 0;
}