-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.cpp
More file actions
32 lines (27 loc) · 887 Bytes
/
main.cpp
File metadata and controls
32 lines (27 loc) · 887 Bytes
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
#include <iostream>
#include <vector>
using std::vector;
using std::string;
// a - vertical
// b - horizontal
int longestSubsequence(string a, string b) {
vector<vector<int>> memo = vector<vector<int>>(a.size() + 1, vector<int>(b.size() + 1, 0));
for (int i = 1; i <= a.size(); i++) {
for (int j = 1; j <= b.size(); j++) {
// if they match
if (a[i - 1] == b[j - 1]) {
memo[i][j] = 1 + memo[i-1][j-1];
} else {
// they do not match
memo[i][j] = std::max(memo[i-1][j], memo[i][j-1]);
}
}
}
return memo[a.size()][b.size()];
}
int main() {
std::cout << longestSubsequence("abc", "b") << std::endl;
std::cout << longestSubsequence("abc", "axxbyca") << std::endl;
std::cout << longestSubsequence("abc", "cba") << std::endl;
return 0;
}