Problem p03165 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <chrono>
#include <cstdio>
#include <fstream>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
#define REP(i, x, y) for (ll i = x; i <= y; i++)
#define SIZE(a) ll(a.size())
#define vll vector<ll>
#define MEMSET(a, n, m)                                                        \
  for (ll i = 0; i <= n; i++)                                                  \
  a[i] = m
#define BIT(n) (ll(1) << n)
#define UNIQUE(v) v.erase(unique(v.begin(), v.end()), v.end())
#define UNIQUE_ARRAY(a, x) unique(a + 1, a + x + 1) - a - 1
#define SORT(a, n) sort(a + 1, a + n + 1)
#define SORT_O(a, n, order) sort(a + 1, a + n + 1, order)
#define PER(i, y, x) for (ll i = y; i >= x; i--)
typedef long long ll;
using namespace std;

struct rect {
  ll hei;
  ll pos;
};

ll const MAX = 1e3 + 3;
ll dp[MAX][MAX] = {};
string s, t;
vector<char> ans;
int main() {
  cin >> s >> t;
  ll n = s.size();
  ll m = t.size();
  REP(i, 1, n) {
    REP(j, 1, m) {
      if (s[i - 1] != t[j - 1]) {
        dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]);
      } else {
        dp[i][j] = max({dp[i - 1][j], dp[i][j - 1], dp[i - 1][j - 1] + 1});
      }
    }
  }
  ll i = n;
  ll j = m;
  while (i != 0 && j != 0) {
    if (s[i - 1] == t[j - 1] && dp[i][j] - dp[i - 1][j - 1] == 1) {
      ans.push_back(s[i - 1]);
      i--;
      j--;
    } else {
      if (dp[i][j] == dp[i - 1][j]) {
        i--;
      } else {
        j--;
      }
    }
  }
  PER(iii, SIZE(ans) - 1, 0) { cout << ans[iii]; }
  cout << endl;
}

Fixed submission:
#include <algorithm>
#include <bitset>
#include <chrono>
#include <cstdio>
#include <fstream>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
#define REP(i, x, y) for (ll i = x; i <= y; i++)
#define SIZE(a) ll(a.size())
#define vll vector<ll>
#define MEMSET(a, n, m)                                                        \
  for (ll i = 0; i <= n; i++)                                                  \
  a[i] = m
#define BIT(n) (ll(1) << n)
#define UNIQUE(v) v.erase(unique(v.begin(), v.end()), v.end())
#define UNIQUE_ARRAY(a, x) unique(a + 1, a + x + 1) - a - 1
#define SORT(a, n) sort(a + 1, a + n + 1)
#define SORT_O(a, n, order) sort(a + 1, a + n + 1, order)
#define PER(i, y, x) for (ll i = y; i >= x; i--)
typedef long long ll;
using namespace std;

struct rect {
  ll hei;
  ll pos;
};

ll const MAX = 3e3 + 3;
ll dp[MAX][MAX] = {};
string s, t;
vector<char> ans;
int main() {
  cin >> s >> t;
  ll n = s.size();
  ll m = t.size();
  REP(i, 1, n) {
    REP(j, 1, m) {
      if (s[i - 1] != t[j - 1]) {
        dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]);
      } else {
        dp[i][j] = max({dp[i - 1][j], dp[i][j - 1], dp[i - 1][j - 1] + 1});
      }
    }
  }
  ll i = n;
  ll j = m;
  while (i != 0 && j != 0) {
    if (s[i - 1] == t[j - 1] && dp[i][j] - dp[i - 1][j - 1] == 1) {
      ans.push_back(s[i - 1]);
      i--;
      j--;
    } else {
      if (dp[i][j] == dp[i - 1][j]) {
        i--;
      } else {
        j--;
      }
    }
  }
  PER(iii, SIZE(ans) - 1, 0) { cout << ans[iii]; }
  cout << endl;
}