Advertisement
fooker

longest common subsequence

Mar 2nd, 2023 (edited)
163
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.78 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. #define ll long long
  4.  
  5. string lcs(string a, string b){
  6.     string s="";
  7.     ll m = a.length(), n = b.length();
  8.     if (m==0 || n==0) return s;
  9.     else if (a[m-1]==b[n-1]){
  10.         s += a[m-1];
  11.         return lcs(a.substr(0,m-1),b.substr(0,n-1)) + s;
  12.     }
  13.     else if ((lcs(a.substr(0,m),b.substr(0,n-1))).length() > (lcs(a.substr(0,m-1),b.substr(0,n)).length())){
  14.         return lcs(a.substr(0,m),b.substr(0,n-1));
  15.     }
  16.     else if ((lcs(a.substr(0,m-1),b.substr(0,n))).length() >= (lcs(a.substr(0,m),b.substr(0,n-1)).length())){
  17.         return lcs(a.substr(0,m-1),b.substr(0,n));
  18.     }
  19. }
  20.  
  21. int main()
  22. {
  23.     int t;
  24.     cin>>t;
  25.     while(t--){
  26.         string s,t;
  27.         cin>>s>>t;
  28.         cout<<lcs(s,t)<<"\n";
  29.     }
  30. }
  31.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement