Problem p03165 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bits/stdc++.h>
#include <cmath>
#include <ctype.h>
#include <deque> //<< fixed << setprecision()
#include <iomanip>
#include <iostream>
#include <map>
#include <queue> //priority_queue
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
#include <vector>
typedef long long ll;
using namespace std;
const double pi = (2 * acos(0));

struct point {
  long double x, y;
};
double area(int x1, int y1, int x2, int y2, int x3, int y3) {
  double a =
      (x2 * y3) - (x3 * y2) - (x1 * y3) + (x3 * y1) + (x1 * y2) - (x2 * y1);
  a /= 2;
  return fabs(a);
}

long double anyarea(point p[], int n) {
  long double sum1 = p[n - 1].x * p[0].y, sum2 = p[n - 1].y * p[0].x;
  for (int i = 0; i < n - 1; i++) {
    sum1 += p[i].x * p[i + 1].y;
    sum2 += p[i].y * p[i + 1].x;
  }
  return fabs((sum1 - sum2) * 0.5);
}

ll fastpow(ll base, ll p, ll m) {
  if (p == 0)
    return 1;
  ll ans = fastpow(base, p / 2, m);
  ans = ((ans % m) * (ans % m)) % m;
  if (p % 2)
    ans = ((base % m) * ans) % m;
  return ans;
}
ll GCD(ll M, ll m) {
  if (M % m == 0)
    return m;
  M %= m;
  ll x1 = M, x2 = m;
  m = min(x1, x2);
  M = max(x1, x2);
  return GCD(M, m);
}
ll lcm(ll M, ll m) { return (M * m) / GCD(max(M, m), min(M, m)); }

int n, n2, k;
string s, s2;
int dp[2005][2005];
int solve(int i, int j) {
  if (dp[i][j] != -1) {
    return dp[i][j];
  }
  if (i >= n || j >= n2)
    return 0;
  if (s[i] == s2[j]) {
    return dp[i][j] = 1 + solve(i + 1, j + 1);
  }
  int m = 0, im = -1;

  return dp[i][j] = max(solve(i, j + 1), solve(i + 1, j));
}
void fun(int i, int j) {
  if (i >= n || j >= n2)
    return;
  if (s[i] == s2[j]) {
    cout << s[i];
    fun(i + 1, j + 1);
  }

  else if (dp[i][j] == (solve(i, j + 1))) {
    fun(i, j + 1);
  } else if (dp[i][j] == solve(i + 1, j)) {
    fun(i + 1, j);
  }
}

int main() {
  cin >> s >> s2;
  memset(dp, -1, sizeof dp);
  n = s.size();
  n2 = s2.size();
  int x = solve(0, 0);
  fun(0, 0);
}


Fixed submission:
#include <algorithm>
#include <bits/stdc++.h>
#include <cmath>
#include <ctype.h>
#include <deque> //<< fixed << setprecision()
#include <iomanip>
#include <iostream>
#include <map>
#include <queue> //priority_queue
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
#include <vector>
typedef long long ll;
using namespace std;
const double pi = (2 * acos(0));

struct point {
  long double x, y;
};
double area(int x1, int y1, int x2, int y2, int x3, int y3) {
  double a =
      (x2 * y3) - (x3 * y2) - (x1 * y3) + (x3 * y1) + (x1 * y2) - (x2 * y1);
  a /= 2;
  return fabs(a);
}

long double anyarea(point p[], int n) {
  long double sum1 = p[n - 1].x * p[0].y, sum2 = p[n - 1].y * p[0].x;
  for (int i = 0; i < n - 1; i++) {
    sum1 += p[i].x * p[i + 1].y;
    sum2 += p[i].y * p[i + 1].x;
  }
  return fabs((sum1 - sum2) * 0.5);
}

ll fastpow(ll base, ll p, ll m) {
  if (p == 0)
    return 1;
  ll ans = fastpow(base, p / 2, m);
  ans = ((ans % m) * (ans % m)) % m;
  if (p % 2)
    ans = ((base % m) * ans) % m;
  return ans;
}
ll GCD(ll M, ll m) {
  if (M % m == 0)
    return m;
  M %= m;
  ll x1 = M, x2 = m;
  m = min(x1, x2);
  M = max(x1, x2);
  return GCD(M, m);
}
ll lcm(ll M, ll m) { return (M * m) / GCD(max(M, m), min(M, m)); }

int n, n2, k;
string s, s2;
int dp[3005][3005];
int solve(int i, int j) {
  if (dp[i][j] != -1) {
    return dp[i][j];
  }
  if (i >= n || j >= n2)
    return 0;
  if (s[i] == s2[j]) {
    return dp[i][j] = 1 + solve(i + 1, j + 1);
  }
  int m = 0, im = -1;

  return dp[i][j] = max(solve(i, j + 1), solve(i + 1, j));
}
void fun(int i, int j) {
  if (i >= n || j >= n2)
    return;
  if (s[i] == s2[j]) {
    cout << s[i];
    fun(i + 1, j + 1);
  }

  else if (dp[i][j] == (solve(i, j + 1))) {
    fun(i, j + 1);
  } else if (dp[i][j] == solve(i + 1, j)) {
    fun(i + 1, j);
  }
}

int main() {
  cin >> s >> s2;
  memset(dp, -1, sizeof dp);
  n = s.size();
  n2 = s2.size();
  int x = solve(0, 0);
  fun(0, 0);
}