Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <fstream>
- #include <vector>
- #include <string>
- #include <algorithm>
- using namespace std;
- int main() {
- ifstream fin("input.txt");
- ofstream fout("output.txt");
- vector<vector<int>> prev;
- string a, b;
- fin >> b >> a;
- int n = a.size() + 1, m = b.size() + 1;
- vector<vector<int>> LCS(n, vector<int>(m));
- vector<char> L;
- for (int i = 0; i < n; i++) {
- for (int j = 0; j < m; j++) {
- LCS[i][j] = 0;
- }
- }
- for (int i = 1; i < n; i++) {
- for (int j = 1; j < m; j++) {
- if (a[i - 1] == b[j - 1]) {
- LCS[i][j] = LCS[i - 1][j - 1] + 1;
- } else {
- LCS[i][j] = max((LCS[i - 1][j]), (LCS[i][j - 1]));
- }
- }
- }
- int i = n;
- int j = m;
- while (i > 0 && j > 0) {
- if (a[i - 1] == b[j - 1]) {
- L.push_back(a[i - 1]);
- i--;
- j--;
- } else if (LCS[i - 1][j] == LCS[i][j]) {
- i--;
- } else {
- j--;
- }
- }
- for (int i = L.size() - 1; i > 0; i--) {
- fout << L[i];
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement