Problem p03165 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#include <unordered_map>
#define inputarr(a, n)                                                         \
  for (ll i = 0; i < n; i++)                                                   \
    cin >> a[i];
#define prllarr(a, n)                                                          \
  for (ll i = 0; i < n; i++)                                                   \
    cout << a[i] << " ";
#define pb push_back
#define ll long long
#define mod 1000000007
#define foi                                                                    \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);                                                               \
  cout.tie(0);
#define in(n) scanf("%lld", &n);
#define in2(x, y) scanf("%lld %lld", &(x), &(y));
#define in3(x, y, z) scanf("%lld %lld %lld", &(x), &(y), &(z));
#define out(n) printf("%lld\n", n);
#define out2(x, y) printf("%lld %lld\n", x, y);
#define test(t)                                                                \
  ll t;                                                                        \
  in(t);                                                                       \
  while (t--)
#define set(arr, n, s)                                                         \
  for (ll i = 0; i < n; i++) {                                                 \
    arr[i] = s;                                                                \
  }

/*error-----
convert every int to long long eg-1LL
create array with proper analysis of problem constrain
check mod also
*/
ll power(ll x, ll y, ll p) {
  ll res = 1;
  x = x % p;
  while (y > 0) {
    if (y & 1) {
      res = (res * x) % p;
    }
    y = y >> 1;
    x = (x * x) % p;
  }
  return res;
}
ll modInverse(ll a, ll p) {
  return power(a, p - 2, p);
} // used with feemat little

ll gcd(ll x, ll y) {
  if (x == 0 || y == 0) {
    return max(y, x);
  }
  return gcd(y % x, x);
}
ll gcdExtended(ll a, ll b, ll &x, ll &y) {
  if (a == 0) {
    x = 0;
    y = 1;
    return b;
  }
  ll x1, y1;
  ll gcd = gcdExtended(b % a, a, x1, y1);
  x = y1 - (b / a) * x1;
  y = x1;
  return gcd;
} // o(log(b))

// ll ncr(ll n,ll r,ll p){if(r==0){return 1;}return ((((fact[n]* inv[r]) % p) *
// inv[n-r]) % p);}
ll dp[3005][3005];
int main() {
  string s, t;
  cin >> s >> t;
  for (ll i = 1; i <= s.size(); i++) {
    for (ll j = 1; j <= t.size(); j++) {
      if (s[i - 1] == t[j - 1]) {
        dp[i][j] = 1 + dp[i - 1][j - 1];
      } else {
        dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]);
      }
    }
  }
  ll i = s.size();
  ll j = t.size();
  string ans = "";
  while (i != 0 || j != 0) {
    if (s[i - 1] == t[j - 1]) {
      ans += s[i - 1];
      i--;
      j--;
    } else {
      if (dp[i][j] == dp[i - 1][j]) {
        i--;
      } else

      {
        j--;
      }
    }
  }
  for (ll i = ans.size() - 1; i >= 0; i--) {
    cout << ans[i];
  }
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#include <unordered_map>
#define inputarr(a, n)                                                         \
  for (ll i = 0; i < n; i++)                                                   \
    cin >> a[i];
#define prllarr(a, n)                                                          \
  for (ll i = 0; i < n; i++)                                                   \
    cout << a[i] << " ";
#define pb push_back
#define ll long long
#define mod 1000000007
#define foi                                                                    \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);                                                               \
  cout.tie(0);
#define in(n) scanf("%lld", &n);
#define in2(x, y) scanf("%lld %lld", &(x), &(y));
#define in3(x, y, z) scanf("%lld %lld %lld", &(x), &(y), &(z));
#define out(n) printf("%lld\n", n);
#define out2(x, y) printf("%lld %lld\n", x, y);
#define test(t)                                                                \
  ll t;                                                                        \
  in(t);                                                                       \
  while (t--)
#define set(arr, n, s)                                                         \
  for (ll i = 0; i < n; i++) {                                                 \
    arr[i] = s;                                                                \
  }

/*error-----
convert every int to long long eg-1LL
create array with proper analysis of problem constrain
check mod also
*/
ll power(ll x, ll y, ll p) {
  ll res = 1;
  x = x % p;
  while (y > 0) {
    if (y & 1) {
      res = (res * x) % p;
    }
    y = y >> 1;
    x = (x * x) % p;
  }
  return res;
}
ll modInverse(ll a, ll p) {
  return power(a, p - 2, p);
} // used with feemat little

ll gcd(ll x, ll y) {
  if (x == 0 || y == 0) {
    return max(y, x);
  }
  return gcd(y % x, x);
}
ll gcdExtended(ll a, ll b, ll &x, ll &y) {
  if (a == 0) {
    x = 0;
    y = 1;
    return b;
  }
  ll x1, y1;
  ll gcd = gcdExtended(b % a, a, x1, y1);
  x = y1 - (b / a) * x1;
  y = x1;
  return gcd;
} // o(log(b))

// ll ncr(ll n,ll r,ll p){if(r==0){return 1;}return ((((fact[n]* inv[r]) % p) *
// inv[n-r]) % p);}
ll dp[3005][3005];
int main() {
  string s, t;
  cin >> s >> t;
  for (ll i = 1; i <= s.size(); i++) {
    for (ll j = 1; j <= t.size(); j++) {
      if (s[i - 1] == t[j - 1]) {
        dp[i][j] = 1 + dp[i - 1][j - 1];
      } else {
        dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]);
      }
    }
  }
  ll i = s.size();
  ll j = t.size();
  string ans = "";
  while (i != 0 && j != 0) {
    if (s[i - 1] == t[j - 1]) {
      ans += s[i - 1];
      i--;
      j--;
    } else {
      if (dp[i][j] == dp[i - 1][j]) {
        i--;
      } else

      {
        j--;
      }
    }
  }
  for (ll i = ans.size() - 1; i >= 0; i--) {
    cout << ans[i];
  }
}