Problem p03165 (C++)

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

using ll = long long;

// constant
const double EPS = 1e-10;

#define REP(i, n) for (ll i = 0; i < n; i++)
#define FOR(i, s, e) for (ll i = s; i < e; i++)
#define RREP(i, n) for (ll i = n; i >= 0; i--)
#define ALL(x) (x).begin(), (x).end()
#define RALL(x) (x).rbegin(), (x).rend()

// debug
#define DUMP(x) cerr << #x << " = " << (x) << endl;
#define DEBUG(x) cerr << #x << " = " << (x) << " (L" << __LINE__ << ")" << endl;

// vector
// template<typename T> vector<vector<T>> vv(ll H, ll W, T v) { return
// vector<vector<T>> vec(H, vector<T>(W, v)); }

// view vector
template <typename T> void view(T e) { cerr << e << endl; }
template <typename T> void view(const vector<T> &v) {
  for (const auto &e : v) {
    cerr << e << " ";
  }
  cerr << endl;
}
template <typename T> void view(const vector<vector<T>> &vv) {
  for (const auto &v : vv) {
    view(v);
  }
}

// DP
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}

// ***************************************

int main() {

  string s, t;
  cin >> s >> t;

  ll S = s.size();
  ll T = t.size();

  vector<vector<ll>> dp(S + 1, vector<ll>(T + 1, 0));

  REP(i, S) {
    REP(j, T) {
      if (s[i] == t[j])
        chmax(dp[i + 1][j + 1], dp[i][j] + 1);
      chmax(dp[i + 1][j + 1], dp[i][j + 1]);
      chmax(dp[i + 1][j + 1], dp[i + 1][j]);
    }
  }

  view(dp);

  ll i = S, j = T;
  string ans = "";
  while (dp[i][j] > 0) {
    while (dp[i][j] == dp[i][j - 1])
      j--;
    while (dp[i][j] == dp[i - 1][j])
      i--;
    i--;
    j--;
    ans = s[i] + ans;
  }

  cout << ans << endl;

  return 0;
}


Fixed submission:
#ifdef _MSC_VER
#include "stdc++.h"
#endif
#ifdef __GNUC__
#include <bits/stdc++.h>
#endif
using namespace std;

using ll = long long;

// constant
const double EPS = 1e-10;

#define REP(i, n) for (ll i = 0; i < n; i++)
#define FOR(i, s, e) for (ll i = s; i < e; i++)
#define RREP(i, n) for (ll i = n; i >= 0; i--)
#define ALL(x) (x).begin(), (x).end()
#define RALL(x) (x).rbegin(), (x).rend()

// debug
#define DUMP(x) cerr << #x << " = " << (x) << endl;
#define DEBUG(x) cerr << #x << " = " << (x) << " (L" << __LINE__ << ")" << endl;

// vector
// template<typename T> vector<vector<T>> vv(ll H, ll W, T v) { return
// vector<vector<T>> vec(H, vector<T>(W, v)); }

// view vector
template <typename T> void view(T e) { cerr << e << endl; }
template <typename T> void view(const vector<T> &v) {
  for (const auto &e : v) {
    cerr << e << " ";
  }
  cerr << endl;
}
template <typename T> void view(const vector<vector<T>> &vv) {
  for (const auto &v : vv) {
    view(v);
  }
}

// DP
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}

// ***************************************

int main() {

  string s, t;
  cin >> s >> t;

  ll S = s.size();
  ll T = t.size();

  vector<vector<ll>> dp(S + 1, vector<ll>(T + 1, 0));

  REP(i, S) {
    REP(j, T) {
      if (s[i] == t[j])
        chmax(dp[i + 1][j + 1], dp[i][j] + 1);
      chmax(dp[i + 1][j + 1], dp[i][j + 1]);
      chmax(dp[i + 1][j + 1], dp[i + 1][j]);
    }
  }

  ll i = S, j = T;
  string ans = "";
  while (dp[i][j] > 0) {
    while (dp[i][j] == dp[i][j - 1])
      j--;
    while (dp[i][j] == dp[i - 1][j])
      i--;
    i--;
    j--;
    ans = s[i] + ans;
  }

  cout << ans << endl;

  return 0;
}