Problem p03165 (C++)

Buggy submission (status: Runtime Error):
/*
lakshaygpt28
Lakshay Gupta
*/

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;

using ll = long long;
using db = double;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using vll = vector<ll>;

template <typename T>
using OrderedSet =
    tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
template <typename T>
using MinPriorityQueue = priority_queue<T, vector<T>, greater<T>>;

#ifndef ONLINE_JUDGE
#define debug(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1> void __f(const char *name, Arg1 &&arg1) {
  cerr << name << " : " << arg1 << std ::endl;
}
template <typename Arg1, typename... Args>
void __f(const char *names, Arg1 &&arg1, Args &&...args) {
  const char *comma = strchr(names + 1, ',');
  cerr.write(names, comma - names) << " : " << arg1 << " | ";
  __f(comma + 1, args...);
}
#else
#define debug(...)
#endif

#define fast_io()                                                              \
  ios_base ::sync_with_stdio(0);                                               \
  cin.tie(0);                                                                  \
  cout.tie(0)
#define eb emplace_back
#define mp make_pair
#define pb push_back
#define mt make_tuple
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()

const db PI = acos(-1);
const ll LINF = LLONG_MAX;
const int INF = INT_MAX, MOD = 1e9 + 7, N = 1e3 + 10;

ll dp[N][N];

int main() {
  fast_io();
  string s, t;
  cin >> s >> t;
  ll len1 = s.length(), len2 = t.length();
  s = "." + s;
  t = "." + t;
  for (ll i = 1; i <= len1; i++) {
    for (ll j = 1; j <= len2; j++) {
      dp[i][j] =
          max({dp[i - 1][j], dp[i][j - 1], dp[i - 1][j - 1] + (s[i] == t[j])});
    }
  }
  string ans;
  ll i = len1, j = len2;
  while (i > 0 and j > 0) {
    if (s[i] == t[j]) {
      ans = s[i] + ans;
      i--, j--;
    } else if (dp[i - 1][j] >= dp[i][j - 1]) {
      i--;
    } else {
      j--;
    }
  }
  cout << ans << "\n";
  // cout << dp[len1][len2] << "\n";
  return 0;
}


Fixed submission:
/*
lakshaygpt28
Lakshay Gupta
*/

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;

using ll = long long;
using db = double;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using vll = vector<ll>;

template <typename T>
using OrderedSet =
    tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
template <typename T>
using MinPriorityQueue = priority_queue<T, vector<T>, greater<T>>;

#ifndef ONLINE_JUDGE
#define debug(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1> void __f(const char *name, Arg1 &&arg1) {
  cerr << name << " : " << arg1 << std ::endl;
}
template <typename Arg1, typename... Args>
void __f(const char *names, Arg1 &&arg1, Args &&...args) {
  const char *comma = strchr(names + 1, ',');
  cerr.write(names, comma - names) << " : " << arg1 << " | ";
  __f(comma + 1, args...);
}
#else
#define debug(...)
#endif

#define fast_io()                                                              \
  ios_base ::sync_with_stdio(0);                                               \
  cin.tie(0);                                                                  \
  cout.tie(0)
#define eb emplace_back
#define mp make_pair
#define pb push_back
#define mt make_tuple
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()

const db PI = acos(-1);
const ll LINF = LLONG_MAX;
const int INF = INT_MAX, MOD = 1e9 + 7, N = 3e3 + 10;

ll dp[N][N];

int main() {
  fast_io();
  string s, t;
  cin >> s >> t;
  ll len1 = s.length(), len2 = t.length();
  s = "." + s;
  t = "." + t;
  for (ll i = 1; i <= len1; i++) {
    for (ll j = 1; j <= len2; j++) {
      dp[i][j] =
          max({dp[i - 1][j], dp[i][j - 1], dp[i - 1][j - 1] + (s[i] == t[j])});
    }
  }
  string ans;
  ll i = len1, j = len2;
  while (i > 0 and j > 0) {
    if (s[i] == t[j]) {
      ans = s[i] + ans;
      i--, j--;
    } else if (dp[i - 1][j] >= dp[i][j - 1]) {
      i--;
    } else {
      j--;
    }
  }
  cout << ans << "\n";
  // cout << dp[len1][len2] << "\n";
  return 0;
}