Problem p03165 (C++)

Buggy submission (status: Time Limit Exceeded):
#include "bits/stdc++.h"
#define fio                                                                    \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0);                                                                  \
  cout.tie(0)
#define all(V) (V).begin(), (V).end()
#define x first
#define y second

using namespace std;

typedef long long ll;
template <typename T> using V = vector<T>;

int main() {
  fio;
  string s, t;
  cin >> s >> t;
  V<V<int>> DP(s.size(), V<int>(t.size(), 0));
  V<V<pair<int, int>>> prev(s.size(), V<pair<int, int>>(t.size()));

  for (int si = 0; si < s.size(); si++) {
    for (int ti = 0; ti < t.size(); ti++) {
      if (s[si] == t[ti]) {
        if (si == 0 || ti == 0) {
          DP[si][ti] = 1;
          prev[si][ti] = {-1, -1};
        } else {
          DP[si][ti] = 1 + DP[si - 1][ti - 1];
          prev[si][ti] = {si - 1, ti - 1};
        }
      } else {
        if (si) {
          DP[si][ti] = max(DP[si][ti], DP[si - 1][ti]);
          if (DP[si][ti] == DP[si - 1][ti]) {
            prev[si][ti] = {si - 1, ti};
          }
        }
        if (ti) {
          DP[si][ti] = max(DP[si][ti], DP[si][ti - 1]);
          if (DP[si][ti] == DP[si][ti - 1]) {
            prev[si][ti] = {si, ti - 1};
          }
        }
      }
    }
  }
  string ans;
  pair<int, int> cur = {s.size() - 1, t.size() - 1};
  while (cur.first != -1) {
    if (s[cur.x] == t[cur.y]) {
      ans.push_back(s[cur.x]);
    }
    cur = prev[cur.x][cur.y];
  }
  reverse(all(ans));
  cout << ans;
  return 0;
}

Fixed submission:
#include "bits/stdc++.h"
#define fio                                                                    \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0);                                                                  \
  cout.tie(0)
#define all(V) (V).begin(), (V).end()
#define x first
#define y second

using namespace std;

typedef long long ll;
template <typename T> using V = vector<T>;

int main() {
  fio;
  string s, t;
  cin >> s >> t;
  V<V<int>> DP(s.size(), V<int>(t.size(), 0));
  V<V<pair<int, int>>> prev(s.size(), V<pair<int, int>>(t.size()));

  for (int si = 0; si < s.size(); si++) {
    for (int ti = 0; ti < t.size(); ti++) {
      if (s[si] == t[ti]) {
        if (si == 0 || ti == 0) {
          DP[si][ti] = 1;
          prev[si][ti] = {-1, -1};
        } else {
          DP[si][ti] = 1 + DP[si - 1][ti - 1];
          prev[si][ti] = {si - 1, ti - 1};
        }
      } else {
        prev[si][ti] = {-1, -1};
        if (si) {
          DP[si][ti] = max(DP[si][ti], DP[si - 1][ti]);
          if (DP[si][ti] == DP[si - 1][ti]) {
            prev[si][ti] = {si - 1, ti};
          }
        }
        if (ti) {
          DP[si][ti] = max(DP[si][ti], DP[si][ti - 1]);
          if (DP[si][ti] == DP[si][ti - 1]) {
            prev[si][ti] = {si, ti - 1};
          }
        }
      }
    }
  }
  string ans;
  pair<int, int> cur = {s.size() - 1, t.size() - 1};
  while (cur.first != -1) {
    if (s[cur.x] == t[cur.y]) {
      ans.push_back(s[cur.x]);
    }
    cur = prev[cur.x][cur.y];
  }
  reverse(all(ans));
  cout << ans;
  return 0;
}