Problem p03165 (C++)

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

using namespace std;

using ll = long long;
using vl = vector<ll>;
using vvl = vector<vl>;

#define FOR(i, a, b) for (ll i = (a); i < (b); (i)++)

#define DBG 1
#define TR(X)                                                                  \
  ({                                                                           \
    if (DBG)                                                                   \
      cerr << "TR " << (#X) << " = " << (X) << endl;                           \
  })
#define TRM(i, j, c)                                                           \
  ({                                                                           \
    if (DBG)                                                                   \
      cerr << "TR (" << (i) << ", " << (j) << ") = " << (c)[i][j] << endl;     \
  })

int main(void) {
  cin.sync_with_stdio(false);
  cin.tie(0);
  string s, t;
  cin >> s >> t;

  vvl dp(s.size() + 1, vl(t.size() + 1, 0));

  FOR(i, 1, s.size() + 1) FOR(j, 1, t.size() + 1) {
    dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]);
    if (s[i - 1] == t[j - 1]) {
      dp[i][j] = max(dp[i][j], dp[i - 1][j - 1] + 1);
    }
    TRM(i, j, dp);
  }
  ll idx_i = s.size(), idx_j = t.size();
  vector<char> res(dp[s.size()][t.size()]);

  while (idx_i >= 1 && idx_j >= 1) {
    ll left = dp[idx_i - 1][idx_j];
    ll up = dp[idx_i][idx_j - 1];
    ll lup = dp[idx_i - 1][idx_j - 1];

    TRM(idx_i, idx_j, dp);
    TR(dp[idx_i - 1][idx_j]);
    TR(dp[idx_i][idx_j - 1]);

    if (s[idx_i - 1] == t[idx_j - 1] && lup <= up && lup <= left) {
      res[dp[idx_i][idx_j] - 1] = s[idx_i - 1];
      TR(res[dp[idx_i][idx_j] - 1]);

      idx_i--;
      idx_j--;
    } else if (up == left) {
      if (idx_i < idx_j) {
        idx_j--;
      } else {
        idx_i--;
      }
    } else if (up > left) {
      if (idx_j == 1) {
        idx_i--;
      } else
        idx_j--;
    } else {
      if (idx_i == 1) {
        idx_j--;
      } else
        idx_i--;
    }
  }
  for (const auto &it : res) {
    cout << it;
  }
  cout << endl;
}


Fixed submission:
#include <bits/stdc++.h>

using namespace std;

using ll = long long;
using vl = vector<ll>;
using vvl = vector<vl>;

#define FOR(i, a, b) for (ll i = (a); i < (b); (i)++)

#define DBG 0
#define TR(X)                                                                  \
  ({                                                                           \
    if (DBG)                                                                   \
      cerr << "TR " << (#X) << " = " << (X) << endl;                           \
  })
#define TRM(i, j, c)                                                           \
  ({                                                                           \
    if (DBG)                                                                   \
      cerr << "TR (" << (i) << ", " << (j) << ") = " << (c)[i][j] << endl;     \
  })

int main(void) {
  cin.sync_with_stdio(false);
  cin.tie(0);
  string s, t;
  cin >> s >> t;

  vvl dp(s.size() + 1, vl(t.size() + 1, 0));

  FOR(i, 1, s.size() + 1) FOR(j, 1, t.size() + 1) {
    dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]);
    if (s[i - 1] == t[j - 1]) {
      dp[i][j] = max(dp[i][j], dp[i - 1][j - 1] + 1);
    }
    TRM(i, j, dp);
  }
  ll idx_i = s.size(), idx_j = t.size();
  vector<char> res(dp[s.size()][t.size()]);

  while (idx_i >= 1 && idx_j >= 1) {
    ll left = dp[idx_i - 1][idx_j];
    ll up = dp[idx_i][idx_j - 1];
    ll lup = dp[idx_i - 1][idx_j - 1];

    TRM(idx_i, idx_j, dp);
    TR(dp[idx_i - 1][idx_j]);
    TR(dp[idx_i][idx_j - 1]);

    if (s[idx_i - 1] == t[idx_j - 1] && lup <= up && lup <= left) {
      res[dp[idx_i][idx_j] - 1] = s[idx_i - 1];
      TR(res[dp[idx_i][idx_j] - 1]);

      idx_i--;
      idx_j--;
    } else if (up == left) {
      if (idx_i < idx_j) {
        idx_j--;
      } else {
        idx_i--;
      }
    } else if (up > left) {
      if (idx_j == 1) {
        idx_i--;
      } else
        idx_j--;
    } else {
      if (idx_i == 1) {
        idx_j--;
      } else
        idx_i--;
    }
  }
  for (const auto &it : res) {
    cout << it;
  }
  cout << endl;
}