Problem p03165 (C++)

Buggy submission (status: Runtime Error):
#include "bits/stdc++.h"
using namespace std;
#define pb push_back
#define eb emplace_back
#define fi first
#define se second
#define FF first
#define SS second
#define pii pair<int, int>
#define fr(i, a, b) for (int i = a; i <= b; i++)
#define sz(x) (int)x.size()
#define mp make_pair
#define Mod 1000000007
typedef long long int ll;

vector<int> dp(100001, 0);
vector<int> ans(100001, 0);

int main() {

  ios_base::sync_with_stdio(0);
  cin.tie(NULL);
  cout.tie(NULL);

#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
#endif

  // dp[i][j] ->  i for string s and j for string t

  string s, t;
  string ans = "";

  cin >> s >> t;
  int n = s.length(), m = t.length();
  int i, j;

  vector<vector<pii>> dp(n + 1, vector<pii>(m + 1));

  for (i = 0; i <= n; i++)
    dp[i][0] = {0, 0};
  for (i = 0; i <= m; i++)
    dp[0][i] = {0, 0};

  for (i = 1; i <= n; i++) {
    for (j = 1; j <= m; j++) {
      if (s[i - 1] == t[j - 1])
        dp[i][j] = {1, dp[i - 1][j - 1].se + 1};
      else
        dp[i][j] = mp(0, max(dp[i][j - 1].se, dp[i - 1][j].se));
      // cout<<dp[i][j].se<<" ";
    }
    // cout<<"\n";
  }

  // cout<<dp[n][m]<<"\n";
  i = n, j = m;
  while (i > 0 && j > 0) {
    if (dp[i][j].se == dp[i - 1][j - 1].se + 1 && dp[i][j].fi == 1) {
      // cout<<j<<"\n";
      ans += t[j - 1];
      i -= 1;
      j -= 1;
    } else if (dp[i][j].se == dp[i - 1][j].se && dp[i][j].fi == 0)
      i -= 1;
    else if (dp[i][j].se == dp[i][j - 1].se && dp[i][j].fi == 0)
      j -= 1;
  }

  // cout<<reverse(ans.begin(), ans.end());
  // cout<<reverse(ans.begin(), ans.end())<<"\n";
  for (i = ans.length() - 1; i >= 0; i--)
    cout << ans[i];
  cout << "\n";
}

Fixed submission:
#include "bits/stdc++.h"
using namespace std;
#define pb push_back
#define eb emplace_back
#define fi first
#define se second
#define FF first
#define SS second
#define pii pair<int, int>
#define fr(i, a, b) for (int i = a; i <= b; i++)
#define sz(x) (int)x.size()
#define mp make_pair
#define Mod 1000000007
typedef long long int ll;

vector<int> dp(100001, 0);
vector<int> ans(100001, 0);

int main() {

  ios_base::sync_with_stdio(0);
  cin.tie(NULL);
  cout.tie(NULL);

  // #ifndef ONLINE_JUDGE
  // freopen("input.txt","r",stdin);
  // #endif

  // dp[i][j] ->  i for string s and j for string t

  string s, t;
  string ans = "";

  cin >> s >> t;
  int n = s.length(), m = t.length();
  int i, j;

  vector<vector<pii>> dp(n + 1, vector<pii>(m + 1));

  for (i = 0; i <= n; i++)
    dp[i][0] = {0, 0};
  for (i = 0; i <= m; i++)
    dp[0][i] = {0, 0};

  for (i = 1; i <= n; i++) {
    for (j = 1; j <= m; j++) {
      if (s[i - 1] == t[j - 1])
        dp[i][j] = {1, dp[i - 1][j - 1].se + 1};
      else
        dp[i][j] = mp(0, max(dp[i][j - 1].se, dp[i - 1][j].se));
      // cout<<dp[i][j].se<<" ";
    }
    // cout<<"\n";
  }

  // cout<<dp[n][m]<<"\n";
  i = n, j = m;
  while (i > 0 && j > 0) {
    if (dp[i][j].se == dp[i - 1][j - 1].se + 1 && dp[i][j].fi == 1) {
      // cout<<j<<"\n";
      ans += t[j - 1];
      i -= 1;
      j -= 1;
    } else if (dp[i][j].se == dp[i - 1][j].se && dp[i][j].fi == 0)
      i -= 1;
    else if (dp[i][j].se == dp[i][j - 1].se && dp[i][j].fi == 0)
      j -= 1;
  }

  // cout<<reverse(ans.begin(), ans.end());
  // cout<<reverse(ans.begin(), ans.end())<<"\n";
  for (i = ans.length() - 1; i >= 0; i--)
    cout << ans[i];
  cout << "\n";
}