Problem p03165 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <stack>
#include <string>
#include <vector>
#define Rep(i, n) for (int i = 0; i < (n); ++i)
#define Repd(i, n) for (int i = (n)-1; i >= 0; --i)
#define For(i, a, b) for (int i = (a); i <= (b); ++i)
#define Ford(i, a, b) for (int i = (a); i >= (b); --i)
#define ALL(a) (a).begin(), (a).end()
#define taomap map<ll, int> mmap
#define Upper(k) upper_bound(a, a + n, (k))
#define Lower(k) lower_bound(a, a + n, (k))
#define pb(i) push_back((i))
#define NAME "TEST"
#define FAST                                                                   \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);                                                               \
  cout.tie(NULL);
#define READ freopen(NAME ".INP", "r", stdin);
#define WRITE freopen(NAME ".OUT", "w", stdout);

using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef float fl;
typedef double db;
const int inf = 1e6;
const int Linf = 1e9;
const ll LLinf = (ll)1e18;
const int maxn = 1e3 + 5;
/*
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
                                        Be amazed
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
*/
int a[maxn][maxn], i, n, j, m;
string x, y;
stack<char> dapso;
int commonlength() {
  for (int i = 0; i <= x.size(); ++i) {
    for (int j = 0; j <= y.size(); ++j) {
      if (i == 0 || j == 0)
        a[i][j] = 0;
      else
        a[i][j] = (x[i - 1] == y[j - 1]) ? a[i - 1][j - 1] + 1
                                         : max(a[i][j - 1], a[i - 1][j]);
    }
  }
  // cout << a[x.size()][y.size()] << endl;
  return a[x.size()][y.size()];
}

int main() {
  FAST; // READ;WRITE;
  int k;
  int n, m;
  // cin >> n >> m >> k;
  cin >> x >> y;
  n = commonlength();
  // return cout << n , 0;
  i = x.size();
  j = y.size();
  // cout << n << endl;
  // return 0;
  while (i > 0 && j > 0) {
    if (x[i - 1] == y[j - 1]) {
      dapso.push(x[i - 1]);
      i--;
      j--;
    } else {
      if (a[i - 1][j] > a[i][j - 1])
        i--;
      else
        j--;
    }
  }
  while (!dapso.empty())
    cout << dapso.top(), dapso.pop();
}


Fixed submission:
#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <stack>
#include <string>
#include <vector>
#define Rep(i, n) for (int i = 0; i < (n); ++i)
#define Repd(i, n) for (int i = (n)-1; i >= 0; --i)
#define For(i, a, b) for (int i = (a); i <= (b); ++i)
#define Ford(i, a, b) for (int i = (a); i >= (b); --i)
#define ALL(a) (a).begin(), (a).end()
#define taomap map<ll, int> mmap
#define Upper(k) upper_bound(a, a + n, (k))
#define Lower(k) lower_bound(a, a + n, (k))
#define pb(i) push_back((i))
#define NAME "TEST"
#define FAST                                                                   \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);                                                               \
  cout.tie(NULL);
#define READ freopen(NAME ".INP", "r", stdin);
#define WRITE freopen(NAME ".OUT", "w", stdout);

using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef float fl;
typedef double db;
const int inf = 1e6;
const int Linf = 1e9;
const ll LLinf = (ll)1e18;
const int maxn = 3e3 + 5;
/*
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
                                        Be amazed
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
*/
int a[maxn][maxn], i, n, j, m;
string x, y;
stack<char> dapso;
int commonlength() {
  for (int i = 0; i <= x.size(); ++i) {
    for (int j = 0; j <= y.size(); ++j) {
      if (i == 0 || j == 0)
        a[i][j] = 0;
      else
        a[i][j] = (x[i - 1] == y[j - 1]) ? a[i - 1][j - 1] + 1
                                         : max(a[i][j - 1], a[i - 1][j]);
    }
  }
  // cout << a[x.size()][y.size()] << endl;
  return a[x.size()][y.size()];
}

int main() {
  FAST; // READ;WRITE;
  int k;
  int n, m;
  // cin >> n >> m >> k;
  cin >> x >> y;
  n = commonlength();
  // return cout << n , 0;
  i = x.size();
  j = y.size();
  // cout << n << endl;
  // return 0;
  while (i > 0 && j > 0) {
    if (x[i - 1] == y[j - 1]) {
      dapso.push(x[i - 1]);
      i--;
      j--;
    } else {
      if (a[i - 1][j] > a[i][j - 1])
        i--;
      else
        j--;
    }
  }
  while (!dapso.empty())
    cout << dapso.top(), dapso.pop();
}