Problem p03165 (C++)

Buggy submission (status: Runtime Error):

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define vi vector<int>
#define vl vector<long long>
#define vvi vector<vector<int>>
#define fin(ar, k, n)                                                          \
  for (int i = k; i < n; i++)                                                  \
  cin >> ar[i]
#define fout(ar, k, n)                                                         \
  for (int i = k; i < n; i++)                                                  \
  cout << ar[i] << ' '
#define all(z) z.begin(), z.end()
#define mcc ((int)1e9 + 7)
#define mcf 998244353
#define mi map<int, int>
#define mem(a, n) memset(a, n, sizeof(a))
#define pii pair<int, int>
#define mp(a, b) make_pair(a, b)

using namespace std;
using namespace __gnu_pbds;
typedef long long ll;
typedef tree<int, null_type, less<int>, rb_tree_tag,
             tree_order_statistics_node_update>
    fst;

const int N = 1e3 + 4;
int dp[N][N];
string s, t;
stack<char> ans;

void solve() {
  cin >> s;
  cin >> t;
  int n = s.length(), m = t.length();
  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];
      else
        dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]);
    }
  }
  int i = n, j = m;
  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 {
      ans.push(s[i - 1]);
      i--;
      j--;
    }
  }
  while (!ans.empty()) {
    cout << ans.top();
    ans.pop();
  }
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  int t = 1;
  // cin>>t;
  while (t--)
    solve();
  return 0;
}

Fixed submission:

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define vi vector<int>
#define vl vector<long long>
#define vvi vector<vector<int>>
#define fin(ar, k, n)                                                          \
  for (int i = k; i < n; i++)                                                  \
  cin >> ar[i]
#define fout(ar, k, n)                                                         \
  for (int i = k; i < n; i++)                                                  \
  cout << ar[i] << ' '
#define all(z) z.begin(), z.end()
#define mcc ((int)1e9 + 7)
#define mcf 998244353
#define mi map<int, int>
#define mem(a, n) memset(a, n, sizeof(a))
#define pii pair<int, int>
#define mp(a, b) make_pair(a, b)

using namespace std;
using namespace __gnu_pbds;
typedef long long ll;
typedef tree<int, null_type, less<int>, rb_tree_tag,
             tree_order_statistics_node_update>
    fst;

const int N = 3e3 + 4;
int dp[N][N];
string s, t;
stack<char> ans;

void solve() {
  cin >> s;
  cin >> t;
  int n = s.length(), m = t.length();
  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];
      else
        dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]);
    }
  }
  int i = n, j = m;
  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 {
      ans.push(s[i - 1]);
      i--;
      j--;
    }
  }
  while (!ans.empty()) {
    cout << ans.top();
    ans.pop();
  }
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  int t = 1;
  // cin>>t;
  while (t--)
    solve();
  return 0;
}