Problem p03165 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define FASTIO                                                                 \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);                                                               \
  cout.tie(NULL);
#define fr(a, b) for (long long i = a; i < b; i++)
#define vi vector<ll>
const ll MAX = 1e3;
const ll mod = 1e9 + 7;
const ll INF = 1e18 + 12;
set<pair<ll, ll>> ss;
ll dp[MAX][MAX];

// SAMEER KUMAR SINGH//
// ================================================================================================================================//
// ================================================================================================================================//
// ================================================================================================================================//

int fun(string s1, string s2, ll n1, ll n2) {

  if (n1 < 0 || n2 < 0)
    return 0;
  if (dp[n1][n2] != 0)
    return dp[n1][n2];
  if (s1[n1] == s2[n2]) {
    dp[n1][n2] = 1 + fun(s1, s2, n1 - 1, n2 - 1);
    return dp[n1][n2];
  } else {
    ll k1 = fun(s1, s2, n1 - 1, n2);
    ll k2 = fun(s1, s2, n1, n2 - 1);
    dp[n1][n2] = max(k1, k2);
    return dp[n1][n2];
  }
}

int main() {
  FASTIO;
  ll tt = 1;
  // cin>>tt;

  while (tt--) {
    // ll n1,n2;cin>>n1>>n2;
    string s1, s2;
    cin >> s1 >> s2;

    for (ll i = 1; i <= s1.size(); i++) {
      for (ll j = 1; j <= s2.size(); j++) {
        if (s1[i - 1] == s2[j - 1])
          dp[i][j] = dp[i - 1][j - 1] + 1;
        else {
          dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]);
        }
      }
    }
    vector<char> vv;
    ll i = s1.size();
    ll j = s2.size();

    while (i > 0 && j > 0) {
      if (s1[i - 1] == s2[j - 1]) {
        vv.push_back(s1[i - 1]);
        i--;
        j--;
      } else if (dp[i - 1][j] >= dp[i][j - 1]) {
        i--;
      } else {
        j--;
      }
    }
    reverse(vv.begin(), vv.end());
    for (auto x : vv)
      cout << x;
  }

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define FASTIO                                                                 \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);                                                               \
  cout.tie(NULL);
#define fr(a, b) for (long long i = a; i < b; i++)
#define vi vector<ll>
const ll MAX = 1e4;
const ll mod = 1e9 + 7;
const ll INF = 1e18 + 12;
set<pair<ll, ll>> ss;
ll dp[MAX][MAX];

// SAMEER KUMAR SINGH//
// ================================================================================================================================//
// ================================================================================================================================//
// ================================================================================================================================//

int fun(string s1, string s2, ll n1, ll n2) {

  if (n1 < 0 || n2 < 0)
    return 0;
  if (dp[n1][n2] != 0)
    return dp[n1][n2];
  if (s1[n1] == s2[n2]) {
    dp[n1][n2] = 1 + fun(s1, s2, n1 - 1, n2 - 1);
    return dp[n1][n2];
  } else {
    ll k1 = fun(s1, s2, n1 - 1, n2);
    ll k2 = fun(s1, s2, n1, n2 - 1);
    dp[n1][n2] = max(k1, k2);
    return dp[n1][n2];
  }
}

int main() {
  FASTIO;
  ll tt = 1;
  // cin>>tt;

  while (tt--) {
    // ll n1,n2;cin>>n1>>n2;
    string s1, s2;
    cin >> s1 >> s2;

    for (ll i = 1; i <= s1.size(); i++) {
      for (ll j = 1; j <= s2.size(); j++) {
        if (s1[i - 1] == s2[j - 1])
          dp[i][j] = dp[i - 1][j - 1] + 1;
        else {
          dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]);
        }
      }
    }
    vector<char> vv;
    ll i = s1.size();
    ll j = s2.size();

    while (i > 0 && j > 0) {
      if (s1[i - 1] == s2[j - 1]) {
        vv.push_back(s1[i - 1]);
        i--;
        j--;
      } else if (dp[i - 1][j] >= dp[i][j - 1]) {
        i--;
      } else {
        j--;
      }
    }
    reverse(vv.begin(), vv.end());
    for (auto x : vv)
      cout << x;
  }

  return 0;
}