Advertisement
Josif_tepe

Untitled

Nov 18th, 2021
189
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.75 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. using namespace std;
  4. int main()
  5. {
  6.     string a, b;
  7.     cin >> a >> b;
  8.    
  9.     vector<vector<int> > dp(a.size() + 1, vector<int>(b.size() + 1, -2e9));
  10.     dp[0][0] = 0;
  11.    
  12.     for(int i = 0; i < a.size(); i++) {
  13.         for(int j = 0; j < b.size(); j++) {
  14.             if(a[i] == b[j]) {
  15.                 dp[i + 1][j + 1] = max(dp[i + 1][j + 1], dp[i][j] + 1);
  16.             }
  17.             dp[i + 1][j] = max(dp[i + 1][j], dp[i][j]);
  18.             dp[i][j + 1] = max(dp[i][j + 1], dp[i][j]);
  19.         }
  20.     }
  21.     int lcs = 0;
  22.     for(int i = 0; i <= a.size(); i++) {
  23.         for(int j = 0; j <= b.size(); j++) {
  24.             lcs = max(lcs, dp[i][j]);
  25.         }
  26.     }
  27.     cout << lcs << endl;
  28.     return 0;
  29. }
  30.  
  31.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement