Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <algorithm>
- #include <iostream>
- #include <vector>
- #include <utility>
- using namespace std;
- void printLCS(vector<vector<char>>& path_, vector<int>& x_, int i, int j) {
- if (i == 0 || j == 0)
- return;
- if (path_[i][j] == 's') {
- printLCS(path_, x_, i-1, j-1);
- cout << x_[i] << " ";
- } else if (path_[i][j] == 'u') {
- printLCS(path_, x_, i-1, j);
- } else {
- printLCS(path_, x_, i, j-1);
- }
- return;
- }
- int main() {
- int xlength, ylength;
- cin >> xlength;
- vector<int> x(xlength + 1);
- for (int i = 1; i <= xlength; ++i)
- cin >> x[i];
- cin >> ylength;
- vector<int> y(ylength + 1);
- for (int i = 1; i <= ylength; ++i)
- cin >> y[i];
- vector<vector<int>> lcs(xlength + 1, vector<int>(ylength + 1, 0));
- vector<vector<char>> path_(xlength + 1, vector<char>(ylength));
- for (int i = 1; i <= xlength; ++i) {
- for (int j = 1; j <= ylength; ++j) {
- if (x[i] == y[j]) {
- lcs[i][j] = lcs[i-1][j-1] + 1;
- path_[i][j] = 's';
- } else if (lcs[i-1][j] >= lcs[i][j-1]) {
- lcs[i][j] = lcs[i-1][j];
- path_[i][j] = 'u';
- } else {
- lcs[i][j] = lcs[i][j-1];
- path_[i][j] = 'l';
- }
- }
- }
- printLCS(path_, x, xlength, ylength);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement