Problem p03165 (C++)

Buggy submission (status: Runtime Error):
// _/\\\\____________/\\\\______/\\\\\\\\\______/\\\________/\\\___/\\\\\\\\/\\____/\\\\\\\\\\_|
//  _\/\\\\\\________/\\\\\\____/\\\\\\\\\\\\\___\/\\\________/\\\__\/\\\____\/\\\__\////\\\///__|
//   _\/\\\//\\\____/\\\//\\\___/\\\/////////\\\__\/\\\________/\\\__\/\\\____\/\\\\____\/\\\_____|
//    _\/\\\\///\\\/\\\/_\/\\\__\/\\\_______\/\\\__\/\\\\\\\\\\\\\\\__\/\\\____\/\\\\____\/\\\_____|
//     _\/\\\__\///\\\/___\/\\\__\/\\\\\\\\\\\\\\\__\/\\\/////////\\\__\/\\\____\/\\\\____\/\\\_____|
//      _\/\\\____\///_____\/\\\__\/\\\/////////\\\__\/\\\_______\/\\\__\/\\\____\/\\\_____\/\\\_____|
//       _\/\\\_____________\/\\\__\/\\\_______\/\\\__\/\\\_______\/\\\__\/\\\___/\\\\______\/\\\_____|
//        _\/\\\_____________\/\\\__\/\\\_______\/\\\__\/\\\_______\/\\\__\/\\\\\\/\\_____/\\\\\\\\\\__|
//         _\///______________\///___\///________\///___\///________\///___\/////////_____\//////////___|
#include <bits/stdc++.h>
using namespace std;
typedef string str;
typedef long long ll;
typedef long double ld;
typedef vector<int> vi;
typedef vector<char> vc;
typedef vector<string> vs;
typedef vector<long long> vl;
typedef vector<pair<int, int>> vpi;
#define hurry                                                                  \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define setp setprecision
#define pop pop_back()
#define pb push_back
#define mp make_pair
#define beg begin()
#define sc second
#define sz size()
#define fr first
#define endl '\n'
//=======================================================================================================================
// global
int dp[1003][1002];
pair<int, int> par[1008][1009];
vc v;
//=======================================================================================================================
// functions
void lcs(str s, str t) {
  for (int i = 1; i <= s.sz; i++) {
    for (int j = 1; j <= t.sz; j++) {
      if (s[i - 1] == t[j - 1]) {
        dp[i][j] = dp[i - 1][j - 1] + 1;
        par[i][j].fr = i - 1;
        par[i][j].sc = j - 1;
      } else {
        if (dp[i - 1][j] >= dp[i][j - 1]) {
          dp[i][j] = dp[i - 1][j];
          par[i][j].fr = i - 1;
          par[i][j].sc = j;
        } else {
          dp[i][j] = dp[i][j - 1];
          par[i][j].fr = i;
          par[i][j].sc = j - 1;
        }
      }
    }
  }
}
//=======================================================================================================================
int main() {
  str s, t;
  cin >> s >> t;
  lcs(s, t);
  int n = s.sz;
  int m = t.sz;
  while (n > 0 and m > 0) {
    if (par[n][m].fr == n - 1 and par[n][m].sc == m - 1)
      v.pb(s[n - 1]);
    pair<int, int> tmp = par[n][m];
    n = tmp.fr;
    m = tmp.sc;
  }
  for (int i = v.sz - 1; i >= 0; i--) {
    cout << v[i];
  }
}
//========================================================================================================================
// 4
// 1 4 2 3
// 4 1 2 3
// 1 2 4 3


Fixed submission:
// _/\\\\____________/\\\\______/\\\\\\\\\______/\\\________/\\\___/\\\\\\\\/\\____/\\\\\\\\\\_|
//  _\/\\\\\\________/\\\\\\____/\\\\\\\\\\\\\___\/\\\________/\\\__\/\\\____\/\\\__\////\\\///__|
//   _\/\\\//\\\____/\\\//\\\___/\\\/////////\\\__\/\\\________/\\\__\/\\\____\/\\\\____\/\\\_____|
//    _\/\\\\///\\\/\\\/_\/\\\__\/\\\_______\/\\\__\/\\\\\\\\\\\\\\\__\/\\\____\/\\\\____\/\\\_____|
//     _\/\\\__\///\\\/___\/\\\__\/\\\\\\\\\\\\\\\__\/\\\/////////\\\__\/\\\____\/\\\\____\/\\\_____|
//      _\/\\\____\///_____\/\\\__\/\\\/////////\\\__\/\\\_______\/\\\__\/\\\____\/\\\_____\/\\\_____|
//       _\/\\\_____________\/\\\__\/\\\_______\/\\\__\/\\\_______\/\\\__\/\\\___/\\\\______\/\\\_____|
//        _\/\\\_____________\/\\\__\/\\\_______\/\\\__\/\\\_______\/\\\__\/\\\\\\/\\_____/\\\\\\\\\\__|
//         _\///______________\///___\///________\///___\///________\///___\/////////_____\//////////___|
#include <bits/stdc++.h>
using namespace std;
typedef string str;
typedef long long ll;
typedef long double ld;
typedef vector<int> vi;
typedef vector<char> vc;
typedef vector<string> vs;
typedef vector<long long> vl;
typedef vector<pair<int, int>> vpi;
#define hurry                                                                  \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define setp setprecision
#define pop pop_back()
#define pb push_back
#define mp make_pair
#define beg begin()
#define sc second
#define sz size()
#define fr first
#define endl '\n'
//=======================================================================================================================
// global
int dp[3003][3002];
pair<int, int> par[3008][3009];
vc v;
//=======================================================================================================================
// functions
void lcs(str s, str t) {
  for (int i = 1; i <= s.sz; i++) {
    for (int j = 1; j <= t.sz; j++) {
      if (s[i - 1] == t[j - 1]) {
        dp[i][j] = dp[i - 1][j - 1] + 1;
        par[i][j].fr = i - 1;
        par[i][j].sc = j - 1;
      } else {
        if (dp[i - 1][j] >= dp[i][j - 1]) {
          dp[i][j] = dp[i - 1][j];
          par[i][j].fr = i - 1;
          par[i][j].sc = j;
        } else {
          dp[i][j] = dp[i][j - 1];
          par[i][j].fr = i;
          par[i][j].sc = j - 1;
        }
      }
    }
  }
}
//=======================================================================================================================
int main() {
  str s, t;
  cin >> s >> t;
  lcs(s, t);
  int n = s.sz;
  int m = t.sz;
  while (n > 0 and m > 0) {
    if (par[n][m].fr == n - 1 and par[n][m].sc == m - 1)
      v.pb(s[n - 1]);
    pair<int, int> tmp = par[n][m];
    n = tmp.fr;
    m = tmp.sc;
  }
  for (int i = v.sz - 1; i >= 0; i--) {
    cout << v[i];
  }
}
//========================================================================================================================
// 4
// 1 4 2 3
// 4 1 2 3
// 1 2 4 3