Problem p03165 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
#include <vector>
using namespace std;
#define assem99                                                                \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define ll long long
#define ull unsigned long long
#define ld long double
#define ii pair<int, int>
#define vii vector<ii>
#define f(i, a, b) for (int i = a; i < b; i++)
#define F first
#define S second
#define sz(a) (int)(a).size()
#define ReadFile freopen("input.txt", "r", stdin)
#define WriteFile freopen("output.txt", "w", stdout)
int x[] = {0, -1, 0, 1, -1, 1, -1, 1};
int y[] = {-1, 0, 1, 0, 1, -1, -1, 1};
const int MOD = 998244353, N = 2e5 + 5;
const double pi = acos(-1);

int dp[3005][3005];

int main() {
  assem99;

  string s, t;
  cin >> s >> t;
  f(i, 0, sz(s)) {
    f(j, 0, sz(t)) {
      if (s[i] == t[j])
        dp[i + 1][j + 1] = dp[i][j] + 1;
      else
        dp[i + 1][j + 1] = max(dp[i][j + 1], dp[i + 1][j]);
    }
  }
  int cur = dp[sz(s)][sz(t)];
  string ans = "";
  int i = sz(s), j = sz(t);
  while (i >= 0 && j >= 0) {
    if (dp[i][j] != dp[i - 1][j] && dp[i][j] != dp[i][j - 1]) {
      ans += s[i - 1];
      i--, j--;
    } else if (dp[i][j] == dp[i - 1][j])
      i--;
    else
      j--;
  }
  for (int i = sz(ans) - 1; i >= 0; i--)
    cout << ans[i];

  return 0;
}


Fixed submission:
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
#include <vector>
using namespace std;
#define assem99                                                                \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define ll long long
#define ull unsigned long long
#define ld long double
#define ii pair<int, int>
#define vii vector<ii>
#define f(i, a, b) for (int i = a; i < b; i++)
#define F first
#define S second
#define sz(a) (int)(a).size()
#define ReadFile freopen("input.txt", "r", stdin)
#define WriteFile freopen("output.txt", "w", stdout)
int x[] = {0, -1, 0, 1, -1, 1, -1, 1};
int y[] = {-1, 0, 1, 0, 1, -1, -1, 1};
const int MOD = 998244353, N = 2e5 + 5;
const double pi = acos(-1);

int dp[3005][3005];

int main() {
  assem99;

  string s, t;
  cin >> s >> t;
  f(i, 0, sz(s)) {
    f(j, 0, sz(t)) {
      if (s[i] == t[j])
        dp[i + 1][j + 1] = dp[i][j] + 1;
      else
        dp[i + 1][j + 1] = max(dp[i][j + 1], dp[i + 1][j]);
    }
  }
  int cur = dp[sz(s)][sz(t)];
  string ans = "";
  int i = sz(s), j = sz(t);
  while (i > 0 && j > 0) {
    if (dp[i][j] != dp[i - 1][j] && dp[i][j] != dp[i][j - 1]) {
      ans += s[i - 1];
      i--, j--;
    } else if (dp[i][j] == dp[i - 1][j])
      i--;
    else
      j--;
  }
  for (int i = sz(ans) - 1; i >= 0; i--)
    cout << ans[i];

  return 0;
}