Problem p03165 (C++)

Buggy submission (status: Runtime Error):
#include "bits/stdc++.h"
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
using namespace std;

#define double long double
#define int long long
#define pb push_back
#define Z(c) (int)(c).size()
#define L(c) c[Z(c) - 1]
#define F first
#define S second
#define nl "\n"
#define mii map<int, int>
#define all(c) (c).begin(), (c).end()
#define rall(c) (c).rbegin(), (c).rend()
#define rep(i, iv, n) for (int(i) = (iv); (i) < (n); ++(i))
#define FO fflush(stdout)
#define D1(x) cerr << "At line " << __LINE__ << " " << #x << "=" << x << nl
#define D2(x, y)                                                               \
  cerr << "At line " << __LINE__ << " " << #x << "=" << x << " " << #y << "="  \
       << y << nl
#define D3(x, y, z)                                                            \
  cerr << "At line " << __LINE__ << " " << #x << "=" << x << " " << #y << "="  \
       << y << " " << #z << "=" << z << nl
#define tr(it, ct) for (auto &(it) : (ct))
#define pi acos(-1)
#define prt(ct)                                                                \
  for (auto &(it) : (ct))                                                      \
    cerr << it << " ";                                                         \
  cerr << nl
#define prtp(ct)                                                               \
  for (auto &(it) : (ct))                                                      \
    cerr << it.F << " " << it.S << nl;                                         \
  cerr << nl

typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int, int> pii;
typedef vector<pii> vp;
typedef tree<int, null_type, less<int>, rb_tree_tag,
             tree_order_statistics_node_update>
    baap;

void swap(char &a, char &b) {
  auto tm = a;
  a = b;
  b = tm;
}

void mina(int &a, int b) { a = (a < b ? a : b); }

void maxa(int &a, int b) { a = (a > b ? a : b); }

const int mod = 1000000007;
const int mod2 = 998244353;
const int INF = 1e16 + 18;
const int N = 1e5 + 15;
const int M = 1e3 + 13;

int dp[M][M];
char ch[M][M];
string ans;

void get_ans(int x, int y) {

  if (x == 0 || y == 0)
    return;

  if (ch[x][y] != '#') {
    ans += ch[x][y];
    get_ans(x - 1, y - 1);
  } else {
    if (dp[x - 1][y] > dp[x][y - 1])
      get_ans(x - 1, y);
    else
      get_ans(x, y - 1);
  }
}

void fft() {
  string s, t;
  cin >> s >> t;
  int n = Z(s), m = Z(t);

  for (int i = 1; i <= n; ++i) {

    for (int j = 1; j <= m; ++j) {

      if (s[i - 1] == t[j - 1]) {
        dp[i][j] = 1 + dp[i - 1][j - 1];
        ch[i][j] = s[i - 1];
      } else {
        dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]);
        ch[i][j] = '#';
      }
    }
  }

  get_ans(n, m);

  reverse(all(ans));

  cout << ans;
}

signed main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);

  int tc = 1;
  //  cin>>tc;

  rep(i, 1, tc + 1) { // cout<<"Case #"<<i<<": ";
    fft();
    if (i < tc)
      cout << nl;
  }

  return 0;
}

Fixed submission:
#include "bits/stdc++.h"
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
using namespace std;

#define double long double
#define int long long
#define pb push_back
#define Z(c) (int)(c).size()
#define L(c) c[Z(c) - 1]
#define F first
#define S second
#define nl "\n"
#define mii map<int, int>
#define all(c) (c).begin(), (c).end()
#define rall(c) (c).rbegin(), (c).rend()
#define rep(i, iv, n) for (int(i) = (iv); (i) < (n); ++(i))
#define FO fflush(stdout)
#define D1(x) cerr << "At line " << __LINE__ << " " << #x << "=" << x << nl
#define D2(x, y)                                                               \
  cerr << "At line " << __LINE__ << " " << #x << "=" << x << " " << #y << "="  \
       << y << nl
#define D3(x, y, z)                                                            \
  cerr << "At line " << __LINE__ << " " << #x << "=" << x << " " << #y << "="  \
       << y << " " << #z << "=" << z << nl
#define tr(it, ct) for (auto &(it) : (ct))
#define pi acos(-1)
#define prt(ct)                                                                \
  for (auto &(it) : (ct))                                                      \
    cerr << it << " ";                                                         \
  cerr << nl
#define prtp(ct)                                                               \
  for (auto &(it) : (ct))                                                      \
    cerr << it.F << " " << it.S << nl;                                         \
  cerr << nl

typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int, int> pii;
typedef vector<pii> vp;
typedef tree<int, null_type, less<int>, rb_tree_tag,
             tree_order_statistics_node_update>
    baap;

void swap(char &a, char &b) {
  auto tm = a;
  a = b;
  b = tm;
}

void mina(int &a, int b) { a = (a < b ? a : b); }

void maxa(int &a, int b) { a = (a > b ? a : b); }

const int mod = 1000000007;
const int mod2 = 998244353;
const int INF = 1e16 + 18;
const int N = 1e5 + 15;
const int M = 3e3 + 13;

int dp[M][M];
char ch[M][M];
string ans;

void get_ans(int x, int y) {

  if (x == 0 || y == 0)
    return;

  if (ch[x][y] != '#') {
    ans += ch[x][y];
    get_ans(x - 1, y - 1);
  } else {
    if (dp[x - 1][y] > dp[x][y - 1])
      get_ans(x - 1, y);
    else
      get_ans(x, y - 1);
  }
}

void fft() {
  string s, t;
  cin >> s >> t;
  int n = Z(s), m = Z(t);

  for (int i = 1; i <= n; ++i) {

    for (int j = 1; j <= m; ++j) {

      if (s[i - 1] == t[j - 1]) {
        dp[i][j] = 1 + dp[i - 1][j - 1];
        ch[i][j] = s[i - 1];
      } else {
        dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]);
        ch[i][j] = '#';
      }
    }
  }

  get_ans(n, m);

  reverse(all(ans));

  cout << ans;
}

signed main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);

  int tc = 1;
  //  cin>>tc;

  rep(i, 1, tc + 1) { // cout<<"Case #"<<i<<": ";
    fft();
    if (i < tc)
      cout << nl;
  }

  return 0;
}