Problem p03165 (C++)

Buggy submission (status: Time Limit Exceeded):
/*


                 """Bismillahir Rahmanur Rahim"""

*/
#include <bits/stdc++.h>
using namespace std;
#define pi 2 * acos(0.0)
#define ll long long int
#define pb push_back
#define pf push_front
const ll sz = 1000001;
#define ek(x) __builtin_popcount(x)
#define ses '\n'
#define stm istringstream
#define sob(x) x.begin(), x.end()
#define ghora                                                                  \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0);
#define gcd __gcd
ll lcm(ll x, ll y) { return (x * y) / gcd(x, y); }
#define tin                                                                    \
  ll T;                                                                        \
  cin >> T;                                                                    \
  for (ll o = 1; o <= T; o++)
#define tout cout << "Case " << o << ": ";
string s, t;
ll l1, l2;
ll dp[3010][3010];
ll lcs(ll i, ll j) {
  if (i == l1 || j == l2)
    return 0;
  if (dp[i][j] > 0)
    return dp[i][j];
  ll ans;
  if (s[i] == t[j])
    ans = 1 + lcs(i + 1, j + 1);
  else {
    ll v1 = lcs(i + 1, j);
    ll v2 = lcs(i, j + 1);
    ans = max(v1, v2);
  }
  return dp[i][j] = ans;
}
string ans;
void slcs(ll i, ll j) {
  if (i == l1 || j == l2) {
    cout << ans << ses;
    return;
  }
  if (s[i] == t[j]) {
    ans += s[i];
    slcs(i + 1, j + 1);
  } else {
    if (dp[i + 1][j] > dp[i][j + 1])
      slcs(i + 1, j);
    else
      slcs(i, j + 1);
  }
}
int main() {

  cin >> s >> t;
  memset(dp, -1, sizeof(dp));
  l1 = s.size();
  l2 = t.size();
  ll x = lcs(0, 0);
  //   cout<<x<<ses;
  slcs(0, 0);
  // cout<<"o"<<ses;
  return 0;
}
/*   --------------------
    |      ~SOHAN~       |
    |   ~Chandler68~     |
     --------------------
    || VALAR MORGULIS||==|| ALL MEN MUST DIE ||
    \\ Power Is Power//
    || I Can Do This All day ||
    // We are on a Break \\ // How you doin'? \\
    || Say My Name || ~~ || I Am The Who Knocks ||
    // I Am Ted Mosby Architect \\
    || It Is Legen --wait for it -- dary ,Legendary ||
    \\ Penny - Penny - Penny // -- Bazinga

*/


Fixed submission:
/*


                 """Bismillahir Rahmanur Rahim"""

*/
#include <bits/stdc++.h>
using namespace std;
#define pi 2 * acos(0.0)
#define ll long long int
#define pb push_back
#define pf push_front
const ll sz = 1000001;
#define ek(x) __builtin_popcount(x)
#define ses '\n'
#define stm istringstream
#define sob(x) x.begin(), x.end()
#define ghora                                                                  \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0);
#define gcd __gcd
ll lcm(ll x, ll y) { return (x * y) / gcd(x, y); }
#define tin                                                                    \
  ll T;                                                                        \
  cin >> T;                                                                    \
  for (ll o = 1; o <= T; o++)
#define tout cout << "Case " << o << ": ";
string s, t;
ll l1, l2;
ll dp[3010][3010];
ll lcs(ll i, ll j) {
  if (i == l1 || j == l2)
    return 0;
  if (dp[i][j] != -1)
    return dp[i][j];
  ll ans;
  if (s[i] == t[j])
    ans = 1 + lcs(i + 1, j + 1);
  else {
    ll v1 = lcs(i + 1, j);
    ll v2 = lcs(i, j + 1);
    ans = max(v1, v2);
  }
  return dp[i][j] = ans;
}
string ans;
void slcs(ll i, ll j) {
  if (i == l1 || j == l2) {
    cout << ans << ses;
    return;
  }
  if (s[i] == t[j]) {
    ans += s[i];
    slcs(i + 1, j + 1);
  } else {
    if (dp[i + 1][j] > dp[i][j + 1])
      slcs(i + 1, j);
    else
      slcs(i, j + 1);
  }
}
int main() {

  cin >> s >> t;
  memset(dp, -1, sizeof(dp));
  l1 = s.size();
  l2 = t.size();
  ll x = lcs(0, 0);
  //   cout<<x<<ses;
  slcs(0, 0);
  // cout<<"o"<<ses;
  return 0;
}
/*   --------------------
    |      ~SOHAN~       |
    |   ~Chandler68~     |
     --------------------
    || VALAR MORGULIS||==|| ALL MEN MUST DIE ||
    \\ Power Is Power//
    || I Can Do This All day ||
    // We are on a Break \\ // How you doin'? \\
    || Say My Name || ~~ || I Am The Who Knocks ||
    // I Am Ted Mosby Architect \\
    || It Is Legen --wait for it -- dary ,Legendary ||
    \\ Penny - Penny - Penny // -- Bazinga

*/