Problem p03165 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
void print_err() { cerr << "\n"; }
template <class T, class... Arg> void print_err(T x, Arg &&...args) {
  cerr << x << " ";
  print_err(args...);
}
template <class T> void print_container(T &cont) {
  for (auto iter : cont) {
    cerr << iter << " ";
  }
  cerr << "\n";
}
#ifdef local
#define debug(...) print_err(_VA_ARGS_)
#define debug_c(container) print_container(container)
#else
#define debug(...)
#define debug_c(container)
#endif
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>
// using namespace __gnu_pbds;
#define ordered_set                                                            \
  tree<int, null_type, less<int>, rb_tree_tag,                                 \
       tree_order_statistics_node_update>
#define mod 1000000009
#define ll long long int
#define ff first
#define ss second
#define pb push_back
#define mp make_pair
#define pll pair<ll, ll>
#define vl vector<ll>
#define vpll vector<pair<ll, ll>>
#define mll map<ll, ll>
#define mllrev map<ll, ll, greater<int>>
#define ps(x, y) fixed << setprecision(y) << x
#define w(t)                                                                   \
  ll t;                                                                        \
  cin >> t;                                                                    \
  while (t--)
#define fo(i, n) for (ll i = 0; i < n; ++i)
#define foab(i, a, b) for (ll i = a; i < b; ++i)
#define inp cin >>
#define pr cout <<
#define all(A) (A).begin(), (A).end()
#define fastio                                                                 \
  ios_base::sync_with_stdio(NULL);                                             \
  cin.tie(NULL);                                                               \
  cout.tie(NULL);
class Graph {
public:
  list<ll> *adj_list;
  ll v;
  Graph(ll v) {
    this->v = v;
    adj_list = new list<ll>[v];
  }
  void add_edge(ll u, ll v, bool bidirectional = true) {
    adj_list[u].push_back(v);
    if (bidirectional) {
      adj_list[v].push_back(u);
    }
  }
  void print();
  void connected_compo();
  void dfs(vector<vector<ll>> vect, ll k1, ll k2, ll n);
  void bfs(ll v, bool visited[]);
};
int dp[3001][3001];
int recursive(string str1, string str2, int n, int m) {
  if (n == 0 || m == 0) {
    return 0;
  }
  if (dp[n][m] != -1) {
    return dp[n][m];
  }
  if (str1[n - 1] == str2[m - 1]) {
    return dp[n][m] = 1 + recursive(str1, str2, n - 1, m - 1);
  } else {
    return dp[n][m] = max(recursive(str1, str2, n - 1, m),
                          recursive(str1, str2, n, m - 1));
  }
  return dp[n][m];
}
void solve() {
  memset(dp, -1, sizeof(dp));
  ll n, m;
  string str1, str2;
  cin >> str1 >> str2;
  n = str1.size();
  m = str2.size();
  // cout << str1 << " " << str2 << endl;
  ll temp = recursive(str1, str2, n, m);
  string ans = "";
  ll i = n, j = m;
  // cout << dp[n][m];
  while (i != 0 and j != 0) {
    if (str1[i - 1] == str2[j - 1]) {
      ans.pb(str1[i - 1]);
      i--;
      j--;
    } else {
      if (dp[i - 1][j] > dp[i][j - 1]) {
        i--;
      } else {
        j--;
      }
    }
  }
  reverse(ans.begin(), ans.end());
  cout << ans << endl;
  // return;
}
// Rod cutting problem test case
//  5
//  100
//  23 15 50
//  4000
//  3 4 5
//  7
//  5 5 2
//  4
//  2 1 1
//  5
//  5 3 2
int main() {
  fastio
#ifdef local
      freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
  freopen("error.txt", "w", stderr);
#endif
  // w(t) {
  solve();
  // }
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
void print_err() { cerr << "\n"; }
template <class T, class... Arg> void print_err(T x, Arg &&...args) {
  cerr << x << " ";
  print_err(args...);
}
template <class T> void print_container(T &cont) {
  for (auto iter : cont) {
    cerr << iter << " ";
  }
  cerr << "\n";
}
#ifdef local
#define debug(...) print_err(_VA_ARGS_)
#define debug_c(container) print_container(container)
#else
#define debug(...)
#define debug_c(container)
#endif
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>
// using namespace __gnu_pbds;
#define ordered_set                                                            \
  tree<int, null_type, less<int>, rb_tree_tag,                                 \
       tree_order_statistics_node_update>
#define mod 1000000009
#define ll long long int
#define ff first
#define ss second
#define pb push_back
#define mp make_pair
#define pll pair<ll, ll>
#define vl vector<ll>
#define vpll vector<pair<ll, ll>>
#define mll map<ll, ll>
#define mllrev map<ll, ll, greater<int>>
#define ps(x, y) fixed << setprecision(y) << x
#define w(t)                                                                   \
  ll t;                                                                        \
  cin >> t;                                                                    \
  while (t--)
#define fo(i, n) for (ll i = 0; i < n; ++i)
#define foab(i, a, b) for (ll i = a; i < b; ++i)
#define inp cin >>
#define pr cout <<
#define all(A) (A).begin(), (A).end()
#define fastio                                                                 \
  ios_base::sync_with_stdio(NULL);                                             \
  cin.tie(NULL);                                                               \
  cout.tie(NULL);
class Graph {
public:
  list<ll> *adj_list;
  ll v;
  Graph(ll v) {
    this->v = v;
    adj_list = new list<ll>[v];
  }
  void add_edge(ll u, ll v, bool bidirectional = true) {
    adj_list[u].push_back(v);
    if (bidirectional) {
      adj_list[v].push_back(u);
    }
  }
  void print();
  void connected_compo();
  void dfs(vector<vector<ll>> vect, ll k1, ll k2, ll n);
  void bfs(ll v, bool visited[]);
};
int dp[3001][3001];
int recursive(string &str1, string &str2, int n, int m) {
  if (n == 0 || m == 0) {
    return 0;
  }
  if (dp[n][m] != -1) {
    return dp[n][m];
  }
  if (str1[n - 1] == str2[m - 1]) {
    return dp[n][m] = 1 + recursive(str1, str2, n - 1, m - 1);
  } else {
    return dp[n][m] = max(recursive(str1, str2, n - 1, m),
                          recursive(str1, str2, n, m - 1));
  }
  return dp[n][m];
}
void solve() {
  memset(dp, -1, sizeof(dp));
  ll n, m;
  string str1, str2;
  cin >> str1 >> str2;
  n = str1.size();
  m = str2.size();
  // cout << str1 << " " << str2 << endl;
  ll temp = recursive(str1, str2, n, m);
  string ans = "";
  ll i = n, j = m;
  // cout << dp[n][m];
  while (i != 0 and j != 0) {
    if (str1[i - 1] == str2[j - 1]) {
      ans.pb(str1[i - 1]);
      i--;
      j--;
    } else {
      if (dp[i - 1][j] > dp[i][j - 1]) {
        i--;
      } else {
        j--;
      }
    }
  }
  reverse(ans.begin(), ans.end());
  cout << ans << endl;
  // return;
}
// Rod cutting problem test case
//  5
//  100
//  23 15 50
//  4000
//  3 4 5
//  7
//  5 5 2
//  4
//  2 1 1
//  5
//  5 3 2
int main() {
  fastio
#ifdef local
      freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
  freopen("error.txt", "w", stderr);
#endif
  // w(t) {
  solve();
  // }
  return 0;
}