SHARE
TWEET

Untitled

a guest Jan 15th, 2020 74 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <fstream>
  2. #include <vector>
  3. #include <algorithm>
  4. #include <string>
  5. using namespace std;
  6.  
  7. int main() {
  8.     ifstream fin("input.txt");
  9.     ofstream fout("output.txt");
  10.     int n;
  11.     fin >> n;
  12.     vector<int>first(n);
  13.     for (int i = 0; i < n; i++) { fin >> first[i]; }
  14.     fin >> n;
  15.     vector<int>second(n);
  16.     for (int i = 0; i < n; i++) { fin >> second[i]; }
  17.    
  18.     vector<vector<int>>dp(first.size(), vector<int>(second.size()));
  19.     for (int i = 0; i < second.size(); i++) {
  20.         if (first[0] == second[i]) {
  21.             dp[0][i] = 1;
  22.         }
  23.     }
  24.     for (int i = 0; i < first.size(); i++) {
  25.         if (second[0] == first[i]) {
  26.             dp[i][0] = 1;
  27.         }
  28.     }
  29.  
  30.     for (int i = 1; i < first.size(); i++) {
  31.         for (int j = 1; j < second.size(); j++) {
  32.             if (first[i] == second[i]) {
  33.                 dp[i][j] = dp[i - 1][j - 1] + 1;
  34.             }
  35.             else {
  36.                 dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]);
  37.             }
  38.         }
  39.     }
  40.  
  41.     fout << dp[first.size() - 1][second.size() - 1];
  42. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top