Problem: 2050E - Three Strings
Language: C++17 (GCC 7-32)

Previous submission (verdict: Compilation error):


#include <bits/stdc++.h>



#define ll long long 

#define mr make_pair



using namespace std;



ll max(ll a, ll b){

    if(a>b) return a;

    return b;

}



ll min(ll a, ll b){

    if(a<b) return a;

    return b;

}



int max(int a,int b){

    if(a>b) return a;

    return b;

}



int min(int a,int b){

    if(a<b) return a;

    return b;

}



void f(vector<ll> &v,ll &n){

   for(ll i = 0;i<n;i++){

       cin>>v[i];

   }

}



void sort(vector<ll>& v){

    sort(v.begin(),v.end());

}



void sort(vector<int>& v){

    sort(v.begin(),v.end());

}



ll solve(string &a,string &b,string &c,ll &i1,ll &i2,vector<vector<ll>>& dp){

    if(i1==0 && i2==0){

        return 0;

    }

    

    if(dp[i1][i2] != -1){

        return dp[i1][i2];

    }

    

    ll maxi = LONG_MAX;

    

    if(i1>0){

        ll val = solve(a,b,c,i1-1,i2,dp);

        if(a[i1-1] == c[i1+i2-1]){

            maxi = min(maxi,val);

        }

        else if(val != LONG_MAX){

            maxi = min(maxi,1+val);

        }

    }

    

    if(i2>0){

        ll val = solve(a,b,c,i1,i2-1,dp);

        if(b[i2-1] == c[i1+i2-1]){

            maxi = min(maxi,val);

        }

        else if(val != LONG_MAX){

            maxi = min(maxi,1+val);

        }

    }

    

    return dp[i1][i2] = maxi;

}



int main(){

    ll t;

    cin>>t;

 

    while(t--){

       string a,b,c;

       cin>>a;

       cin>>b;

       cin>>c;

       

       ll n = a.length();

       ll m = b.length();

       

       vector<vector<ll>> dp(n+1,vector<ll>(m+1,-1));

       

       cout<<solve(a,b,c,n,m,dp)<<endl;

    }

    

}





Submission (verdict: Accepted):


#include <bits/stdc++.h>



#define ll long long 

#define mr make_pair



using namespace std;



ll max(ll a, ll b){

    if(a>b) return a;

    return b;

}



ll min(ll a, ll b){

    if(a<b) return a;

    return b;

}



int max(int a,int b){

    if(a>b) return a;

    return b;

}



int min(int a,int b){

    if(a<b) return a;

    return b;

}



void f(vector<ll> &v,ll &n){

   for(ll i = 0;i<n;i++){

       cin>>v[i];

   }

}



void sort(vector<ll>& v){

    sort(v.begin(),v.end());

}



void sort(vector<int>& v){

    sort(v.begin(),v.end());

}



ll solve(string &a,string &b,string &c,ll i1,ll i2,vector<vector<ll>>& dp){

    if(i1==0 && i2==0){

        return 0;

    }

    

    if(dp[i1][i2] != -1){

        return dp[i1][i2];

    }

    

    ll maxi = LONG_MAX;

    

    if(i1>0){

        ll val = solve(a,b,c,i1-1,i2,dp);

        if(a[i1-1] == c[i1+i2-1]){

            maxi = min(maxi,val);

        }

        else if(val != LONG_MAX){

            maxi = min(maxi,1+val);

        }

    }

    

    if(i2>0){

        ll val = solve(a,b,c,i1,i2-1,dp);

        if(b[i2-1] == c[i1+i2-1]){

            maxi = min(maxi,val);

        }

        else if(val != LONG_MAX){

            maxi = min(maxi,1+val);

        }

    }

    

    return dp[i1][i2] = maxi;

}



int main(){

    ll t;

    cin>>t;

 

    while(t--){

       string a,b,c;

       cin>>a;

       cin>>b;

       cin>>c;

       

       ll n = a.length();

       ll m = b.length();

       

       vector<vector<ll>> dp(n+1,vector<ll>(m+1,-1));

       

       cout<<solve(a,b,c,n,m,dp)<<endl;

    }

    

}