Problem p03165 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define all(cont) cont.begin(), cont.end()
#define pb push_back
#define fi first
#define se second
#define DEBUG(x) cerr << (#x) << ": " << (x) << '\n'

using namespace std;

typedef pair<int, int> pii;
typedef vector<int> vi;
typedef long long ll;
typedef unsigned long long ull;

template <class T> bool uin(T &a, T b) {
  return (a <= b ? false : (a = b, true));
}
template <class T> bool uax(T &a, T b) {
  return (a >= b ? false : (a = b, true));
}

// ifstream f(".in");
// ofstream g(".out");

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
#ifdef LOCAL_DEFINE
  freopen(".in", "r", stdin);
#endif

  string s, t;
  cin >> s >> t;
  int n = s.length();
  int m = t.length();
  s = " " + s;
  t = " " + t;
  vector<vector<int>> dp(n + 2, vector<int>(m + 2));
  vector<vector<pair<int, int>>> from(n + 2,
                                      vector<pair<int, int>>(m + 2, {-1, -1}));

  auto setFrom = [&](int val, pair<int, int> curr, pair<int, int> old) {
    if (uax(dp[curr.first][curr.second], val)) {
      from[curr.first][curr.second] = old;
    }
  };

  int ans = 0;
  pair<int, int> p = {0, 0};
  for (int i = 1; i <= n; ++i) {
    for (int j = 1; j <= m; ++j) {
      if (s[i] == t[j]) {
        setFrom(1 + dp[i - 1][j - 1], {i, j}, {i - 1, j - 1});
      }
      setFrom(dp[i - 1][j], {i, j}, {i - 1, j});
      setFrom(dp[i][j - 1], {i, j}, {i, j - 1});
      if (uax(ans, dp[i][j])) {
        p = {i, j};
      }
    }
  }

  if (ans == 0) {
    cout << "";
    return 0;
  }

  string lcs;
  while (p.first != 0 && p.second != 0) {
    if (s[p.first] == t[p.second]) {
      lcs += s[p.first];
    }
    p = from[p.first][p.second];
  }

  reverse(lcs.begin(), lcs.end());

  cout << lcs << '\n';

#ifdef LOCAL_DEFINE
  cerr << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.\n";
#endif
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define all(cont) cont.begin(), cont.end()
#define pb push_back
#define fi first
#define se second
#define DEBUG(x) cerr << (#x) << ": " << (x) << '\n'

using namespace std;

typedef pair<int, int> pii;
typedef vector<int> vi;
typedef long long ll;
typedef unsigned long long ull;

template <class T> bool uin(T &a, T b) {
  return (a <= b ? false : (a = b, true));
}
template <class T> bool uax(T &a, T b) {
  return (a >= b ? false : (a = b, true));
}

// ifstream f(".in");
// ofstream g(".out");

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
#ifdef LOCAL_DEFINE
  freopen(".in", "r", stdin);
#endif

  string s, t;
  cin >> s >> t;
  int n = s.length();
  int m = t.length();
  s = " " + s;
  t = " " + t;
  vector<vector<int>> dp(n + 2, vector<int>(m + 2));
  vector<vector<pair<int, int>>> from(n + 2,
                                      vector<pair<int, int>>(m + 2, {0, 0}));

  auto setFrom = [&](int val, pair<int, int> curr, pair<int, int> old) {
    if (uax(dp[curr.first][curr.second], val)) {
      from[curr.first][curr.second] = old;
    }
  };

  int ans = 0;
  pair<int, int> p = {0, 0};
  for (int i = 1; i <= n; ++i) {
    for (int j = 1; j <= m; ++j) {
      if (s[i] == t[j]) {
        setFrom(1 + dp[i - 1][j - 1], {i, j}, {i - 1, j - 1});
      }
      setFrom(dp[i - 1][j], {i, j}, {i - 1, j});
      setFrom(dp[i][j - 1], {i, j}, {i, j - 1});
      if (uax(ans, dp[i][j])) {
        p = {i, j};
      }
    }
  }

  if (ans == 0) {
    cout << "";
    return 0;
  }

  string lcs;
  while (p.first != 0 && p.second != 0) {
    if (s[p.first] == t[p.second]) {
      lcs += s[p.first];
    }
    p = from[p.first][p.second];
  }

  reverse(lcs.begin(), lcs.end());

  cout << lcs << '\n';

#ifdef LOCAL_DEFINE
  cerr << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.\n";
#endif
  return 0;
}