Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cmath>
- #include <vector>
- #include <queue>
- #include <algorithm>
- #include <cstring>
- #include <fstream>
- using namespace std;
- string a, b;
- int dp[3005][3005];
- pair<int, int> backtrack[3005][3005];
- char bukva[3005][3005];
- int rec(int i, int j) {
- if(i == -1 or j == -1) {
- return 0;
- }
- if(dp[i][j] != -1) {
- return dp[i][j];
- }
- int result = 0;
- int r1 = -1, r2 = -1, r3 = -1;
- if(a[i] == b[j]) {
- r1 = rec(i - 1, j - 1) + 1;
- }
- r2 = rec(i - 1, j);
- r3 = rec(i, j - 1);
- result = max(r1, max(r2, r3));
- if(r1 == result) {
- backtrack[i][j] = make_pair(i - 1, j - 1);
- bukva[i][j] = a[i];
- }
- else if(r2 == result) {
- backtrack[i][j] = make_pair(i - 1, j);
- bukva[i][j] = '/';
- }
- else {
- backtrack[i][j] = make_pair(i, j - 1);
- bukva[i][j] = '/';
- }
- return dp[i][j] = result;
- }
- int main() {
- cin >> a >> b;
- memset(dp, -1, sizeof dp);
- rec(a.size() - 1, b.size() - 1);
- pair<int, int> at = make_pair(a.size() - 1, b.size() - 1);
- string s = "";
- while(true) {
- if(at.first >= 0 and at.second >= 0) {
- s += bukva[at.first][at.second];
- at = make_pair(backtrack[at.first][at.second].first, backtrack[at.first][at.second].second);
- }
- else {
- break;
- }
- }
- for(int i = (int) s.size() - 1; i >= 0; i--) {
- if(s[i] != '/') {
- cout << s[i];
- }
- }
- return 0;
- }
- // 100 000 000
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement