Problem p03165 (C++)

Buggy submission (status: Time Limit Exceeded):
/* * * * * * * * * * * **
 *                      *
 *	   saurabh8522	   *
 *	  I will handle	   *
 *		   IT.		   *
 *                      *
 * * * * * * * * * * * **/
#include <bits/stdc++.h>
#define mp(a, b) make_pair(a, b)
#define pb push_back
#define FastRead                                                               \
  ios::sync_with_stdio(0);                                                     \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define endl '\n'
#define ld long double
#define zero(a) memset((a), 0, sizeof((a)))
#define one(a) memset((a), 1, sizeof((a)))
#define minus(a) memset((a), -1, sizeof((a)))
#define all(g) g.begin(), g.end()
#define ppb pop_back
using namespace std;
typedef long long int ll;
#define MOD 1000000007
ll extgcd(ll a, ll b, ll &x, ll &y) {
  if (b == 0) {
    x = 1;
    y = 0;
    return a;
  } else {
    int g = extgcd(b, a % b, y, x);
    y -= a / b * x;
    return g;
  }
}
ll modpow(ll a, ll b) {
  ll res = 1;
  a %= MOD;
  for (; b; b >>= 1) {
    if (b & 1)
      res = res * a % MOD;
    a = a * a % MOD;
  }
  return res;
}
ll numdigit(ll n) { return floor(log10(n)) + 1; }
bool isPowerTwo(ll x) { return x && (!(x & (x - 1))); }
// ll dp[3001][3001];
// ll find(string &s,string &t,ll i,ll j){
// 	if(i<0||j<0) return 0;
// 	ll &ans=dp[i][j];
// 	if(ans!=-1) return ans;
// 	ans=0;
// 	if(s[i]==t[j]){
// 		ans=1+find(s,t,i-1,j-1);
// 	}
// 	else ans=max(ans,max(find(s,t,i,j-1),find(s,t,i-1,j)));
// 	return ans;
// }
int main() {
  FastRead;
  ll t = 1;
  // cin>>t;

  while (t--) {
    string s, u;
    cin >> s >> u;
    // cout<<s<<" "<<u<<endl;
    ll n = s.size(), m = u.size();
    // cout<<n<<" "<<m<<endl;
    ll dp[n][m];

    for (int i = 0; i < n; i++) {
      for (int j = 0; j < m; j++) {
        dp[i][j] = 0;
      }
    } // minus(dp);
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < m; j++) {
        if (s[i] == u[j]) {
          dp[i][j] = (i == 0 || j == 0) ? 1 : dp[i - 1][j - 1] + 1;
        } else {
          dp[i][j] =
              max((i == 0) ? 0 : dp[i - 1][j], (j == 0) ? 0 : dp[i][j - 1]);
        }
        // cout<<i<<" "<<j<<endl;
      }
    }
    // find(s,u,n-1,m-1);
    string ans = "";
    int i = n - 1, j = m - 1;
    // cout<<dp[n-1][m-1]<<endl;
    while (i >= 0 && j >= 0) {
      if (s[i] == u[j]) {
        ans += s[i];
        i--;
        j--;
        continue;
      }
      if (i == 0 && j == 0) {
        break;
      }
      if (i > 0) {
        if (dp[i][j] == dp[i - 1][j]) {
          i--;
        }
      } else {
        j--;
      }
    }
    reverse(ans.begin(), ans.end());
    cout << ans << endl;
  }
}

Fixed submission:
/* * * * * * * * * * * **
 *                      *
 *	   saurabh8522	   *
 *	  I will handle	   *
 *		   IT.		   *
 *                      *
 * * * * * * * * * * * **/
#include <bits/stdc++.h>
#define mp(a, b) make_pair(a, b)
#define pb push_back
#define FastRead                                                               \
  ios::sync_with_stdio(0);                                                     \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define endl '\n'
#define ld long double
#define zero(a) memset((a), 0, sizeof((a)))
#define one(a) memset((a), 1, sizeof((a)))
#define minus(a) memset((a), -1, sizeof((a)))
#define all(g) g.begin(), g.end()
#define ppb pop_back
using namespace std;
typedef long long int ll;
#define MOD 1000000007
ll extgcd(ll a, ll b, ll &x, ll &y) {
  if (b == 0) {
    x = 1;
    y = 0;
    return a;
  } else {
    int g = extgcd(b, a % b, y, x);
    y -= a / b * x;
    return g;
  }
}
ll modpow(ll a, ll b) {
  ll res = 1;
  a %= MOD;
  for (; b; b >>= 1) {
    if (b & 1)
      res = res * a % MOD;
    a = a * a % MOD;
  }
  return res;
}
ll numdigit(ll n) { return floor(log10(n)) + 1; }
bool isPowerTwo(ll x) { return x && (!(x & (x - 1))); }
// ll dp[3001][3001];
// ll find(string &s,string &t,ll i,ll j){
// 	if(i<0||j<0) return 0;
// 	ll &ans=dp[i][j];
// 	if(ans!=-1) return ans;
// 	ans=0;
// 	if(s[i]==t[j]){
// 		ans=1+find(s,t,i-1,j-1);
// 	}
// 	else ans=max(ans,max(find(s,t,i,j-1),find(s,t,i-1,j)));
// 	return ans;
// }
int main() {
  FastRead;
  ll t = 1;
  // cin>>t;

  while (t--) {
    string s, u;
    cin >> s >> u;
    // cout<<s<<" "<<u<<endl;
    ll n = s.size(), m = u.size();
    // cout<<n<<" "<<m<<endl;
    ll dp[n][m];

    for (int i = 0; i < n; i++) {
      for (int j = 0; j < m; j++) {
        dp[i][j] = 0;
      }
    } // minus(dp);
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < m; j++) {
        if (s[i] == u[j]) {
          dp[i][j] = (i == 0 || j == 0) ? 1 : dp[i - 1][j - 1] + 1;
        } else {
          dp[i][j] =
              max((i == 0) ? 0 : dp[i - 1][j], (j == 0) ? 0 : dp[i][j - 1]);
        }
        // cout<<i<<" "<<j<<endl;
      }
    }
    // find(s,u,n-1,m-1);
    string ans = "";
    int i = n - 1, j = m - 1;
    // cout<<dp[n-1][m-1]<<endl;
    while (i >= 0 && j >= 0) {
      if (s[i] == u[j]) {
        ans += s[i];
        i--;
        j--;
        continue;
      }
      if (i == 0 && j == 0) {
        break;
      }
      // if(i>0){
      if (dp[i][j] == dp[i - 1][j] && i > 0) {
        i--;
      }
      // }
      else {
        j--;
      }
    }
    reverse(ans.begin(), ans.end());
    cout << ans << endl;
  }
}