Problem p03165 (C++)

Buggy submission (status: Runtime Error):
/********************************
*** ******** ********** *********
*** * ****** ********* * ********
*** ** ***** ******** *** *******
*** *** **** ******* ***** ******
*** **** *** ****** ******* *****
*** ***** ** ***** +++++++++ ****
*** ****** * **** *********** ***
*** *******  *** ************* **
*** ******** ** *************** *
**********_**********************
* |   /  / \  \     /    /\     *
* |  /  |   |  \   /    /  \    *
* | /   |   |   \ /    /    \   *
* |+\   |   |    |    /______\  *
* |  \  |   |    |   |        | *
* |   \  \_/     |   |        | *
********************************/
#include <bits/stdc++.h>
#define lli long long int
#define MOD 1000000007
#define KING 1000000002
#define queen 1000002
#define fi first
#define se second
#define se second
#define lsb(i) (i & -i)
#define Pi 3.141592653589793238
#define nl cout << '\n'
#define TEST cout << "TEST", nl
#define all(a) a.begin(), a.end()
#define remax(a, b) (a = ((a < b) ? b : a))
#define remin(a, b) (a = ((a < b) ? a : b))
#define pr pair<lli, lli>
#define pb push_back
#define eb emplace_back
#define precise(a) cout << fixed << setprecision(a)
#define mp make_pair
#define vi vector<lli>
#define vpi vector<pr>
#define P pair<lli, pr>
#define fastIO                                                                 \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL)
#define NA(i, s, n) for (lli i = s; i < n; i++)
using namespace std;

lli power(lli x, lli y, lli p) {
  lli res = 1;
  x = x % p;
  while (y > 0) {
    if (y & 1)
      res = (res * x) % p;
    y = y >> 1;
    x = (x * x) % p;
  }
  return res;
}

const lli N = 3e3 + 2;
lli dp[N][N], n, m;
string s, t;

void solve() {
  cin >> s >> t;
  n = s.size(), m = t.size();
  memset(dp, 0, sizeof dp);
  NA(i, 1, n + 1) {
    NA(j, 1, m + 1) {
      dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]);
      if (s[i - 1] == t[j - 1]) {
        remax(dp[i][j], dp[i - 1][j - 1] + 1);
      }
    }
  }
  string res = "";
  if (!dp[n][m]) {
    cout << res;
    return;
  }
  lli i = n, j = m;
  while (i > 0 || j > 0) {
    if (dp[i][j] == dp[i - 1][j - 1] + 1 && s[i - 1] == t[j - 1])
      res += s[i - 1], --i, --j;
    else if (dp[i][j - 1] == dp[i][j])
      --j;
    else
      --i;
  }
  reverse(all(res));
  cout << res;
  return;
}
int main() {
  fastIO;
  lli t = 1;
  // cin>>t;
  NA(i, 0, t) { solve(); }
  return 0;
}


Fixed submission:
/********************************
*** ******** ********** *********
*** * ****** ********* * ********
*** ** ***** ******** *** *******
*** *** **** ******* ***** ******
*** **** *** ****** ******* *****
*** ***** ** ***** +++++++++ ****
*** ****** * **** *********** ***
*** *******  *** ************* **
*** ******** ** *************** *
**********_**********************
* |   /  / \  \     /    /\     *
* |  /  |   |  \   /    /  \    *
* | /   |   |   \ /    /    \   *
* |+\   |   |    |    /______\  *
* |  \  |   |    |   |        | *
* |   \  \_/     |   |        | *
********************************/
#include <bits/stdc++.h>
#define lli long long int
#define MOD 1000000007
#define KING 1000000002
#define queen 1000002
#define fi first
#define se second
#define se second
#define lsb(i) (i & -i)
#define Pi 3.141592653589793238
#define nl cout << '\n'
#define TEST cout << "TEST", nl
#define all(a) a.begin(), a.end()
#define remax(a, b) (a = ((a < b) ? b : a))
#define remin(a, b) (a = ((a < b) ? a : b))
#define pr pair<lli, lli>
#define pb push_back
#define eb emplace_back
#define precise(a) cout << fixed << setprecision(a)
#define mp make_pair
#define vi vector<lli>
#define vpi vector<pr>
#define P pair<lli, pr>
#define fastIO                                                                 \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL)
#define NA(i, s, n) for (lli i = s; i < n; i++)
using namespace std;

lli power(lli x, lli y, lli p) {
  lli res = 1;
  x = x % p;
  while (y > 0) {
    if (y & 1)
      res = (res * x) % p;
    y = y >> 1;
    x = (x * x) % p;
  }
  return res;
}

const lli N = 3e3 + 2;
lli dp[N][N], n, m;
string s, t;

void solve() {
  cin >> s >> t;
  n = s.size(), m = t.size();
  memset(dp, 0, sizeof dp);
  NA(i, 1, n + 1) {
    NA(j, 1, m + 1) {
      dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]);
      if (s[i - 1] == t[j - 1]) {
        remax(dp[i][j], dp[i - 1][j - 1] + 1);
      }
    }
  }
  string res = "";
  if (!dp[n][m]) {
    cout << res;
    return;
  }
  lli i = n, j = m;
  while (i > 0 && j > 0) {
    if (dp[i][j] == dp[i - 1][j - 1] + 1 && s[i - 1] == t[j - 1])
      res += s[i - 1], --i, --j;
    else if (dp[i][j - 1] == dp[i][j])
      --j;
    else
      --i;
  }
  reverse(all(res));
  cout << res;
  return;
}
int main() {
  fastIO;
  lli t = 1;
  // cin>>t;
  NA(i, 0, t) { solve(); }
  return 0;
}