Problem p03165 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define rep(i, n, m) for (int i = (n); i < (m); i++)
#define rrep(i, n, m) for (int i = (n)-1; i >= (m); i--)
#define pvec(vec)                                                              \
  {                                                                            \
    for (auto v : vec)                                                         \
      cout << v << ' ';                                                        \
    cout << endl;                                                              \
  }
#define pivec(vec)                                                             \
  {                                                                            \
    rep(i, 0, vec.size()) cout << i << ':' << vec[i] << ' ';                   \
    cout << endl;                                                              \
  }

using namespace std;
using ll = long long;

int main() {
  string s, t;
  cin >> s >> t;

  s += '$';
  t += '$';

  vector<vector<int>> dp(3010, vector<int>(3010, -1));
  vector<vector<pair<int, int>>> par(3000,
                                     vector<pair<int, int>>(3000, {-1, -1}));
  dp[0][0] = 0;
  rep(spos, 0, s.size() + 1) rep(tpos, 0, t.size() + 1) {
    if (s[spos] == t[tpos] and dp[spos + 1][tpos + 1] < dp[spos][tpos] + 1) {
      dp[spos + 1][tpos + 1] = dp[spos][tpos] + 1;
      par[spos + 1][tpos + 1] = make_pair(spos, tpos);
    }
    if (dp[spos + 1][tpos] < dp[spos][tpos]) {
      dp[spos + 1][tpos] = dp[spos][tpos];
      par[spos + 1][tpos] = make_pair(spos, tpos);
    }
    if (dp[spos][tpos + 1] < dp[spos][tpos]) {
      dp[spos][tpos + 1] = dp[spos][tpos];
      par[spos][tpos + 1] = make_pair(spos, tpos);
    }
  }

  // cout << dp[s.size()][t.size()] << endl;

  string res = "";
  pair<int, int> now = {s.size(), t.size()};
  while (now.first > 0 and now.second > 0) {
    pair<int, int> prev = par[now.first][now.second];
    // cout << now.first << ' ' << now.second << ' ' << prev.first << ' ' <<
    // prev.second << endl;

    if (now.first - prev.first + now.second - prev.second == 2)
      res += s[prev.first];

    swap(prev, now);
  }
  reverse(res.begin(), res.end());
  res.pop_back();

  cout << res << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n, m) for (int i = (n); i < (m); i++)
#define rrep(i, n, m) for (int i = (n)-1; i >= (m); i--)
#define pvec(vec)                                                              \
  {                                                                            \
    for (auto v : vec)                                                         \
      cout << v << ' ';                                                        \
    cout << endl;                                                              \
  }
#define pivec(vec)                                                             \
  {                                                                            \
    rep(i, 0, vec.size()) cout << i << ':' << vec[i] << ' ';                   \
    cout << endl;                                                              \
  }

using namespace std;
using ll = long long;

int main() {
  string s, t;
  cin >> s >> t;

  s += '$';
  t += '$';

  vector<vector<int>> dp(3010, vector<int>(3010, -1));
  vector<vector<pair<int, int>>> par(3100,
                                     vector<pair<int, int>>(3100, {-1, -1}));
  dp[0][0] = 0;
  rep(spos, 0, s.size() + 1) rep(tpos, 0, t.size() + 1) {
    if (s[spos] == t[tpos] and dp[spos + 1][tpos + 1] < dp[spos][tpos] + 1) {
      dp[spos + 1][tpos + 1] = dp[spos][tpos] + 1;
      par[spos + 1][tpos + 1] = make_pair(spos, tpos);
    }
    if (dp[spos + 1][tpos] < dp[spos][tpos]) {
      dp[spos + 1][tpos] = dp[spos][tpos];
      par[spos + 1][tpos] = make_pair(spos, tpos);
    }
    if (dp[spos][tpos + 1] < dp[spos][tpos]) {
      dp[spos][tpos + 1] = dp[spos][tpos];
      par[spos][tpos + 1] = make_pair(spos, tpos);
    }
  }

  // cout << dp[s.size()][t.size()] << endl;

  string res = "";
  pair<int, int> now = {s.size(), t.size()};
  while (now.first > 0 and now.second > 0) {
    pair<int, int> prev = par[now.first][now.second];
    // cout << now.first << ' ' << now.second << ' ' << prev.first << ' ' <<
    // prev.second << endl;

    if (now.first - prev.first + now.second - prev.second == 2)
      res += s[prev.first];

    swap(prev, now);
  }
  reverse(res.begin(), res.end());
  res.pop_back();

  cout << res << endl;
  return 0;
}