Problem p03165 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bits/stdc++.h>
using namespace std;
#define fio                                                                    \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);                                                               \
  cout.tie(NULL)
#define endl "\n"
#define ll long long
#define pii pair<int, int>
#define pll pair<ll, ll>
#define F first
#define S second
#define bitcnt(v) __builtin_popcount(v)
#define all(v) v.begin(), v.end()

void solution() {

  string s, t;
  cin >> s >> t;

  int m = s.size(), n = t.size();

  pii dp[m + 1][n + 1];

  for (int i = 0; i <= m; i++) {
    dp[i][0] = {0, 0};
  }
  for (int i = 0; i <= n; i++) {
    dp[0][i] = {0, 0};
  }

  pii cur = {-1, -1};
  int best = 0;

  for (int i = 1; i <= (int)s.size(); i++) {
    for (int j = 1; j <= (int)t.size(); j++) {
      if (s[i - 1] == t[j - 1]) {
        dp[i][j] = {1 + dp[i - 1][j - 1].F, 3};
      } else {
        if (dp[i][j - 1].F < dp[i - 1][j].F) {
          dp[i][j] = {dp[i - 1][j].F, 2};
        } else {
          dp[i][j] = {dp[i][j - 1].F, 1};
        }
      }
      if (best <= dp[i][j].F) {
        best = dp[i][j].F;
        cur = {i, j};
      }
    }
  }

  string ans = "";

  while (cur.F > 0 || cur.S > 0) {
    pii p = dp[cur.F][cur.S];
    if (p.S == 3) {
      ans.push_back(s[cur.F - 1]);
      cur.F--;
      cur.S--;
    } else if (p.S == 1)
      cur.S--;
    else
      cur.F--;
  }

  reverse(all(ans));
  cout << ans << endl;
}

int main() {

  fio; // fast input output
  int t;
  t = 1;
  // cin >> t;
  for (int i = 1; i <= t; i++) {
    // GOOGLE //
    // cout << "Case #" << i << ": ";
    solution();
  }
  return 0;
}

Fixed submission:
#include <algorithm>
#include <bits/stdc++.h>
using namespace std;
#define fio                                                                    \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);                                                               \
  cout.tie(NULL)
#define endl "\n"
#define ll long long
#define pii pair<int, int>
#define pll pair<ll, ll>
#define F first
#define S second
#define bitcnt(v) __builtin_popcount(v)
#define all(v) v.begin(), v.end()

void solution() {

  string s, t;
  cin >> s >> t;

  int m = s.size(), n = t.size();

  pii dp[m + 1][n + 1];

  for (int i = 0; i <= m; i++) {
    dp[i][0] = {0, 0};
  }
  for (int i = 0; i <= n; i++) {
    dp[0][i] = {0, 0};
  }

  pii cur = {-1, -1};
  int best = 0;

  for (int i = 1; i <= (int)s.size(); i++) {
    for (int j = 1; j <= (int)t.size(); j++) {
      if (s[i - 1] == t[j - 1]) {
        dp[i][j] = {1 + dp[i - 1][j - 1].F, 3};
      } else {
        if (dp[i][j - 1].F < dp[i - 1][j].F) {
          dp[i][j] = {dp[i - 1][j].F, 2};
        } else {
          dp[i][j] = {dp[i][j - 1].F, 1};
        }
      }
      if (best <= dp[i][j].F) {
        best = dp[i][j].F;
        cur = {i, j};
      }
    }
  }

  string ans = "";

  while (cur.F > 0 && cur.S > 0) {
    pii p = dp[cur.F][cur.S];
    if (p.S == 3) {
      ans.push_back(s[cur.F - 1]);
      cur.F--;
      cur.S--;
    } else if (p.S == 1)
      cur.S--;
    else
      cur.F--;
  }

  reverse(all(ans));
  cout << ans << endl;
}

int main() {

  fio; // fast input output
  int t;
  t = 1;
  // cin >> t;
  for (int i = 1; i <= t; i++) {
    // GOOGLE //
    // cout << "Case #" << i << ": ";
    solution();
  }
  return 0;
}