Problem p03165 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp> //PBDS
#include <ext/pb_ds/tree_policy.hpp>     //PBDS
#define int long long int
#define vi vector<int>
#define vii vector<pair<int, int>>
#define pi pair<int, int>
#define fast                                                                   \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);                                                               \
  cout.tie(NULL);
#define pb push_back
#define pf push_front
#define fi first
#define se second
#define mi map<int, int>
#define PI 3.141592653589
#define all(a) (a).begin(), (a).end()
#define rall(a) (a).rbegin(), (a).rend()
#define f(i, x, n) for (int i = x; i < n; i++)
#define rf(i, x, n) for (int i = n - 1; i >= x; i--)
#define check(x) cerr << #x << " : " << x << endl;
#define ordered_set                                                            \
  tree<int, null_type, less_equal<int>, rb_tree_tag,                           \
       tree_order_statistics_node_update>
#define fbo find_by_order // return iterator PBDS
#define ook order_of_key  // return number :)) PBDS
#define baar exit(0)
#define INF 1000000000000000000
using namespace __gnu_pbds;
using namespace std;
int powmod(int base, int exponent, int mod) {
  int ans = 1;
  while (exponent) {
    if (exponent & 1)
      ans = (ans * base) % mod;
    base = (base * base) % mod;
    exponent /= 2;
  }
  return ans;
}
string ans = "";
int LCS[3000][3000];
void findLCS(string text1, string text2, int n, int m) {
  if (n == 0 or m == 0)
    return;
  if (text1[n - 1] == text2[m - 1]) {
    ans += (text1[n - 1]);
    findLCS(text1, text2, n - 1, m - 1);
  } else {
    if (LCS[n - 1][m] >= LCS[n][m - 1]) {
      findLCS(text1, text2, n - 1, m);
    } else {
      findLCS(text1, text2, n, m - 1);
    }
  }
}
signed main() {
  fast string text1, text2;
  cin >> text1 >> text2;
  int n = text1.length();
  int m = text2.length();
  memset(LCS, 0, sizeof(LCS));
  for (int i = 0; i <= n; i++) {
    for (int j = 0; j <= m; j++) {
      if (i == 0 or j == 0)
        LCS[i][j] = 0;
      else if (text1[i - 1] == text2[j - 1])
        LCS[i][j] = LCS[i - 1][j - 1] + 1;
      else
        LCS[i][j] = max(LCS[i - 1][j], LCS[i][j - 1]);
    }
  }
  findLCS(text1, text2, n, m);
  reverse(all(ans));
  cout << ans;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp> //PBDS
#include <ext/pb_ds/tree_policy.hpp>     //PBDS
#define int long long int
#define vi vector<int>
#define vii vector<pair<int, int>>
#define pi pair<int, int>
#define fast                                                                   \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);                                                               \
  cout.tie(NULL);
#define pb push_back
#define pf push_front
#define fi first
#define se second
#define mi map<int, int>
#define PI 3.141592653589
#define all(a) (a).begin(), (a).end()
#define rall(a) (a).rbegin(), (a).rend()
#define f(i, x, n) for (int i = x; i < n; i++)
#define rf(i, x, n) for (int i = n - 1; i >= x; i--)
#define check(x) cerr << #x << " : " << x << endl;
#define ordered_set                                                            \
  tree<int, null_type, less_equal<int>, rb_tree_tag,                           \
       tree_order_statistics_node_update>
#define fbo find_by_order // return iterator PBDS
#define ook order_of_key  // return number :)) PBDS
#define baar exit(0)
#define INF 1000000000000000000
using namespace __gnu_pbds;
using namespace std;
int powmod(int base, int exponent, int mod) {
  int ans = 1;
  while (exponent) {
    if (exponent & 1)
      ans = (ans * base) % mod;
    base = (base * base) % mod;
    exponent /= 2;
  }
  return ans;
}
string ans = "";
int LCS[3005][3005];
void findLCS(string text1, string text2, int n, int m) {
  if (n == 0 or m == 0)
    return;
  if (text1[n - 1] == text2[m - 1]) {
    ans += (text1[n - 1]);
    findLCS(text1, text2, n - 1, m - 1);
  } else {
    if (LCS[n - 1][m] >= LCS[n][m - 1]) {
      findLCS(text1, text2, n - 1, m);
    } else {
      findLCS(text1, text2, n, m - 1);
    }
  }
}
signed main() {
  fast string text1, text2;
  cin >> text1 >> text2;
  int n = text1.length();
  int m = text2.length();
  memset(LCS, 0, sizeof(LCS));
  for (int i = 0; i <= n; i++) {
    for (int j = 0; j <= m; j++) {
      if (i == 0 or j == 0)
        LCS[i][j] = 0;
      else if (text1[i - 1] == text2[j - 1])
        LCS[i][j] = LCS[i - 1][j - 1] + 1;
      else
        LCS[i][j] = max(LCS[i - 1][j], LCS[i][j - 1]);
    }
  }
  findLCS(text1, text2, n, m);
  reverse(all(ans));
  cout << ans;
  return 0;
}