Problem p03165 (C++)

Buggy submission (status: Runtime Error):
/*----------------by syr----------------*/
/*
  -----  -----  -----
  |   |  |---|    _/
  |   |  | \_    /
  -----  |   \  -----

  |---\  \   /  |\  /|
  |   |   \_/   | \/ |
  |   |   / \   |    |
  |---/  /   \  |    |
*/
#include <bits/stdc++.h>

using namespace std;

#define PH push
#define MP make_pair
#define PB push_back
#define fst first
#define snd second
#define FOR(i, x, y) for (int i = (x); i < (y); ++i)
#define REP(i, x, y) for (int i = (x); i <= (y); ++i)
#define x0 x0123456789
#define y0 y0123456789
#define x1 x1234567890
#define y1 y1234567890
#define x2 x2345678901
#define y2 y2345678901
typedef double db;
typedef long long ll;
typedef long double ldb;
typedef pair<int, int> pii;

const int INF = 1e9 + 7;
const int maxn = 1e3 + 5;

int n, m;
int dp[maxn][maxn];
char s[maxn], t[maxn];
pii f[maxn][maxn];
vector<char> ans;

int main() {
  scanf("%s%s", s + 1, t + 1);
  n = strlen(s + 1);
  m = strlen(t + 1);
  dp[0][0] = 0;
  REP(i, 1, n) {
    REP(j, 1, m) {
      if (s[i] == t[j]) {
        dp[i][j] = max(dp[i][j], dp[i - 1][j - 1] + 1);
        if (dp[i][j] == dp[i - 1][j - 1] + 1)
          f[i][j] = MP(i - 1, j - 1);
      }
      dp[i][j] = max(dp[i][j], dp[i - 1][j]);
      if (dp[i][j] == dp[i - 1][j])
        f[i][j] = MP(i - 1, j);
      dp[i][j] = max(dp[i][j], dp[i][j - 1]);
      if (dp[i][j] == dp[i][j - 1])
        f[i][j] = MP(i, j - 1);
    }
  }
  int x = n, y = m;
  while (x || y) {
    if (f[x][y] == MP(x - 1, y - 1))
      ans.PB(s[x]);
    int nx = f[x][y].fst, ny = f[x][y].snd;
    x = nx, y = ny;
  }
  for (int i = ans.size() - 1; i >= 0; --i)
    printf("%c", ans[i]);
  return 0;
}


Fixed submission:
/*----------------by syr----------------*/
/*
  -----  -----  -----
  |   |  |---|    _/
  |   |  | \_    /
  -----  |   \  -----

  |---\  \   /  |\  /|
  |   |   \_/   | \/ |
  |   |   / \   |    |
  |---/  /   \  |    |
*/
#include <bits/stdc++.h>

using namespace std;

#define PH push
#define MP make_pair
#define PB push_back
#define fst first
#define snd second
#define FOR(i, x, y) for (int i = (x); i < (y); ++i)
#define REP(i, x, y) for (int i = (x); i <= (y); ++i)
#define x0 x0123456789
#define y0 y0123456789
#define x1 x1234567890
#define y1 y1234567890
#define x2 x2345678901
#define y2 y2345678901
typedef double db;
typedef long long ll;
typedef long double ldb;
typedef pair<int, int> pii;

const int INF = 1e9 + 7;
const int maxn = 3e3 + 5;

int n, m;
int dp[maxn][maxn];
char s[maxn], t[maxn];
pii f[maxn][maxn];
vector<char> ans;

int main() {
  scanf("%s%s", s + 1, t + 1);
  n = strlen(s + 1);
  m = strlen(t + 1);
  dp[0][0] = 0;
  REP(i, 1, n) {
    REP(j, 1, m) {
      if (s[i] == t[j]) {
        dp[i][j] = max(dp[i][j], dp[i - 1][j - 1] + 1);
        if (dp[i][j] == dp[i - 1][j - 1] + 1)
          f[i][j] = MP(i - 1, j - 1);
      }
      dp[i][j] = max(dp[i][j], dp[i - 1][j]);
      if (dp[i][j] == dp[i - 1][j])
        f[i][j] = MP(i - 1, j);
      dp[i][j] = max(dp[i][j], dp[i][j - 1]);
      if (dp[i][j] == dp[i][j - 1])
        f[i][j] = MP(i, j - 1);
    }
  }
  int x = n, y = m;
  while (x || y) {
    if (f[x][y] == MP(x - 1, y - 1))
      ans.PB(s[x]);
    int nx = f[x][y].fst, ny = f[x][y].snd;
    x = nx, y = ny;
  }
  for (int i = ans.size() - 1; i >= 0; --i)
    printf("%c", ans[i]);
  return 0;
}