Problem p03165 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;

// #define int long long

#define pb push_back
#define F first
#define S second
#define endl "\n"
#define rep(i, a, b) for (int i = a; i < (int)b; i++)
#define req(i, a, b) for (int i = (int)b - 1; i >= a; i--)
#define all(a) (a).begin(), (a).end()
#define ret(x)                                                                 \
  {                                                                            \
    cout << x << endl;                                                         \
    return;                                                                    \
  }
#define sz(x) (int)x.size()
#define type(x) typeid(x).name()

typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> pii;
typedef vector<vector<int>> vii;

int gcd(int a, int b) { return b ? gcd(b, a % b) : a; }

#ifdef LOCAL
#define wa(x) cerr << (#x) << " --- " << (x) << endl
#define pvi(v)                                                                 \
  {                                                                            \
    for (auto it : v)                                                          \
      cerr << it << " ";                                                       \
    cerr << endl;                                                              \
  }
#define line1 cerr << "---------------------------" << endl;
#else
#define wa     //
#define line1  //
#define pvi    //
#define printf //
#endif

void solve();
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout << fixed << setprecision(10);

  int t = 1;
  // cin>>t;
  while (t--) {
    solve();
    line1;
  }
  printf("Time : %fms\n", (1000 * (double)clock()) / CLOCKS_PER_SEC);
  return 0;
}

//___________________________________________________________________________________________________

void solve() {
  string a, b;
  cin >> a >> b;
  int la = a.size();
  int lb = b.size();

  // dp[i][j] :
  // Length of LCS when we consider the first i characters
  // of string_a and first j characters of string_b.
  vector<vi> dp(la + 1, vi(lb + 1, 0));

  rep(i, 1, la + 1) {
    rep(j, 1, lb + 1) {
      if (a[i - 1] == b[j - 1])
        dp[i][j] = max(dp[i][j], dp[i - 1][j - 1] + 1);
      else
        dp[i][j] = max({dp[i][j], dp[i - 1][j], dp[i][j - 1]});
    }
  }

  int i = la, j = lb;
  string ans = {};
  while (i != 0 || j != 0) {
    if (i > 0 && j > 0 && dp[i][j] == dp[i - 1][j - 1] + 1) {
      if (a[i - 1] == b[j - 1]) {
        ans += a[i - 1];
        i--, j--;
      }
    } else if (j > 0 && dp[i][j] == dp[i][j - 1])
      j--;
    else if (i > 0 && dp[i][j] == dp[i - 1][j])
      i--;
  }

  reverse(all(ans));
  cout << ans << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;

// #define int long long

#define pb push_back
#define F first
#define S second
#define endl "\n"
#define rep(i, a, b) for (int i = a; i < (int)b; i++)
#define req(i, a, b) for (int i = (int)b - 1; i >= a; i--)
#define all(a) (a).begin(), (a).end()
#define ret(x)                                                                 \
  {                                                                            \
    cout << x << endl;                                                         \
    return;                                                                    \
  }
#define sz(x) (int)x.size()
#define type(x) typeid(x).name()

typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> pii;
typedef vector<vector<int>> vii;

int gcd(int a, int b) { return b ? gcd(b, a % b) : a; }

#ifdef LOCAL
#define wa(x) cerr << (#x) << " --- " << (x) << endl
#define pvi(v)                                                                 \
  {                                                                            \
    for (auto it : v)                                                          \
      cerr << it << " ";                                                       \
    cerr << endl;                                                              \
  }
#define line1 cerr << "---------------------------" << endl;
#else
#define wa     //
#define line1  //
#define pvi    //
#define printf //
#endif

void solve();
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout << fixed << setprecision(10);

  int t = 1;
  // cin>>t;
  while (t--) {
    solve();
    line1;
  }
  printf("Time : %fms\n", (1000 * (double)clock()) / CLOCKS_PER_SEC);
  return 0;
}

//___________________________________________________________________________________________________

void solve() {
  string a, b;
  cin >> a >> b;
  int la = a.size();
  int lb = b.size();

  // dp[i][j] :
  // Length of LCS when we consider the first i characters
  // of string_a and first j characters of string_b.
  vector<vi> dp(la + 1, vi(lb + 1, 0));

  rep(i, 1, la + 1) {
    rep(j, 1, lb + 1) {
      if (a[i - 1] == b[j - 1])
        dp[i][j] = max(dp[i][j], dp[i - 1][j - 1] + 1);
      else
        dp[i][j] = max({dp[i][j], dp[i - 1][j], dp[i][j - 1]});
    }
  }

  int i = la, j = lb;
  string ans = {};
  while (i != 0 || j != 0) {
    if (i > 0 && j > 0 && a[i - 1] == b[j - 1]) {
      ans += a[i - 1];
      i--, j--;
    } else if (j > 0 && dp[i][j] == dp[i][j - 1])
      j--;
    else if (i > 0 && dp[i][j] == dp[i - 1][j])
      i--;
  }

  reverse(all(ans));
  cout << ans << endl;
}