Problem p03165 (C++)

Buggy submission (status: Time Limit Exceeded):
/////////////////////////////////TEST CASES////////////////////////////////////
/*
 */
/////////////////////////////////////CODE//////////////////////////////////////
#include <bits/stdc++.h>
using namespace std;
#define FOR(i, a, b) for (ll i = (a); i < (b); i++)
#define FORD(i, a, b) for (ll i = a; i > b; i--)
#define fastio ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
#define PI 3.14159265
typedef long long ll;
#define vl vector<ll>
#define IN(inp)                                                                \
  ll inp;                                                                      \
  cin >> inp;
#define pb push_back
#define all(a) a.begin(), a.end()
#define FR(i, a) for (auto i : a)
#define what(A) cout << #A << " is " << A << endl;
ll MAX = 100000000000;
ll MOD = 1000000007;
string s, t, ans = "";
ll dp[3010][3010], vis[3001][3001];
ll check(ll pos1, ll pos2) {
  if (vis[pos1][pos2])
    return dp[pos1][pos2];
  vis[pos1][pos2] = 1;
  if (pos1 == s.length() or pos2 == t.length())
    return 0;
  if (s[pos1] == t[pos2]) {
    dp[pos1][pos2] += 1 + check(pos1 + 1, pos2 + 1);
  } else
    dp[pos1][pos2] +=
        max(check(pos1 + 1, pos2),
            max(check(pos1, pos2 + 1), check(pos1 + 1, pos2 + 1)));
  return dp[pos1][pos2];
}
void solve() {
  cin >> s >> t;
  check(0, 0);
  ll i = 1, j = 1;
  while (i <= s.length() and j <= t.length()) {
    // what(i)
    if (dp[i][j] + 1 == dp[i - 1][j - 1] and s[i - 1] == t[j - 1]) {
      ans += s[i - 1];
      i++, j++;
    } else if (dp[i - 1][j] == dp[i][j])
      i++;
    else if ((dp[i][j - 1] == dp[i][j]))
      j++;
  }
  // reverse(all(ans));
  cout << ans;
}
int main() {
  fastio
      // freopen("input.txt", "rt", stdin);
      // freopen("output.txt", "wt", stdout);
      ll test = 1;
  // cin >> test;
  while (test--) {
    solve();
  }
}

Fixed submission:
/////////////////////////////////TEST CASES////////////////////////////////////
/*
 */
/////////////////////////////////////CODE//////////////////////////////////////
#include <bits/stdc++.h>
using namespace std;
#define FOR(i, a, b) for (ll i = (a); i < (b); i++)
#define FORD(i, a, b) for (ll i = a; i > b; i--)
#define fastio ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
#define PI 3.14159265
typedef long long ll;
#define vl vector<ll>
#define IN(inp)                                                                \
  ll inp;                                                                      \
  cin >> inp;
#define pb push_back
#define all(a) a.begin(), a.end()
#define FR(i, a) for (auto i : a)
#define what(A) cout << #A << " is " << A << endl;
ll MAX = 100000000000;
ll MOD = 1000000007;
string s, t, ans = "";
ll dp[3010][3010], vis[3001][3001];
ll check(ll pos1, ll pos2) {
  if (vis[pos1][pos2])
    return dp[pos1][pos2];
  vis[pos1][pos2] = 1;
  if (pos1 == s.length() or pos2 == t.length())
    return 0;
  if (s[pos1] == t[pos2]) {
    dp[pos1][pos2] += 1 + check(pos1 + 1, pos2 + 1);
  } else
    dp[pos1][pos2] +=
        max(check(pos1 + 1, pos2),
            max(check(pos1, pos2 + 1), check(pos1 + 1, pos2 + 1)));
  return dp[pos1][pos2];
}
void solve() {
  cin >> s >> t;
  check(0, 0);
  ll i = 1, j = 1;
  while (i <= s.length() and j <= t.length()) {
    // what(i)
    if (dp[i][j] + 1 == dp[i - 1][j - 1] and s[i - 1] == t[j - 1]) {
      ans += s[i - 1];
      i++, j++;
    } else if (dp[i - 1][j] == dp[i][j])
      i++;
    else
      j++;
  }
  // reverse(all(ans));
  cout << ans;
}
int main() {
  fastio
      // freopen("input.txt", "rt", stdin);
      // freopen("output.txt", "wt", stdout);
      ll test = 1;
  // cin >> test;
  while (test--) {
    solve();
  }
}