Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- #define ll long long
- string lcs(string a, string b){
- string s="";
- ll m = a.length(), n = b.length();
- if (m==0 || n==0) return s;
- else if (a[m-1]==b[n-1]){
- s += a[m-1];
- return lcs(a.substr(0,m-1),b.substr(0,n-1)) + s;
- }
- else if ((lcs(a.substr(0,m),b.substr(0,n-1))).length() > (lcs(a.substr(0,m-1),b.substr(0,n)).length())){
- return lcs(a.substr(0,m),b.substr(0,n-1));
- }
- else if ((lcs(a.substr(0,m-1),b.substr(0,n))).length() >= (lcs(a.substr(0,m),b.substr(0,n-1)).length())){
- return lcs(a.substr(0,m-1),b.substr(0,n));
- }
- }
- int main()
- {
- int t;
- cin>>t;
- while(t--){
- string s,t;
- cin>>s>>t;
- cout<<lcs(s,t)<<"\n";
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement