Problem p03165 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <chrono>
#include <climits>
#include <cmath>
#include <cstring>
#include <deque>
#include <fstream>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;

typedef long long ll;
typedef pair<ll, ll> p64;
typedef vector<ll> v64;
const int N = 2 * 1e5 + 10;
const ll inf = 1e18 + 100;
const ll mod = 1e9 + 7;

#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define rep(i, s, e) for (long long i = s; i <= e; i++)
#define brep(i, s, e) for (long long i = s; i >= e; i--)
#define all(x) x.begin(), x.end()
#define mem(x, y) memset(x, y, sizeof(x))
#define DANGER                                                                 \
  std::ios::sync_with_stdio(false);                                            \
  cin.tie(NULL);                                                               \
  cout.tie(NULL)

void solve() {
  string s, t;
  cin >> s >> t;

  ll dp[s.size() + 1][t.size() + 1];

  for (int i = 0; i <= s.size(); i++) {
    for (int j = 0; j <= t.size(); j++) {
      if (i == 0 || j == 0) {
        dp[i][j] = 0;
      } else {
        if (s[i - 1] == t[j - 1]) {
          dp[i][j] = dp[i - 1][j - 1] + 1;
        } else {
          dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]);
        }
      }
    }
  }

  // cout<<dp[s.size() +1][t.size() + 1];
  string ans = "";
  int row = s.size(), col = t.size();

  while (row != 0 || col != 0) {
    if (dp[row][col] == dp[row - 1][col]) {
      row = row - 1;
      continue;
    }
    if (dp[row][col] == dp[row][col - 1]) {
      col = col - 1;
      continue;
    }

    ans += s[row - 1];

    row--;
    col--;
  }

  reverse(ans.begin(), ans.end());
  cout << ans;
}
int main() {
  ll t = 1;
  // cin>>t;
  while (t--) {
    solve();
  }
  return 0;
}

Fixed submission:
#include <algorithm>
#include <bitset>
#include <chrono>
#include <climits>
#include <cmath>
#include <cstring>
#include <deque>
#include <fstream>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;

typedef long long ll;
typedef pair<ll, ll> p64;
typedef vector<ll> v64;
const int N = 2 * 1e5 + 10;
const ll inf = 1e18 + 100;
const ll mod = 1e9 + 7;

#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define rep(i, s, e) for (long long i = s; i <= e; i++)
#define brep(i, s, e) for (long long i = s; i >= e; i--)
#define all(x) x.begin(), x.end()
#define mem(x, y) memset(x, y, sizeof(x))
#define DANGER                                                                 \
  std::ios::sync_with_stdio(false);                                            \
  cin.tie(NULL);                                                               \
  cout.tie(NULL)

void solve() {
  string s, t;
  cin >> s >> t;

  ll dp[s.size() + 1][t.size() + 1];

  for (int i = 0; i <= s.size(); i++) {
    for (int j = 0; j <= t.size(); j++) {
      if (i == 0 || j == 0) {
        dp[i][j] = 0;
      } else {
        if (s[i - 1] == t[j - 1]) {
          dp[i][j] = dp[i - 1][j - 1] + 1;
        } else {
          dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]);
        }
      }
    }
  }

  // cout<<dp[s.size() +1][t.size() + 1];
  string ans = "";
  int row = s.size(), col = t.size();

  while (row != 0 && col != 0) {
    if (dp[row][col] == dp[row - 1][col]) {
      row = row - 1;
      continue;
    }
    if (dp[row][col] == dp[row][col - 1]) {
      col = col - 1;
      continue;
    }

    ans += s[row - 1];

    row--;
    col--;
  }

  reverse(ans.begin(), ans.end());
  cout << ans;
}
int main() {
  ll t = 1;
  // cin>>t;
  while (t--) {
    solve();
  }
  return 0;
}