Alex_tz307

LCS

Sep 19th, 2020
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.68 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. int main() {
  6.     ios_base::sync_with_stdio(false);
  7.     cin.tie(nullptr);
  8.     cout.tie(nullptr);
  9.     int T;
  10.     cin >> T;
  11.     while(T--) {
  12.         int N, M;
  13.         cin >> N >> M;
  14.         string s, t;
  15.         cin >> s >> t;
  16.         vector < vector < int > > dp(N + 1, vector < int >(M + 1));
  17.         for(int i = 1; i <= N; ++i)
  18.             for(int j = 1; j <= M; ++j) {
  19.                 if(s[i - 1] == t[j - 1])
  20.                     dp[i][j] = dp[i - 1][j - 1] + 1;
  21.                 else
  22.                     dp[i][j] = max(dp[i][j], max(dp[i - 1][j], dp[i][j - 1]));
  23.             }
  24.         cout << dp[N][M] << '\n';
  25.     }
  26. }
  27.  
Advertisement
Add Comment
Please, Sign In to add comment