#include <bits/stdc++.h>

using namespace std;

string soln(int x,int y,string s1,string s2){
        int t[x+1][y+1];
        for(int i=0;i<x+1;i++) t[i][0]=0;
        for(int j=0;j<y+1;j++) t[0][j]=0;
        
        for(int i=1;i<x+1;i++){
            for(int j=1;j<y+1;j++){
                if(s1[i-1]==s2[j-1]) t[i][j]=1+t[i-1][j-1];
                else t[i][j]=max(t[i-1][j],t[i][j-1]);
            }
        }
        string ans="";
        int a=x;
        int b=y;
        while(a || b){
            if(s1[a-1]==s2[b-1]){
                ans+=s1[a-1];
                a--;
                b--;
            }
            else{
                if(t[a][b-1]>=t[a-1][b]) b--;
                else a--;
            }
        }
        reverse (ans.begin(),ans.end());
        return ans;
}



int main()
{
   int t;
   cin>>t;
   while(t--){
       int x,y;
       cin>>x>>y;
       string s1,s2;
       cin>>s1>>s2;
       string p=soln(x,y,s1,s2);
       cout<<p<<endl;
   }

    return 0;
}

Embed on website

To embed this project on your website, copy the following code and paste it into your website's HTML: