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

Previous submission (verdict: Time limit exceeded on test 16):
#include <bits/stdc++.h>
using namespace std;
int ar[1001][1001]; //ar[a][b] la gia tri minimum cua de bai khi day st1 co a phan tu dau tien, 
                    //st2 co b phan tu dau tien, va st co a+b phan tu dau tien

int minn (int a, int b){
    if (a<b) return a;
    return b;
}
//ab 
//ba 
//aabb
void dp (string st1, int s1, string st2, int s2, string st){
    //s1 la do dai chuoi st1, s1 la do dai chuoi st2
    //s1, s2 la so luong phan tu nhe, trong ham main thi ghi la 
    // dp(st1, st1.length(), st2, st2.length(), st)
    int a = s1;
    int b = s2;
    // truong hop co so 
    if (a==0){
        if (ar[0][b-1]==-1) dp(st1, 0, st2, b-1, st);
        if (st[b-1]==st2[b-1]) ar[0][b]=ar[0][b-1];
        else ar[0][b]=ar[0][b-1]+1;
        return;
    }
    if (b==0){
        if (ar[0][b-1]==-1) dp(st1, 0, st2, b-1, st);
        if (st[a-1]==st1[a-1]) ar[a][0]=ar[a-1][0];
        else ar[a][0]=ar[a-1][0]+1;
        return;
    }
    // xu ly
    // so sanh st[i] voi st1[a] va st2[b]
    // neu st[i] bang mot trong hai thi ar[a][b]=(ar[a-1][b] HOAC ar[a][b-1])
    // neu st[i] bang ca hai thi ar[a][b]= MIN(ar[a-1][b] VA ar[a][b-1])
    // neu st[i] khac ca hai thi ar[a][b]= MIN(ar[a-1][b] VA ar[a][b-1]) + 1
    // tat nhien la phai check xem ar[a-1][b] co ton tai ko, neu ko thi = dp (...)
    int i=a+b-1;
    if (ar[a-1][b]==-1) {
        dp(st1, a-1, st2, b, st);
    }
    if (ar[a][b-1]==-1) {
        dp(st1, a, st2, b-1, st);
    }
    // --------------------------------------
    if (st[i]==st1[a-1] && st[i]==st2[b-1]){
        int mini = minn(ar[a-1][b], ar[a][b-1]);
        ar[a][b]=mini;
    }
    else if (st[i]!=st1[a-1] && st[i]==st2[b-1]) {
        ar[a][b]=minn(ar[a][b-1], ar[a-1][b]+1);
    }
    else if (st[i]==st1[a-1] && st[i]!=st2[b-1]) {
        ar[a][b]=minn(ar[a-1][b], ar[a][b-1]+1);
    }
    else {
        int mini = minn(ar[a-1][b], ar[a][b-1]) + 1;
        ar[a][b]=mini;
    }
}
// e   g   g
// ann    ie
// egaeg   ae   g
int main() {
    int test;
    cin>>test;
    string st, st1, st2;
    for (int it=0; it<test; it++){
        cin>>st1>>st2>>st;
        for (int i=0; i<1001; i++){
            for (int j=0; j<1001; j++){
                ar[i][j]=-1;
            }
        }
        if (st[0]==st1[0]) ar[1][0]=0; else ar[1][0]=1;
        if (st[0]==st2[0]) ar[0][1]=0; else ar[0][1]=1;//bang 1 het do
        dp(st1, st1.length(), st2, st2.length(), st); //dp(a, 1, bcd, 3, decf)
        cout<<ar[st1.length()][st2.length()]<<"\n";
    }
    return 0;
}

Submission (verdict: Accepted):
#include <bits/stdc++.h>
using namespace std;
int ar[1001][1001]; //ar[a][b] la gia tri minimum cua de bai khi day st1 co a phan tu dau tien, 
                    //st2 co b phan tu dau tien, va st co a+b phan tu dau tien

int minn (int a, int b){
    if (a<b) return a;
    return b;
}
//ab 
//ba 
//aabb

/*
void dp (string st1, int s1, string st2, int s2, string st){
    //s1 la do dai chuoi st1, s1 la do dai chuoi st2
    //s1, s2 la so luong phan tu nhe, trong ham main thi ghi la 
    // dp(st1, st1.length(), st2, st2.length(), st)
    int a = s1;
    int b = s2;
    // truong hop co so 
    if (a==0){
        if (ar[0][b-1]==-1) dp(st1, 0, st2, b-1, st);
        if (st[b-1]==st2[b-1]) ar[0][b]=ar[0][b-1];
        else ar[0][b]=ar[0][b-1]+1;
        return;
    }
    if (b==0){
        if (ar[0][b-1]==-1) dp(st1, 0, st2, b-1, st);
        if (st[a-1]==st1[a-1]) ar[a][0]=ar[a-1][0];
        else ar[a][0]=ar[a-1][0]+1;
        return;
    }
    // xu ly
    // so sanh st[i] voi st1[a] va st2[b]
    // neu st[i] bang mot trong hai thi ar[a][b]=(ar[a-1][b] HOAC ar[a][b-1])
    // neu st[i] bang ca hai thi ar[a][b]= MIN(ar[a-1][b] VA ar[a][b-1])
    // neu st[i] khac ca hai thi ar[a][b]= MIN(ar[a-1][b] VA ar[a][b-1]) + 1
    // tat nhien la phai check xem ar[a-1][b] co ton tai ko, neu ko thi = dp (...)
    int i=a+b-1;
    if (ar[a-1][b]==-1) {
        dp(st1, a-1, st2, b, st);
    }
    if (ar[a][b-1]==-1) {
        dp(st1, a, st2, b-1, st);
    }
    // --------------------------------------
    if (st[i]==st1[a-1] && st[i]==st2[b-1]){
        int mini = minn(ar[a-1][b], ar[a][b-1]);
        ar[a][b]=mini;
    }
    else if (st[i]!=st1[a-1] && st[i]==st2[b-1]) {
        ar[a][b]=minn(ar[a][b-1], ar[a-1][b]+1);
    }
    else if (st[i]==st1[a-1] && st[i]!=st2[b-1]) {
        ar[a][b]=minn(ar[a-1][b], ar[a][b-1]+1);
    }
    else {
        int mini = minn(ar[a-1][b], ar[a][b-1]) + 1;
        ar[a][b]=mini;
    }
}
*/
void dp(string st1, string st2, string st) {
    int a = st1.length();
    int b = st2.length();
    for (int i = 0; i <= a; ++i) {
        for (int j = 0; j <= b; ++j) {
            if (i == 0 && j == 0) continue;
            if (i == 0) {
                ar[i][j] = ar[i][j-1] + (st[i+j-1] != st2[j-1]);
            } else if (j == 0) {
                ar[i][j] = ar[i-1][j] + (st[i+j-1] != st1[i-1]);
            } else {
                int cost1 = ar[i-1][j] + (st[i+j-1] != st1[i-1]);
                int cost2 = ar[i][j-1] + (st[i+j-1] != st2[j-1]);
                ar[i][j] = min(cost1, cost2);
            }
        }
    }
}

// e   g   g
// ann    ie
// egaeg   ae   g
int main() {
    int test;
    cin>>test;
    string st, st1, st2;
    for (int it=0; it<test; it++){
        cin>>st1>>st2>>st;
        for (int i=0; i<1001; i++){
            for (int j=0; j<1001; j++){
                ar[i][j]=-1;
            }
        }
        if (st[0]==st1[0]) ar[1][0]=0; else ar[1][0]=1;
        if (st[0]==st2[0]) ar[0][1]=0; else ar[0][1]=1;//bang 1 het do
        dp(st1, st2, st); //dp(a, 1, bcd, 3, decf)
        cout<<ar[st1.length()][st2.length()]+1<<"\n";
    }
    return 0;
}