Problem p03165 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cctype>
#include <cmath>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
#define REP(i, n) for (int i = 0; i < (n); ++i)
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
const int INF = 100100100;
const ll INFLL = 100100100100100100;
const int MOD = (int)1e9 + 7;
const ll MODLL = (ll)1e9 + 7;
const double EPS = 1e-9;

int main() {

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

  vector<vector<int>> dp(3000, vector<int>(3000, 0));
  vector<vector<int>> prev_i(3000, vector<int>(3000, 0));
  vector<vector<int>> prev_j(3000, vector<int>(3000, 0));

  for (int i = 0; i < s.length(); i++) {
    for (int j = 0; j < t.length(); j++) {
      if (s[i] == t[j]) {
        dp[i + 1][j + 1] = dp[i][j] + 1;
        prev_i[i + 1][j + 1] = i;
        prev_j[i + 1][j + 1] = j;
      } else {
        if (dp[i + 1][j] > dp[i][j + 1]) {
          dp[i + 1][j + 1] = dp[i + 1][j];
          prev_i[i + 1][j + 1] = i + 1;
          prev_j[i + 1][j + 1] = j;
        } else {
          dp[i + 1][j + 1] = dp[i][j + 1];
          prev_i[i + 1][j + 1] = i;
          prev_j[i + 1][j + 1] = j + 1;
        }
      }
    }
  }

  int i = s.length();
  int j = t.length();
  string ans = "";
  while (!(i == 0 && j == 0)) {
    int next_i = prev_i[i][j];
    int next_j = prev_j[i][j];
    if (next_i < i && next_j < j) {
      ans = s[next_i] + ans;
    }
    i = next_i;
    j = next_j;
  }
  cout << ans << endl;
  return 0;
}


Fixed submission:
#include <algorithm>
#include <bitset>
#include <cctype>
#include <cmath>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
#define REP(i, n) for (int i = 0; i < (n); ++i)
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
const int INF = 100100100;
const ll INFLL = 100100100100100100;
const int MOD = (int)1e9 + 7;
const ll MODLL = (ll)1e9 + 7;
const double EPS = 1e-9;

int main() {

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

  vector<vector<int>> dp(3010, vector<int>(3010, 0));
  vector<vector<int>> prev_i(3010, vector<int>(3010, 0));
  vector<vector<int>> prev_j(3010, vector<int>(3010, 0));

  for (int i = 0; i < s.length(); i++) {
    for (int j = 0; j < t.length(); j++) {
      if (s[i] == t[j]) {
        dp[i + 1][j + 1] = dp[i][j] + 1;
        prev_i[i + 1][j + 1] = i;
        prev_j[i + 1][j + 1] = j;
      } else {
        if (dp[i + 1][j] > dp[i][j + 1]) {
          dp[i + 1][j + 1] = dp[i + 1][j];
          prev_i[i + 1][j + 1] = i + 1;
          prev_j[i + 1][j + 1] = j;
        } else {
          dp[i + 1][j + 1] = dp[i][j + 1];
          prev_i[i + 1][j + 1] = i;
          prev_j[i + 1][j + 1] = j + 1;
        }
      }
    }
  }

  int i = s.length();
  int j = t.length();
  string ans = "";
  while (!(i == 0 && j == 0)) {
    int next_i = prev_i[i][j];
    int next_j = prev_j[i][j];
    if (next_i < i && next_j < j) {
      ans = s[next_i] + ans;
    }
    i = next_i;
    j = next_j;
  }
  cout << ans << endl;
  return 0;
}