Problem p03165 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#include <string>

#define ll long long
#define ld long double
// #define endl '\n'
#define With_the_name_of_Allah                                                 \
  ios::sync_with_stdio(0);                                                     \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0), cout.tie(0);
#define pi double(acos(-1))

using namespace std;

long long mod;
long long Fast_pow(long long a, long long b) {
  if (b == 0)
    return 1;

  long long z = Fast_pow(a, b / 2);

  if (b % 2 == 0)
    return ((z % mod) * (z % mod)) % mod;
  else
    return (((z * z) % mod) * a) % mod;
}

long long gcd(long long a, long long b) {
  if (a == 0)
    return b;
  else
    return gcd(b % a, a);
}

bool prime[1000001];

void sieve() {
  memset(prime, true, sizeof prime);
  prime[0] = prime[1] = 0;

  for (int i = 2; i * i <= 1000000; i++) {
    if (prime[i] == true)
      for (int j = i * i; j <= 100000; j += i)
        prime[j] = false;
  }
}

ll lcm(ll a, ll b) { return (a * b) / __gcd(a, b); }
int res;
int ans[1000][1000];
string a, b, c;
int fn(int i, int j) {
  if (i >= a.size() || j >= b.size())
    return 0;
  if (ans[i][j] != -1)
    return ans[i][j];
  int res = 0;

  if (a[i] == b[j])
    res = 1 + fn(i + 1, j + 1), c += a[i];

  else {
    res = max(res, fn(i + 1, j));
    res = max(res, fn(i, j + 1));
  }
  ans[i][j] = res;
  return res;
}

void pr(int i, int j) {
  if (i >= a.size() || j >= b.size())
    return;

  else if (a[i] == b[j])
    cout << a[i], pr(i + 1, j + 1);

  else {
    if (fn(i + 1, j) == ans[i][j])
      pr(i + 1, j);
    else
      pr(i, j + 1);
  }
}

string ss, bb;
int f(int i, int j) {
  if (i == a.size())
    return b.size() - j;
  else if (j == b.size())
    return a.size() - i;

  if (ans[i][j] != -1)
    return ans[i][j];
  res = 1e9;
  if (a[i] == b[j])
    res = f(i + 1, j + 1);
  else {
    res = min(f(i + 1, j) + 1, f(i, j + 1) + 1);
    res = min(res, f(i + 1, j + 1) + 1);
  }
  ans[i][j] = res;
  return res;
}

int main() {
  //------lcs------//

  cin >> a >> b;
  memset(ans, -1, sizeof ans);
  /*fn(0,0);
   pr(0,0);
   */

  fn(0, 0);
  pr(0, 0);
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#include <string>

#define ll long long
#define ld long double
// #define endl '\n'
#define With_the_name_of_Allah                                                 \
  ios::sync_with_stdio(0);                                                     \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0), cout.tie(0);
#define pi double(acos(-1))

using namespace std;

long long mod;
long long Fast_pow(long long a, long long b) {
  if (b == 0)
    return 1;

  long long z = Fast_pow(a, b / 2);

  if (b % 2 == 0)
    return ((z % mod) * (z % mod)) % mod;
  else
    return (((z * z) % mod) * a) % mod;
}

long long gcd(long long a, long long b) {
  if (a == 0)
    return b;
  else
    return gcd(b % a, a);
}

bool prime[1000001];

void sieve() {
  memset(prime, true, sizeof prime);
  prime[0] = prime[1] = 0;

  for (int i = 2; i * i <= 1000000; i++) {
    if (prime[i] == true)
      for (int j = i * i; j <= 100000; j += i)
        prime[j] = false;
  }
}

ll lcm(ll a, ll b) { return (a * b) / __gcd(a, b); }
int res;
int ans[3 * 1001][3 * 1001];
string a, b, c;
int fn(int i, int j) {
  if (i >= a.size() || j >= b.size())
    return 0;
  if (ans[i][j] != -1)
    return ans[i][j];
  int res = 0;

  if (a[i] == b[j])
    res = 1 + fn(i + 1, j + 1), c += a[i];

  else {
    res = max(res, fn(i + 1, j));
    res = max(res, fn(i, j + 1));
  }
  ans[i][j] = res;
  return res;
}

void pr(int i, int j) {
  if (i >= a.size() || j >= b.size())
    return;

  else if (a[i] == b[j])
    cout << a[i], pr(i + 1, j + 1);

  else {
    if (fn(i + 1, j) == ans[i][j])
      pr(i + 1, j);
    else
      pr(i, j + 1);
  }
}

string ss, bb;
int f(int i, int j) {
  if (i == a.size())
    return b.size() - j;
  else if (j == b.size())
    return a.size() - i;

  if (ans[i][j] != -1)
    return ans[i][j];
  res = 1e9;
  if (a[i] == b[j])
    res = f(i + 1, j + 1);
  else {
    res = min(f(i + 1, j) + 1, f(i, j + 1) + 1);
    res = min(res, f(i + 1, j + 1) + 1);
  }
  ans[i][j] = res;
  return res;
}

int main() {
  //------lcs------//

  cin >> a >> b;
  memset(ans, -1, sizeof ans);
  /*fn(0,0);
   pr(0,0);
   */

  fn(0, 0);
  pr(0, 0);
  return 0;
}