Problem p03165 (C++)

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

const int MAXN = 2e3 + 5;
const int INF = 1e9 + 7;
#define ll long long
#define pb push_back
#define endl '\n'
#define fast_io                                                                \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);
#define F first
#define S second

ll poww(ll a, ll b, ll md) {
  return (!b ? 1
             : (b & 1 ? a * poww(a * a % md, b / 2, md) % md
                      : poww(a * a % md, b / 2, md) % md));
}

ll MOD(ll a) { return ((a % INF) + INF) % INF; }

ll inv(ll a) { return poww(a, INF - 2, INF); }

char a[MAXN][MAXN];
ll dp[MAXN][MAXN];
pair<int, int> par[MAXN][MAXN];

int main() {
  fast_io;
  // cout << fixed << setprecision(15);

  string s, t;
  cin >> s >> t;
  s = "." + s;
  t = "." + t;
  for (int i = 1; i < s.size(); ++i) {
    for (int j = 1; j < t.size(); ++j) {
      int first = dp[i - 1][j], second = dp[i][j - 1], third = 0;
      if (s[i] == t[j])
        third = 1 + dp[i - 1][j - 1];
      dp[i][j] = max(first, max(second, third));
      if (dp[i][j] == first)
        par[i][j] = {i - 1, j};
      else if (dp[i][j] == second)
        par[i][j] = {i, j - 1};
      else
        par[i][j] = {i - 1, j - 1};
    }
  }
  string ans;
  pair<int, int> now;
  now.F = s.size() - 1, now.S = t.size() - 1;
  while (true) {
    pair<int, int> pars = par[now.F][now.S];
    if (pars.F == now.F - 1 && pars.S == now.S - 1)
      ans += s[now.F];
    now.F = pars.F, now.S = pars.S;
    if (pars.F == 0 || pars.S == 0)
      break;
  }
  reverse(ans.begin(), ans.end());
  cout << ans;
}


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

const int MAXN = 3e3 + 5;
const int INF = 1e9 + 7;
#define ll long long
#define pb push_back
#define endl '\n'
#define fast_io                                                                \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);
#define F first
#define S second

ll poww(ll a, ll b, ll md) {
  return (!b ? 1
             : (b & 1 ? a * poww(a * a % md, b / 2, md) % md
                      : poww(a * a % md, b / 2, md) % md));
}

ll MOD(ll a) { return ((a % INF) + INF) % INF; }

ll inv(ll a) { return poww(a, INF - 2, INF); }

char a[MAXN][MAXN];
ll dp[MAXN][MAXN];
pair<int, int> par[MAXN][MAXN];

int main() {
  fast_io;
  // cout << fixed << setprecision(15);

  string s, t;
  cin >> s >> t;
  s = "." + s;
  t = "." + t;
  for (int i = 1; i < s.size(); ++i) {
    for (int j = 1; j < t.size(); ++j) {
      int first = dp[i - 1][j], second = dp[i][j - 1], third = 0;
      if (s[i] == t[j])
        third = 1 + dp[i - 1][j - 1];
      dp[i][j] = max(first, max(second, third));
      if (dp[i][j] == first)
        par[i][j] = {i - 1, j};
      else if (dp[i][j] == second)
        par[i][j] = {i, j - 1};
      else
        par[i][j] = {i - 1, j - 1};
    }
  }
  string ans;
  pair<int, int> now;
  now.F = s.size() - 1, now.S = t.size() - 1;
  while (true) {
    pair<int, int> pars = par[now.F][now.S];
    if (pars.F == now.F - 1 && pars.S == now.S - 1)
      ans += s[now.F];
    now.F = pars.F, now.S = pars.S;
    if (pars.F == 0 || pars.S == 0)
      break;
  }
  reverse(ans.begin(), ans.end());
  cout << ans;
}