Problem p03165 (C++)

Buggy submission (status: Runtime Error):
/// Bismillahir Rahmanir Rahim

#include <bits/stdc++.h>

using namespace std;
using namespace __gnu_cxx;

#define FASTIO                                                                 \
  ios::sync_with_stdio(false);                                                 \
  cin.tie(0);                                                                  \
  cout.tie(0)
#define IN freopen("input.txt", "r+", stdin)
#define OUT freopen("output.txt", "w+", stdout)
#define DBG cout << "Hi there" << endl;
#define all(x) (x).begin(), (x).end()
#define fap(x) cout << " -- " << #x << ": " << (x) << "\n"
#define CHECK_BIT(var, pos) ((var) & (1 << (pos)))

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<pair<int, int>> vpii;

const ll MAX = 998244353;
const ll MAX2 = 1271309838631957LL;
long double PI = acosl(-1);
const ll INF = 1e15;
const int NMAX = 1e5 + 5;
const int MAXLG = log2(NMAX) + 1;
const int oo = 1e5 + 10;
const int sz = 100005;
const int MOD = 10000019;
const double EPS = 1e-9;

/// code starts from here

void solve() {
  string a, b;
  cin >> a >> b;

  int n = a.size();
  int m = b.size();
  vector<vector<int>> dp(n + 1, vector<int>(m + 1, 0));

  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]);
    }
  }

  stack<char> s;
  int i = n, j = m;
  while (i > 0 && j > 0) {
    if (a[i - 1] == b[j - 1]) {
      i--;
      j--;
      s.push(a[i]);
    } else if (dp[i - 1][j] > dp[i][j - 1]) {
      i--;
    } else
      j--;
  }

  while (!s.empty()) {
    cout << s.top();
    s.pop();
  }

  cout << endl;
}

int main() {
#ifndef ONLINE_JUDGE
  IN;
#endif
  FASTIO;
  int tc = 1;
  // cin >> tc;

  while (tc--) {
    solve();
  }
}


Fixed submission:
/// Bismillahir Rahmanir Rahim

#include <bits/stdc++.h>

using namespace std;
using namespace __gnu_cxx;

#define FASTIO                                                                 \
  ios::sync_with_stdio(false);                                                 \
  cin.tie(0);                                                                  \
  cout.tie(0)
#define IN freopen("input.txt", "r+", stdin)
#define OUT freopen("output.txt", "w+", stdout)
#define DBG cout << "Hi there" << endl;
#define all(x) (x).begin(), (x).end()
#define fap(x) cout << " -- " << #x << ": " << (x) << "\n"
#define CHECK_BIT(var, pos) ((var) & (1 << (pos)))

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<pair<int, int>> vpii;

const ll MAX = 998244353;
const ll MAX2 = 1271309838631957LL;
long double PI = acosl(-1);
const ll INF = 1e15;
const int NMAX = 1e5 + 5;
const int MAXLG = log2(NMAX) + 1;
const int oo = 1e5 + 10;
const int sz = 100005;
const int MOD = 10000019;
const double EPS = 1e-9;

/// code starts from here

void solve() {
  string a, b;
  cin >> a >> b;

  int n = a.size();
  int m = b.size();
  vector<vector<int>> dp(n + 1, vector<int>(m + 1, 0));

  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]);
    }
  }

  stack<char> s;
  int i = n, j = m;
  while (i > 0 && j > 0) {
    if (a[i - 1] == b[j - 1]) {
      i--;
      j--;
      s.push(a[i]);
    } else if (dp[i - 1][j] > dp[i][j - 1]) {
      i--;
    } else
      j--;
  }

  while (!s.empty()) {
    cout << s.top();
    s.pop();
  }

  cout << endl;
}

int main() {
  // #ifndef ONLINE_JUDGE
  // IN;
  // #endif
  FASTIO;
  int tc = 1;
  // cin >> tc;

  while (tc--) {
    solve();
  }
}