Problem p03165 (C++)

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

using ll = long long;
const int INF = 1 << 29;
const int MOD = (int)1e9 + 7;
const double EPS = 1e-9;
#ifdef LOCAL_ENV
#define debug(var) std::cout << #var " = " << var << std::endl
#else
#define debug(var)
#endif
#define p(var) std::cout << var << std::endl
#define PI (acos(-1))
#define rep(i, n) for (int i = 0, i##_length = (n); i < i##_length; ++i)
#define repeq(i, n) for (int i = 1, i##_length = (n); i <= i##_length; ++i)
#define all(a) (a).begin(), (a).end()
#define pb push_back
inline double isnatural(double a) { return a >= 0 && ceil(a) == floor(a); }
template <typename T> inline T gcd(T a, T b) { return b ? gcd(b, a % b) : a; }
template <typename T> inline T lcm(T a, T b) { return a / gcd(a, b) * b; }
template <typename T1, typename T2> inline bool chmax(T1 &a, T2 b) {
  return a < b && (a = b, true);
}
template <typename T1, typename T2> inline bool chmin(T1 &a, T2 b) {
  return a > b && (a = b, true);
}
template <typename T1, typename T2>
ostream &operator<<(ostream &s, const pair<T1, T2> &p) {
  return s << "(" << p.first << ", " << p.second << ")";
}
template <typename T> ostream &operator<<(ostream &s, const vector<T> &v) {
  for (int i = 0, len = v.size(); i < len; ++i) {
    s << v[i];
    if (i < len - 1)
      s << "\t";
  }
  return s;
}
template <typename T>
ostream &operator<<(ostream &s, const vector<vector<T>> &vv) {
  for (int i = 0, len = vv.size(); i < len; ++i) {
    s << vv[i] << endl;
  }
  return s;
}
template <typename T1, typename T2>
ostream &operator<<(ostream &s, const map<T1, T2> &m) {
  s << "{" << endl;
  for (typeof(m.begin()) itr = m.begin(); itr != m.end(); ++itr) {
    s << "\t" << (*itr).first << " : " << (*itr).second << endl;
  }
  s << "}" << endl;
  return s;
}

/*-----8<-----8<-----*/

// sとtの最長共通部分列を求める
// 戻り値:最長共通部分列の長さ
// dp配列を格納して返す
ll lcs(string s, string t, vector<vector<ll>> &dp) {
  dp.assign(s.size() + 1, vector<ll>(t.size() + 1, 0));
  for (ll i = 0, slen = s.size(); i < slen; i++) {
    for (ll j = 0, tlen = t.size(); j < tlen; j++) {
      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]);
    }
  }
  return dp[s.size()][t.size()];
}

// sとtの最長共通部分列を求める
// 戻り値:最長共通部分列の文字列
string buildlcs(string s, string t, vector<vector<ll>> &dp) {
  string lcsstr = "";
  ll i = s.size();
  ll j = t.size();
  if (dp[i][j] == 0)
    return 0;
  while (i > 0 && j > 0) {
    if (dp[i][j] == dp[i - 1][j]) {
      i--;
    } else if (dp[i][j] == dp[i][j - 1]) {
      j--;
    } else {
      i--;
      j--;
      lcsstr = s[i] + lcsstr;
    }
  }
  return lcsstr;
}

signed main() {
  // Longest Common Subsequence
  string s, t;
  cin >> s >> t;

  vector<vector<ll>> dp;
  ll lcsnum = lcs(s, t, dp);
  debug(lcsnum);

  string lcsstr = buildlcs(s, t, dp);
  p(lcsstr);

  return 0;
}


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

using ll = long long;
const int INF = 1 << 29;
const int MOD = (int)1e9 + 7;
const double EPS = 1e-9;
#ifdef LOCAL_ENV
#define debug(var) std::cout << #var " = " << var << std::endl
#else
#define debug(var)
#endif
#define p(var) std::cout << var << std::endl
#define PI (acos(-1))
#define rep(i, n) for (int i = 0, i##_length = (n); i < i##_length; ++i)
#define repeq(i, n) for (int i = 1, i##_length = (n); i <= i##_length; ++i)
#define all(a) (a).begin(), (a).end()
#define pb push_back
inline double isnatural(double a) { return a >= 0 && ceil(a) == floor(a); }
template <typename T> inline T gcd(T a, T b) { return b ? gcd(b, a % b) : a; }
template <typename T> inline T lcm(T a, T b) { return a / gcd(a, b) * b; }
template <typename T1, typename T2> inline bool chmax(T1 &a, T2 b) {
  return a < b && (a = b, true);
}
template <typename T1, typename T2> inline bool chmin(T1 &a, T2 b) {
  return a > b && (a = b, true);
}
template <typename T1, typename T2>
ostream &operator<<(ostream &s, const pair<T1, T2> &p) {
  return s << "(" << p.first << ", " << p.second << ")";
}
template <typename T> ostream &operator<<(ostream &s, const vector<T> &v) {
  for (int i = 0, len = v.size(); i < len; ++i) {
    s << v[i];
    if (i < len - 1)
      s << "\t";
  }
  return s;
}
template <typename T>
ostream &operator<<(ostream &s, const vector<vector<T>> &vv) {
  for (int i = 0, len = vv.size(); i < len; ++i) {
    s << vv[i] << endl;
  }
  return s;
}
template <typename T1, typename T2>
ostream &operator<<(ostream &s, const map<T1, T2> &m) {
  s << "{" << endl;
  for (typeof(m.begin()) itr = m.begin(); itr != m.end(); ++itr) {
    s << "\t" << (*itr).first << " : " << (*itr).second << endl;
  }
  s << "}" << endl;
  return s;
}

/*-----8<-----8<-----*/

// sとtの最長共通部分列を求める
// 戻り値:最長共通部分列の長さ
// dp配列を格納して返す
ll lcs(string s, string t, vector<vector<ll>> &dp) {
  dp.assign(s.size() + 1, vector<ll>(t.size() + 1, 0));
  for (ll i = 0, slen = s.size(); i < slen; i++) {
    for (ll j = 0, tlen = t.size(); j < tlen; j++) {
      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]);
    }
  }
  return dp[s.size()][t.size()];
}

// sとtの最長共通部分列を求める
// 戻り値:最長共通部分列の文字列
string buildlcs(string s, string t, vector<vector<ll>> &dp) {
  string lcsstr = "";
  ll i = s.size();
  ll j = t.size();
  if (dp[i][j] == 0)
    return "";
  while (i > 0 && j > 0) {
    if (dp[i][j] == dp[i - 1][j]) {
      i--;
    } else if (dp[i][j] == dp[i][j - 1]) {
      j--;
    } else {
      i--;
      j--;
      lcsstr = s[i] + lcsstr;
    }
  }
  return lcsstr;
}

signed main() {
  // Longest Common Subsequence
  string s, t;
  cin >> s >> t;

  vector<vector<ll>> dp;
  ll lcsnum = lcs(s, t, dp);
  debug(lcsnum);

  string lcsstr = buildlcs(s, t, dp);
  p(lcsstr);

  return 0;
}