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

Previous submission (verdict: Wrong answer on test 1):

// author: arnavwinner

#include <bits/stdc++.h>
using namespace std;

const int INF = 1E9;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int tt;
  cin >> tt;
  while (tt--) {
    string a, b, c;
    cin >> a >> b >> c;
    a = "$" + a;
    b = "$" + b;
    c = "$" + c;
    int n = a.size();
    int m = b.size();
    vector<vector<int>> dp(n + 1, vector<int> (m + 1, -1));
    dp[0][0] = 0;
    for (int i = 1; i <= n; i++) {
      dp[i][0] = dp[i - 1][0] + a[i] != c[i];
    }
    for (int i = 1; i <= m; i++) {
      dp[0][i] = dp[0][i - 1] + b[i] != c[i];
    }
    function<int(int, int)> rec = [&] (int N, int M) -> int {
      // if (N < 0) return INF;
      // if (M < 0) return INF; // THIS DOESN'T WORK... You Initialized dp[N][M] with -1
      // now that you are checking only dp[N][M] != -1 below... that is v important to see
      if (dp[N][M] != -1) {
        return dp[N][M];
      }
      if (a[N] == c[N + M] && b[M] != c[N + M]) {
        return dp[N][M] = min(rec(N, M - 1) + 1, rec(N - 1, M));
      }
      if (a[N] != c[N + M] && b[M] == c[N + M]) {
        return dp[N][M] = min(rec(N, M - 1), rec(N - 1, M) + 1);
      }
      if (a[N] == c[N + M] && b[M] == c[N + M]) {
        return dp[N][M] = min(rec(N, M - 1), rec(N - 1, M));
      }
      if (a[N] != c[N + M] && b[M] != c[N + M]) {
        return dp[N][M] = min(rec(N, M - 1) + 1, rec(N - 1, M) + 1);
      }
    };
    cout << rec(n, m) << '\n';
    // cout << dp[n][m] << '\n';
  }
  return 0;
}

Submission (verdict: Accepted):

// author: arnavwinner

#include <bits/stdc++.h>
using namespace std;

const int INF = 1E9;
void print(vector<vector<int>> some) {
  for (auto x: some) {
    for (auto y: x) {
      cout << y << " ";
    }
    cout << '\n';
  }
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int tt;
  cin >> tt;
  int testno = 1;
  while (tt--) {
    string a, b, c;
    cin >> a >> b >> c;
    int n = a.size();
    int m = b.size();
    // cout << a << " " << b << " " << c << '\n';
    // cout << "tt: " << testno << '\n';
    // testno++;
    vector<vector<int>> dp(n + 1, vector<int> (m + 1, -1));
    dp[0][0] = 0;
    for (int i = 1; i <= n; i++) {
      dp[i][0] = dp[i - 1][0] + (a[i - 1] != c[i - 1]);
      // print(dp);
      // cout << "\n--------------------\n";
    }
    for (int i = 1; i <= m; i++) {
      // cout << b[i - 1] << c[i - 1] << '\n';
      dp[0][i] = dp[0][i - 1] + (b[i - 1] != c[i - 1]);
      // print(dp);
      // cout << "\n--------------------\n";
    }
    function<int(int, int)> rec = [&] (int N, int M) -> int {
      // if (N < 0) return INF;
      // if (M < 0) return INF; // THIS DOESN'T WORK... You Initialized dp[N][M] with -1
      // now that you are checking only dp[N][M] != -1 below... that is v important to see
      if (dp[N][M] != -1) {
        return dp[N][M];
      }
      if (a[N - 1] == c[N + M - 1] && b[M - 1] != c[N + M - 1]) {
        return dp[N][M] = min(rec(N, M - 1) + 1, rec(N - 1, M));
      }
      if (a[N - 1] != c[N + M - 1] && b[M - 1] == c[N + M - 1]) {
        return dp[N][M] = min(rec(N, M - 1), rec(N - 1, M) + 1);
      }
      if (a[N - 1] == c[N + M - 1] && b[M - 1] == c[N + M - 1]) {
        return dp[N][M] = min(rec(N, M - 1), rec(N - 1, M));
      }
      if (a[N - 1] != c[N + M - 1] && b[M - 1] != c[N + M - 1]) {
        return dp[N][M] = min(rec(N, M - 1) + 1, rec(N - 1, M) + 1);
      }
    };
    cout << rec(n, m) << '\n';
    // cout << dp[n][m] << '\n';
    // for (int i = 0; i <= n; i++) {
    //   for (int j = 0; j <= m; j++) {
    //     cout << dp[i][j] << " ";
    //   }
    //   cout << '\n';
    // }
    // cout << "---------------\n";
  }
  return 0;
}