Problem p03165 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

const long long mod = 1e+7;
const int N = 1e3 + 5;

#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define ll long long
#define fr(i, l, m) for (int i = l; i < m; i++)
#define vii vector<int>
#define vpr vector<pair<ll, ll>>
ll pwr(ll x, ll y);
bool isprime(int n);
string s, t, ans = "";
int lca[N][N];

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

  cin >> s >> t;
  int s_sz = s.size(), t_sz = t.size();

  fr(i, 0, s_sz + 1) {
    fr(j, 0, t_sz + 1) {
      if (i == 0 || j == 0) {
        lca[i][j] = 0;
      } else if (s[i - 1] == t[j - 1]) {
        lca[i][j] = (1 + lca[i - 1][j - 1]);
      } else {
        lca[i][j] = max(lca[i - 1][j], lca[i][j - 1]);
      }
      // cout<<lca[i][j]<<' ';
    }
    //  cout<<endl;
  }

  int i = s_sz, j = t_sz;
  while (i > 0 && j > 0) {

    if (s[i - 1] == t[j - 1]) {
      ans += s[i - 1];
      i -= 1;
      j -= 1;
    } else if (lca[i - 1][j] > lca[i][j - 1]) {
      i -= 1;

    } else {
      j -= 1;
    }
  }

  reverse(ans.begin(), ans.end());
  cout << ans << endl;
  return 0;
}
ll pwr(ll x, ll y) {
  ll ans = 1;
  x = x % mod;
  while (y > 0) {
    if (y & 1)
      ans = (x * (ans % mod)) % mod;
    x = (x * x) % mod;
    y = y / 2;
  }
  return ans % mod;
}

bool isprime(int n) {
  if (n <= 1)
    return false;
  if (n <= 3)
    return true;
  if (n % 2 == 0 || n % 3 == 0)
    return false;

  for (int i = 5; i * i <= n; i = i + 6)
    if (n % i == 0 || n % (i + 2) == 0)
      return false;

  return true;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;

const long long mod = 1e+7;
const int N = 3e3 + 5;

#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define ll long long
#define fr(i, l, m) for (int i = l; i < m; i++)
#define vii vector<int>
#define vpr vector<pair<ll, ll>>
ll pwr(ll x, ll y);
bool isprime(int n);
string s, t, ans = "";
int lca[N][N];

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

  cin >> s >> t;
  int s_sz = s.size(), t_sz = t.size();

  fr(i, 0, s_sz + 1) {
    fr(j, 0, t_sz + 1) {
      if (i == 0 || j == 0) {
        lca[i][j] = 0;
      } else if (s[i - 1] == t[j - 1]) {
        lca[i][j] = (1 + lca[i - 1][j - 1]);
      } else {
        lca[i][j] = max(lca[i - 1][j], lca[i][j - 1]);
      }
      // cout<<lca[i][j]<<' ';
    }
    //  cout<<endl;
  }

  int i = s_sz, j = t_sz;
  while (i > 0 && j > 0) {

    if (s[i - 1] == t[j - 1]) {
      ans += s[i - 1];
      i -= 1;
      j -= 1;
    } else if (lca[i - 1][j] > lca[i][j - 1]) {
      i -= 1;

    } else {
      j -= 1;
    }
  }

  reverse(ans.begin(), ans.end());
  cout << ans << endl;
  return 0;
}
ll pwr(ll x, ll y) {
  ll ans = 1;
  x = x % mod;
  while (y > 0) {
    if (y & 1)
      ans = (x * (ans % mod)) % mod;
    x = (x * x) % mod;
    y = y / 2;
  }
  return ans % mod;
}

bool isprime(int n) {
  if (n <= 1)
    return false;
  if (n <= 3)
    return true;
  if (n % 2 == 0 || n % 3 == 0)
    return false;

  for (int i = 5; i * i <= n; i = i + 6)
    if (n % i == 0 || n % (i + 2) == 0)
      return false;

  return true;
}