Problem p02318 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define int long long int
#define MOD(x) ((x % MOD_N) + MOD_N) % MOD_N
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define FORE(i, a, b) for (int i = (a); i <= (b); ++i)
#define RFOR(i, a, b) for (int i = (b)-1; i >= (a); --i)
#define RFORE(i, a, b) for (int i = (b); i >= (a); --i)
#define REP(i, n) FOR(i, 0, n)
#define RREP(i, n) RFOR(i, 0, n)
#define ALL(c) (c).begin(), (c).end()
#define RALL(c) (c).rbegin(), (c).rend()
#define SORT(c) sort(ALL(c))
#define RSORT(c) sort(RALL(c))
#define SZ(c) (int)((c).size())
#define EACH(i, v) for (auto i = v.begin(); i != v.end(); ++i)
#define REACH(i, v) for (auto i = v.rbegin(); i != v.rend(); ++i)
#define LB(c, x) distance((c).begin(), lower_bound(ALL(c), x))
#define UB(c, x) distance((c).begin(), upper_bound(ALL(c), x))
#define COUNT(c, x) (lower_bound(ALL(c), x) - upper_bound(ALL(c), x))
#define UNIQUE(c)                                                              \\
  SORT(c);                                                                     \\
  (c).erase(unique(ALL(c)), (c).end());
#define COPY(c1, c2) copy(ALL(c1), (c2).begin())
#define EXIST(s, e) (bool)((s).find(e) != (s).end())
#define PB push_back
#define MP make_pair
#define DUMP(x) cerr << #x << " = " << (x) << endl;
#define NL cerr << endl;
using namespace std;
template <typename T, typename U> using P = pair<T, U>;
template <typename T> using V = vector<T>;
template <typename T> bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <typename T> bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <typename T> T sum(vector<T> &v) { return accumulate(ALL(v), T()); }
template <typename T> T sum(vector<T> &v, int a, int b) {
  return accumulate(v.begin() + a, v.begin() + b, T());
}
template <typename T> T max(vector<T> &v) { return *max_element(ALL(v)); }
template <typename T> T min(vector<T> &v) { return *max_element(ALL(v)); }
template <typename T> T max_index(vector<T> &v) {
  return distance((v).begin(), max_element(ALL(v)));
}
template <typename T> T min_index(vector<T> &v) {
  return distance((v).begin(), min_element(ALL(v)));
}

struct edge {
  int to, cost;
};

template <typename T> auto &operator<<(ostream &s, const vector<T> &v) {
  s << "[";
  bool a = 1;
  for (auto e : v) {
    s << (a ? "" : " ") << e;
    a = 0;
  }
  s << "]";
  return s;
}
template <typename T, typename U>
auto &operator<<(ostream &s, const pair<T, U> &p) {
  s << "(" << p.first << "," << p.second << ")";
  return s;
}
template <typename T> auto &operator<<(ostream &s, const set<T> &st) {
  s << "{";
  bool a = 1;
  for (auto e : st) {
    s << (a ? "" : " ") << e;
    a = 0;
  }
  s << "}";
  return s;
}
template <typename T, typename U>
auto &operator<<(ostream &s, const map<T, U> &m) {
  s << "{";
  bool a = 1;
  for (auto e : m) {
    s << (a ? "" : " ") << e.first << ":" << e.second;
    a = 0;
  }
  s << "}";
  return s;
}

const int INF = 1e18;
const int MOD_N = 1e9 + 7;

int LevenshteinDistance(const string &s1, const string &s2) {
  int n1 = s1.size(), n2 = s2.size();
  V<V<int>> dp(n1 + 1, V<int>(n2 + 1));
  FORE(i, 0, n1) dp[i][0] = i;
  FORE(j, 0, n2) dp[0][j] = j;
  REP(i, n1) {
    REP(j, n2) {
      dp[i + 1][j + 1] = min(dp[i][j] + (s1[i] == s2[j] ? 0 : 1),
                             min(dp[i + 1][j] + 1, dp[i][j + 1] + 1));
    }
  }
  return dp[n1][n2];
}

signed main() {
  string s1, s2;
  cin >> s1 >> s2;
  cout << LevenshteinDistance(s1, s2) << endl;

  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define int long long int
#define MOD(x) ((x % MOD_N) + MOD_N) % MOD_N
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define FORE(i, a, b) for (int i = (a); i <= (b); ++i)
#define RFOR(i, a, b) for (int i = (b)-1; i >= (a); --i)
#define RFORE(i, a, b) for (int i = (b); i >= (a); --i)
#define REP(i, n) FOR(i, 0, n)
#define RREP(i, n) RFOR(i, 0, n)
#define ALL(c) (c).begin(), (c).end()
#define RALL(c) (c).rbegin(), (c).rend()
#define SORT(c) sort(ALL(c))
#define RSORT(c) sort(RALL(c))
#define SZ(c) (int)((c).size())
#define EACH(i, v) for (auto i = v.begin(); i != v.end(); ++i)
#define REACH(i, v) for (auto i = v.rbegin(); i != v.rend(); ++i)
#define LB(c, x) distance((c).begin(), lower_bound(ALL(c), x))
#define UB(c, x) distance((c).begin(), upper_bound(ALL(c), x))
#define COUNT(c, x) (lower_bound(ALL(c), x) - upper_bound(ALL(c), x))
#define UNIQUE(c)                                                              \\
  SORT(c);                                                                     \\
  (c).erase(unique(ALL(c)), (c).end());
#define COPY(c1, c2) copy(ALL(c1), (c2).begin())
#define EXIST(s, e) (bool)((s).find(e) != (s).end())
#define PB push_back
#define MP make_pair
#define DUMP(x) cerr << #x << " = " << (x) << endl;
#define NL cerr << endl;
using namespace std;
template <typename T, typename U> using P = pair<T, U>;
template <typename T> using V = vector<T>;
template <typename T> bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <typename T> bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <typename T> T sum(vector<T> &v) { return accumulate(ALL(v), T()); }
template <typename T> T sum(vector<T> &v, int a, int b) {
  return accumulate(v.begin() + a, v.begin() + b, T());
}
template <typename T> T max(vector<T> &v) { return *max_element(ALL(v)); }
template <typename T> T min(vector<T> &v) { return *max_element(ALL(v)); }
template <typename T> T max_index(vector<T> &v) {
  return distance((v).begin(), max_element(ALL(v)));
}
template <typename T> T min_index(vector<T> &v) {
  return distance((v).begin(), min_element(ALL(v)));
}

struct edge {
  int to, cost;
};

template <typename T> auto &operator<<(ostream &s, const vector<T> &v) {
  s << "[";
  bool a = 1;
  for (auto e : v) {
    s << (a ? "" : " ") << e;
    a = 0;
  }
  s << "]";
  return s;
}
template <typename T, typename U>
auto &operator<<(ostream &s, const pair<T, U> &p) {
  s << "(" << p.first << "," << p.second << ")";
  return s;
}
template <typename T> auto &operator<<(ostream &s, const set<T> &st) {
  s << "{";
  bool a = 1;
  for (auto e : st) {
    s << (a ? "" : " ") << e;
    a = 0;
  }
  s << "}";
  return s;
}
template <typename T, typename U>
auto &operator<<(ostream &s, const map<T, U> &m) {
  s << "{";
  bool a = 1;
  for (auto e : m) {
    s << (a ? "" : " ") << e.first << ":" << e.second;
    a = 0;
  }
  s << "}";
  return s;
}

const int INF = 1e18;
const int MOD_N = 1e9 + 7;

signed main() {
  string s1, s2;
  cin >> s1 >> s2;
  int n1 = s1.size(), n2 = s2.size();
  V<V<int>> dp(n1 + 1, V<int>(n2 + 1));
  FORE(i, 0, n1) {
    FORE(j, 0, n2) {
      if (i < n1 && j < n2 && s1[i] == s2[j]) {
        chmax(dp[i + 1][j + 1], dp[i][j] + 1);
      }
      if (i < n1)
        chmax(dp[i + 1][j], dp[i][j]);
      if (j < n2)
        chmax(dp[i][j + 1], dp[i][j]);
    }
  }

  int i = n1 - 1, j = n2 - 1;
  stack<char> lcs;
  while (i >= 0 && j >= 0) {
    if (dp[i + 1][j + 1] == dp[i][j] + 1) {
      lcs.push(s1[i]);
      i--;
      j--;
    } else if (dp[i + 1][j + 1] == dp[i + 1][j]) {
      j--;
    } else if (dp[i + 1][j + 1] == dp[i][j + 1]) {
      i--;
    }
  }
  int len = lcs.size();

  V<int> id1, id2;
  id1.push_back(-1);
  id2.push_back(-1);
  i = 0;
  j = 0;
  while (!lcs.empty()) {
    char top = lcs.top();
    lcs.pop();
    while (s1[i] != top)
      i++;
    while (s2[j] != top)
      j++;
    id1.push_back(i);
    id2.push_back(j);
  }
  id1.push_back(n1);
  id2.push_back(n2);

  int total = 0;
  REP(k, len + 1) {
    total += min(id1[k + 1] - id1[k], id2[k + 1] - id2[k]) - 1;
  }

  int ans = n1 + n2 - len * 2 - total;
  cout << ans << endl;

  return 0;
}