Problem p03165 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <vector>

using namespace std;
typedef long long ll;

typedef pair<int, int> P;

int dp[3000][3000];
P pre[3000][3000];

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout << setprecision(10) << fixed;
  string s, t;
  cin >> s >> t;
  int len = 0;
  P fin = P(0, 0);
  for (int i = 0; i < s.size(); i++) {
    for (int j = 0; j < t.size(); j++) {
      if (s[i] == t[j]) {
        if (dp[i][j] + 1 > dp[i + 1][j] && dp[i][j] + 1 > dp[i][j + 1]) {
          dp[i + 1][j + 1] = dp[i][j] + 1;
          pre[i + 1][j + 1] = P(i, j);
          if (len < dp[i + 1][j + 1]) {
            fin = P(i, j);
            len = dp[i + 1][j + 1];
          }
        } else if (dp[i][j + 1] > dp[i + 1][j]) {
          dp[i + 1][j + 1] = dp[i][j + 1];
          pre[i + 1][j + 1] = P(i, j + 1);
        } else {
          dp[i + 1][j + 1] = dp[i + 1][j];
          pre[i + 1][j + 1] = P(i + 1, j);
        }
      } else {
        if (dp[i][j + 1] > dp[i + 1][j]) {
          dp[i + 1][j + 1] = dp[i][j + 1];
          pre[i + 1][j + 1] = P(i, j + 1);
        } else {
          dp[i + 1][j + 1] = dp[i + 1][j];
          pre[i + 1][j + 1] = P(i + 1, j);
        }
      }
    }
  }
  // cout << dp[s.size()][t.size()] << endl;
  vector<char> ans;
  int l = fin.first + 1, r = fin.second + 1;
  // cout << l << ' ' << r << endl;
  while (l > 0 && r > 0) {
    P p = pre[l][r];
    if (s[l - 1] == t[r - 1] && l != p.first && r != p.second) {
      ans.push_back(s[l - 1]);
      // cout << l << ' ' << r << endl;
    }
    l = p.first;
    r = p.second;
  }
  // cout << ans.size() << endl;
  for (int i = 0; i < ans.size(); i++) {
    cout << ans[ans.size() - i - 1];
  }
  cout << endl;
}

Fixed submission:
#include <algorithm>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <vector>

using namespace std;
typedef long long ll;

typedef pair<int, int> P;

int dp[3001][3001];
P pre[3001][3001];

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout << setprecision(10) << fixed;
  string s, t;
  cin >> s >> t;
  int len = 0;
  P fin = P(0, 0);
  for (int i = 0; i < s.size(); i++) {
    for (int j = 0; j < t.size(); j++) {
      if (s[i] == t[j]) {
        if (dp[i][j] + 1 > dp[i + 1][j] && dp[i][j] + 1 > dp[i][j + 1]) {
          dp[i + 1][j + 1] = dp[i][j] + 1;
          pre[i + 1][j + 1] = P(i, j);
          if (len < dp[i + 1][j + 1]) {
            fin = P(i, j);
            len = dp[i + 1][j + 1];
          }
        } else if (dp[i][j + 1] > dp[i + 1][j]) {
          dp[i + 1][j + 1] = dp[i][j + 1];
          pre[i + 1][j + 1] = P(i, j + 1);
        } else {
          dp[i + 1][j + 1] = dp[i + 1][j];
          pre[i + 1][j + 1] = P(i + 1, j);
        }
      } else {
        if (dp[i][j + 1] > dp[i + 1][j]) {
          dp[i + 1][j + 1] = dp[i][j + 1];
          pre[i + 1][j + 1] = P(i, j + 1);
        } else {
          dp[i + 1][j + 1] = dp[i + 1][j];
          pre[i + 1][j + 1] = P(i + 1, j);
        }
      }
    }
  }
  // cout << dp[s.size()][t.size()] << endl;
  vector<char> ans;
  int l = fin.first + 1, r = fin.second + 1;
  // cout << l << ' ' << r << endl;
  while (l > 0 && r > 0) {
    P p = pre[l][r];
    if (s[l - 1] == t[r - 1] && l != p.first && r != p.second) {
      ans.push_back(s[l - 1]);
      // cout << l << ' ' << r << endl;
    }
    l = p.first;
    r = p.second;
  }
  // cout << ans.size() << endl;
  for (int i = 0; i < ans.size(); i++) {
    cout << ans[ans.size() - i - 1];
  }
  cout << endl;
}