Problem p03165 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define ull unsigned long long int
#define Endl endl
#define mod 998244353
#define mkp make_pair
#define pb push_back
#define cin(n) scanf("%d", &n)
#define cout(n) printf("%d ", n)
#define sf(n) scanf("%d", &n)
#define pf(n) printf("%d ", n)
#define bitget(n) __builtin_popcountll(n)

ll dx[] = {1, 1, 0, -1, -1, -1, 0, 1};
ll dy[] = {0, -1, -1, -1, 0, 1, 1, 1};
ll dxx[] = {1, -1, 0, 0};
ll dyy[] = {0, 0, -1, 1};
ll modexpo(ll x, ll y) {
  if (y == 0)
    return 1;
  if (y % 2) {
    ll viky = modexpo(x, y / 2);
    return (((x * viky) % mod) * viky) % mod;
  } else {
    ll viky = modexpo(x, y / 2);
    return (viky * viky) % mod;
  }
}
ll intpoint(ll x1, ll y1, ll x2, ll y2) {
  return (__gcd(abs(x1 - x2), abs(y1 - y2)) + 1);
}
bool isvalid(ll x, ll y, ll n, ll m) {
  return (x >= 0 && x < n && y >= 0 && y < m);
}
// mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
// int pp = uniform_int_distribution<int>(0, kk-1)(rng);
// ---------------------///////////////---------------------------------
// ---------------------///////////////----------------------------------
// -------------/////////////////---------------------------------///////
ll dp[311][311];
int main() {
  // #ifndef ONLINE_JUDGE
  //     freopen("input.txt", "r", stdin);
  //     freopen("output.txt", "w", stdout);
  // #endif
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  ll t, i, j, l, r, q, k, kk, m, n, p;

  string s, sp;
  cin >> s >> sp;

  n = s.size();
  m = sp.size();

  // ll dp[n+11][m+11];

  for (i = 1; i <= n; i++) {
    for (j = 1; j <= m; j++) {
      if (s[i - 1] == sp[j - 1]) {
        dp[i][j] = max(dp[i - 1][j - 1] + 1, dp[i - 1][j]);
      } else {
        dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]);
      }
    }
  }

  string ans = "";
  i = n;
  j = m;
  while (j != 0 && i != 0) {
    if (dp[i][j] == dp[i - 1][j]) {
      i--;
    } else if (dp[i][j] == dp[i][j - 1]) {
      j--;
    } else {
      i--;
      j--;
      ans.push_back(s[i]);
    }
  }

  reverse(ans.begin(), ans.end());

  cout << ans;
  // for(i=n;i>=1;i--){
  // 	for(j=m;j>=1;j--){
  // 		if(dp[i][j] == dp[i-1][j]){

  // 		}
  // 	}
  // }

  // cout<<dp[n][m];

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define ull unsigned long long int
#define Endl endl
#define mod 998244353
#define mkp make_pair
#define pb push_back
#define cin(n) scanf("%d", &n)
#define cout(n) printf("%d ", n)
#define sf(n) scanf("%d", &n)
#define pf(n) printf("%d ", n)
#define bitget(n) __builtin_popcountll(n)

ll dx[] = {1, 1, 0, -1, -1, -1, 0, 1};
ll dy[] = {0, -1, -1, -1, 0, 1, 1, 1};
ll dxx[] = {1, -1, 0, 0};
ll dyy[] = {0, 0, -1, 1};
ll modexpo(ll x, ll y) {
  if (y == 0)
    return 1;
  if (y % 2) {
    ll viky = modexpo(x, y / 2);
    return (((x * viky) % mod) * viky) % mod;
  } else {
    ll viky = modexpo(x, y / 2);
    return (viky * viky) % mod;
  }
}
ll intpoint(ll x1, ll y1, ll x2, ll y2) {
  return (__gcd(abs(x1 - x2), abs(y1 - y2)) + 1);
}
bool isvalid(ll x, ll y, ll n, ll m) {
  return (x >= 0 && x < n && y >= 0 && y < m);
}
// mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
// int pp = uniform_int_distribution<int>(0, kk-1)(rng);
// ---------------------///////////////---------------------------------
// ---------------------///////////////----------------------------------
// -------------/////////////////---------------------------------///////
ll dp[3011][3011];
int main() {
  // #ifndef ONLINE_JUDGE
  //     freopen("input.txt", "r", stdin);
  //     freopen("output.txt", "w", stdout);
  // #endif
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  ll t, i, j, l, r, q, k, kk, m, n, p;

  string s, sp;
  cin >> s >> sp;

  n = s.size();
  m = sp.size();

  // ll dp[n+11][m+11];

  for (i = 1; i <= n; i++) {
    for (j = 1; j <= m; j++) {
      if (s[i - 1] == sp[j - 1]) {
        dp[i][j] = max(dp[i - 1][j - 1] + 1, dp[i - 1][j]);
      } else {
        dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]);
      }
    }
  }

  string ans = "";
  i = n;
  j = m;
  while (j != 0 && i != 0) {
    if (dp[i][j] == dp[i - 1][j]) {
      i--;
    } else if (dp[i][j] == dp[i][j - 1]) {
      j--;
    } else {
      i--;
      j--;
      ans.push_back(s[i]);
    }
  }

  reverse(ans.begin(), ans.end());

  cout << ans;
  // for(i=n;i>=1;i--){
  // 	for(j=m;j>=1;j--){
  // 		if(dp[i][j] == dp[i-1][j]){

  // 		}
  // 	}
  // }

  // cout<<dp[n][m];

  return 0;
}