Problem p03165 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define rep(i, a, n) for (int i = a; i < n; i++)
#define per(i, a, n) for (int i = n - 1; i >= a; i--)
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(), (x).end()
#define fi first
#define se second
#define SZ(x) ((int)(x).size())
#define fast                                                                   \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(0);                                                                  \
  cout.tie(0)
typedef vector<int> VI;
typedef long long ll;
typedef pair<int, int> PII;
typedef double db;
bool comp(int a, int b) { return a > b; }
mt19937 mrand(random_device{}());
const ll mod = 1000000007;
int rnd(int x) { return mrand() % x; }
ll powmod(ll a, ll b) {
  ll res = 1;
  a %= mod;
  assert(b >= 0);
  for (; b; b >>= 1) {
    if (b & 1)
      res = res * a % mod;
    a = a * a % mod;
  }
  return res;
}
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
int main() {
  fast;
  string s1, s2;
  cin >> s1 >> s2;

  ll l1, l2;
  l1 = s1.length();
  l2 = s2.length();
  int dp[1001][1001];
  memset(dp, 0, sizeof(dp));
  rep(i, 0, l1 + 1) {
    rep(j, 0, l2 + 1) {
      if (i == 0 || j == 0) {
        dp[i][j] = 0;
        //	continue;
      } else if (s1[i - 1] == s2[j - 1]) {
        dp[i][j] = 1 + dp[i - 1][j - 1];
        //	continue;
      } else
        dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]);

      //	cout<<dp[i][j]<<" ";
    }
    //	cout<<endl;
  }
  ll i = l1, j = l2;
  vector<char> c;

  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 if (dp[i][j] != dp[i - 1][j - 1]) {
      //  cout<<s1[i-1];

      c.push_back(s1[i - 1]);
      i--;
      j--;
    }
  }
  // cout<<endl;
  reverse(c.begin(), c.end());
  for (auto x : c)
    cout << x;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define rep(i, a, n) for (int i = a; i < n; i++)
#define per(i, a, n) for (int i = n - 1; i >= a; i--)
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(), (x).end()
#define fi first
#define se second
#define SZ(x) ((int)(x).size())
#define fast                                                                   \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(0);                                                                  \
  cout.tie(0)
typedef vector<int> VI;
typedef long long ll;
typedef pair<int, int> PII;
typedef double db;
bool comp(int a, int b) { return a > b; }
mt19937 mrand(random_device{}());
const ll mod = 1000000007;
int rnd(int x) { return mrand() % x; }
ll powmod(ll a, ll b) {
  ll res = 1;
  a %= mod;
  assert(b >= 0);
  for (; b; b >>= 1) {
    if (b & 1)
      res = res * a % mod;
    a = a * a % mod;
  }
  return res;
}
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
int main() {
  fast;
  string s1, s2;
  cin >> s1 >> s2;

  ll l1, l2;
  l1 = s1.length();
  l2 = s2.length();
  vector<vector<int>> dp(3010, vector<int>(3010, 0));
  //	memset(dp,0,sizeof(dp));
  rep(i, 0, l1 + 1) {
    rep(j, 0, l2 + 1) {
      if (i == 0 || j == 0) {
        dp[i][j] = 0;
        //	continue;
      } else if (s1[i - 1] == s2[j - 1]) {
        dp[i][j] = 1 + dp[i - 1][j - 1];
        //	continue;
      } else
        dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]);

      //	cout<<dp[i][j]<<" ";
    }
    //	cout<<endl;
  }
  ll i = l1, j = l2;
  vector<char> c;

  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 if (dp[i][j] != dp[i - 1][j - 1]) {
      //  cout<<s1[i-1];

      c.push_back(s1[i - 1]);
      i--;
      j--;
    }
  }
  // cout<<endl;
  reverse(c.begin(), c.end());
  for (auto x : c)
    cout << x;
}