Problem p03165 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
#define F(n)                                                                   \
  for (int i = 0; i < n; i++)                                                  \
    ;
#define FI(n)                                                                  \
  for (int i = 0; i <= n; i++)                                                 \
    ;
#define FS(a, n)                                                               \
  for (int i = a; i < n; i++)                                                  \
    ;
#define vi vector<int>;
#define vl vector<long int>;
#define vll vector<long long>;

int main() {

#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif

  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);

  string a;
  string b;
  cin >> a >> b;
  int n = a.size();
  int m = b.size();

  int dp[n + 1][m + 1];
  memset(dp, 0, sizeof(dp));

  for (int i = 0; i <= n; i++) {
    for (int j = 0; j <= m; j++) {
      if (i == 0 || j == 0) {
        dp[i][j] = 0;
      } else if (a[i - 1] == b[j - 1]) {
        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 < n + 1; i++) {
  //     for(int j = 0; j < m + 1; j++) {
  //         cout << dp[i][j] << " ";
  //     }
  //     cout << endl;
  // }

  int i = n;
  int j = m;
  string ans = "";
  while (i > 0 && j > 0) {
    if (a[i - 1] == b[j - 1]) {
      ans = ans + a[i - 1];
      i--;
      j--;
    } else if (dp[i][j - 1] > dp[i - 1][j]) {
      j--;
    } else {
      i--;
    }
  }
  reverse(ans.begin(), ans.end());
  cout << ans << endl;

  return 0;
}

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

typedef long long ll;
#define F(n)                                                                   \
  for (int i = 0; i < n; i++)                                                  \
    ;
#define FI(n)                                                                  \
  for (int i = 0; i <= n; i++)                                                 \
    ;
#define FS(a, n)                                                               \
  for (int i = a; i < n; i++)                                                  \
    ;
#define vi vector<int>;
#define vl vector<long int>;
#define vll vector<long long>;

int main() {

  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);

  string a;
  string b;
  cin >> a >> b;
  int n = a.size();
  int m = b.size();

  int dp[n + 1][m + 1];
  memset(dp, 0, sizeof(dp));

  for (int i = 0; i <= n; i++) {
    for (int j = 0; j <= m; j++) {
      if (i == 0 || j == 0) {
        dp[i][j] = 0;
      } else if (a[i - 1] == b[j - 1]) {
        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 < n + 1; i++) {
  //     for(int j = 0; j < m + 1; j++) {
  //         cout << dp[i][j] << " ";
  //     }
  //     cout << endl;
  // }

  int i = n;
  int j = m;
  string ans = "";
  while (i > 0 && j > 0) {
    if (a[i - 1] == b[j - 1]) {
      ans = ans + a[i - 1];
      i--;
      j--;
    } else if (dp[i][j - 1] > dp[i - 1][j]) {
      j--;
    } else {
      i--;
    }
  }
  reverse(ans.begin(), ans.end());
  cout << ans << endl;

  return 0;
}