Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- using std::vector;
- using std::string;
- int main() {
- std::ios_base::sync_with_stdio(0);
- std::cin.tie(0);
- std::cout.tie(0);
- string first_str, second_str;
- std::cin >> first_str >> second_str;
- int n = first_str.size(), m = second_str.size();
- vector<vector<bool>> get_ans1(n + 1);
- vector<vector<bool>> get_ans2(n + 1);
- vector<bool> base(m + 1, 0);
- vector<int> LCS(m + 1, 0);
- get_ans1[0] = base;
- get_ans2[0] = base;
- for (int i = 1; i <= n; ++i) {
- vector<int> temp_max_str(m+1);
- vector<bool> temp_get_ans1(m + 1);
- vector<bool> temp_get_ans2(m + 1);
- temp_max_str[0] = 0;
- temp_get_ans1[0] = 0;
- temp_get_ans2[0] = 0;
- for (int j = 1; j <= m; ++j) {
- //temp_max_str[j] = std::max(LCS[i-1][j], temp_max_str[j-1]);
- if (LCS[j] > temp_max_str[j-1]) {
- temp_max_str[j] = LCS[j];
- temp_get_ans1[j] = 0;// {false, get_ans[i-1][j].second};//2*bit(LCS[i-1][j].second, 2);
- temp_get_ans2[j] = get_ans2[i-1][j];
- } else {
- temp_max_str[j] = temp_max_str[j-1];
- temp_get_ans1[j] = temp_get_ans1[j-1];//bit(temp_max_str[j-1].second, 1);
- temp_get_ans2[j] = 0;
- }
- if (first_str[i-1] == second_str[j-1]) {
- temp_max_str[j] = std::max(temp_max_str[j], LCS[j-1] + 1);
- temp_get_ans1[j] = 1;
- temp_get_ans2[j] = 1;
- }
- }
- LCS = temp_max_str;
- get_ans1[i] = temp_get_ans1;
- get_ans2[i] = temp_get_ans2;
- }
- int cnt = 0, first = n, second = m;
- int general_size = LCS[m];
- vector<char> answer;
- while (cnt < general_size) {
- if (get_ans1[first][second] == 1 && get_ans2[first][second] == 1) {
- answer.push_back(first_str[first-1]);
- ++cnt;
- --first;
- --second;
- } else if (get_ans1[first][second] == 0 && get_ans2[first][second] == 1) {
- --first;
- } else if (get_ans1[first][second] == 1 && get_ans2[first][second] == 0) {
- --second;
- } else if (get_ans1[first][second] == 0 && get_ans2[first][second] == 0) {
- --first;
- --second;
- }
- }
- for (int i = answer.size() - 1; i >= 0; --i) {
- std::cout << answer[i];
- }
- std::cout << std::endl;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement