Problem p03165 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ull unsigned long long
#define FAST_IO                                                                \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL)
#define trace1(x) cerr << #x << ": " << x << endl
#define trace2(x, y) cerr << #x << ": " << x << " | " << #y << ": " << y << endl
#define trace3(x, y, z)                                                        \
  cerr << #x << ":" << x << " | " << #y << ": " << y << " | " << #z << ": "    \
       << z << endl
#define trace4(a, b, c, d)                                                     \
  cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": "   \
       << c << " | " << #d << ": " << d << endl
#define trace5(a, b, c, d, e)                                                  \
  cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": "   \
       << c << " | " << #d << ": " << d << " | " << #e << ": " << e << endl
#define trace6(a, b, c, d, e, f)                                               \
  cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": "   \
       << c << " | " << #d << ": " << d << " | " << #e << ": " << e << " | "   \
       << #f << ": " << f << endl

int main() {
  FAST_IO;
  string s, t;
  cin >> s >> t;
  int n1 = s.length();
  int n2 = t.length();

  int dp[n1 + 1][n2 + 1];
  for (int i = 0; i < n1 + 1; i++) {
    dp[i][n2] = 0;
  }
  for (int i = 0; i < n2 + 1; i++) {
    dp[n1][i] = 0;
  }

  for (int i = n1 - 1; i >= 0; i--) {
    for (int j = n2 - 1; j >= 0; j--) {
      if (s[i] == t[j]) {
        dp[i][j] = dp[i + 1][j + 1] + 1;
      } else {
        dp[i][j] = max(dp[i + 1][j], dp[i][j + 1]);
      }
    }
  }
  /* for(int i=0; i <= n1; i++){
       for(int j= 0; j <=n2; j++){
           cout << dp[i][j] << " ";
       }
       cout << endl;
   }*/
  string ans;
  int i = 0;
  int j = 0;
  while (i < n1 || j < n2) {
    if (s[i] == t[j]) {
      ans.push_back(s[i]);
      i++;
      j++;
    } else {
      if (dp[i + 1][j] == dp[i][j]) {
        i++;
      } else {
        j++;
      }
    }
  }

  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ull unsigned long long
#define FAST_IO                                                                \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL)
#define trace1(x) cerr << #x << ": " << x << endl
#define trace2(x, y) cerr << #x << ": " << x << " | " << #y << ": " << y << endl
#define trace3(x, y, z)                                                        \
  cerr << #x << ":" << x << " | " << #y << ": " << y << " | " << #z << ": "    \
       << z << endl
#define trace4(a, b, c, d)                                                     \
  cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": "   \
       << c << " | " << #d << ": " << d << endl
#define trace5(a, b, c, d, e)                                                  \
  cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": "   \
       << c << " | " << #d << ": " << d << " | " << #e << ": " << e << endl
#define trace6(a, b, c, d, e, f)                                               \
  cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": "   \
       << c << " | " << #d << ": " << d << " | " << #e << ": " << e << " | "   \
       << #f << ": " << f << endl

int main() {
  FAST_IO;
  string s, t;
  cin >> s >> t;
  int n1 = s.length();
  int n2 = t.length();

  int dp[n1 + 1][n2 + 1];
  for (int i = 0; i < n1 + 1; i++) {
    dp[i][n2] = 0;
  }
  for (int i = 0; i < n2 + 1; i++) {
    dp[n1][i] = 0;
  }

  for (int i = n1 - 1; i >= 0; i--) {
    for (int j = n2 - 1; j >= 0; j--) {
      if (s[i] == t[j]) {
        dp[i][j] = dp[i + 1][j + 1] + 1;
      } else {
        dp[i][j] = max(dp[i + 1][j], dp[i][j + 1]);
      }
    }
  }
  /* for(int i=0; i <= n1; i++){
       for(int j= 0; j <=n2; j++){
           cout << dp[i][j] << " ";
       }
       cout << endl;
   }*/
  string ans;
  int i = 0;
  int j = 0;
  while (i < n1 && j < n2) {
    if (s[i] == t[j]) {
      ans.push_back(s[i]);
      i++;
      j++;
    } else {
      if (dp[i + 1][j] == dp[i][j]) {
        i++;
      } else {
        j++;
      }
    }
  }

  cout << ans << endl;
  return 0;
}