Problem p03165 (C++)

Buggy submission (status: Runtime Error):
#define NDEBUG
NDEBUG

#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <cmath>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <memory>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <string>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;

template <class T> using vec = std::vector<T>;

template <class T> using vec2d = std::vector<std::vector<T>>;

template <class T>
vec2d<T> makeVec2d(const int n1, const int n2, const T initValue) {
  return vec2d<T>(n1, vec<T>(n2, initValue));
}

#define FOR(i, n) for (int i = 0; i < (n); ++i)

#define all(c) c.begin(), c.end()

// TC_REMOVE_BEGIN
/// caide keep
bool __hack = std::ios::sync_with_stdio(false);
/// caide keep
auto __hack1 = cin.tie(nullptr);
// TC_REMOVE_END

// Section with adoption of array and vector algorithms.

// #include <ext/pb_ds/tree_policy.hpp>
// #include <ext/pb_ds/assoc_container.hpp>
//
// template <class T> using StdTree = tree<T, null_type, less<T>,
// rb_tree_tag,tree_order_statistics_node_update>;

namespace template_util {

constexpr int bytecount(uint64_t x) { return x ? 1 + bytecount(x >> 8) : 0; }

template <int N> struct bytetype {};

/// caide keep
template <uint64_t N> struct minimal_uint : bytetype<bytecount(N)> {};
} // namespace template_util

template <class T> T next(istream &in) {
  T ret;
  in >> ret;
  return ret;
}

void solve(istream &in, ostream &out) {
  auto s = next<string>(in);
  auto t = next<string>(in);

  vec2d<int> f = makeVec2d<int>(s.length() + 1, t.length() + 1, -1);
  vec2d<int> p = makeVec2d<int>(s.length() + 1, t.length() + 1, -1);
  f[0][0] = 0;
  FOR(i, s.length()) {
    FOR(j, t.length()) {
      if (f[i][j] != -1) {
        if (f[i + 1][j] < f[i][j]) {
          f[i + 1][j] = f[i][j];
          p[i + 1][j] = 0;
        }
        if (f[i][j + 1] < f[i][j]) {
          f[i][j + 1] = f[i][j];
          p[i][j + 1] = 1;
        }
        if (s[i] == t[j]) {
          if (f[i + 1][j + 1] < f[i][j] + 1) {
            f[i + 1][j + 1] = f[i][j] + 1;
            p[i + 1][j + 1] = 2;
          }
        }
      }
    }
  }
  int res = 0;
  int bi = -1, bj = -1;
  FOR(i, s.length() + 1) {
    FOR(j, t.length() + 1) {
      if (res < f[i][j]) {
        res = f[i][j];
        bi = i;
        bj = j;
      }
    }
  }
  string r;
  while (bi != 0 || bj != 0) {
    if (p[bi][bj] == 2) {
      r.push_back(s[bi - 1]);
      --bi;
      --bj;
    } else if (p[bi][bj] == 0) {
      --bi;
    } else {
      --bj;
    }
  }
  reverse(all(r));
  out << r << "\n";
}

// #include <fstream>

int main() {
  //  ifstream fin("bridges.in");
  //  ofstream fout("bridges.out");
  solve(cin, cout);
  //  solve(fin, fout);
  return 0;
}


Fixed submission:
#define NDEBUG
NDEBUG

#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <cmath>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <memory>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <string>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;

template <class T> using vec = std::vector<T>;

template <class T> using vec2d = std::vector<std::vector<T>>;

template <class T>
vec2d<T> makeVec2d(const int n1, const int n2, const T initValue) {
  return vec2d<T>(n1, vec<T>(n2, initValue));
}

#define FOR(i, n) for (int i = 0; i < (n); ++i)

#define all(c) c.begin(), c.end()

// TC_REMOVE_BEGIN
/// caide keep
bool __hack = std::ios::sync_with_stdio(false);
/// caide keep
auto __hack1 = cin.tie(nullptr);
// TC_REMOVE_END

// Section with adoption of array and vector algorithms.

// #include <ext/pb_ds/tree_policy.hpp>
// #include <ext/pb_ds/assoc_container.hpp>
//
// template <class T> using StdTree = tree<T, null_type, less<T>,
// rb_tree_tag,tree_order_statistics_node_update>;

namespace template_util {

constexpr int bytecount(uint64_t x) { return x ? 1 + bytecount(x >> 8) : 0; }

template <int N> struct bytetype {};

/// caide keep
template <uint64_t N> struct minimal_uint : bytetype<bytecount(N)> {};
} // namespace template_util

template <class T> T next(istream &in) {
  T ret;
  in >> ret;
  return ret;
}

void solve(istream &in, ostream &out) {
  auto s = next<string>(in);
  auto t = next<string>(in);

  vec2d<int> f = makeVec2d<int>(s.length() + 1, t.length() + 1, -1);
  vec2d<int> p = makeVec2d<int>(s.length() + 1, t.length() + 1, -1);
  f[0][0] = 0;
  FOR(i, s.length()) {
    FOR(j, t.length()) {
      if (f[i][j] != -1) {
        if (f[i + 1][j] < f[i][j]) {
          f[i + 1][j] = f[i][j];
          p[i + 1][j] = 0;
        }
        if (f[i][j + 1] < f[i][j]) {
          f[i][j + 1] = f[i][j];
          p[i][j + 1] = 1;
        }
        if (s[i] == t[j]) {
          if (f[i + 1][j + 1] < f[i][j] + 1) {
            f[i + 1][j + 1] = f[i][j] + 1;
            p[i + 1][j + 1] = 2;
          }
        }
      }
    }
  }
  int res = 0;
  int bi = 0, bj = 0;
  FOR(i, s.length() + 1) {
    FOR(j, t.length() + 1) {
      if (res < f[i][j]) {
        res = f[i][j];
        bi = i;
        bj = j;
      }
    }
  }
  string r;
  while (bi != 0 || bj != 0) {
    if (p[bi][bj] == 2) {
      r.push_back(s[bi - 1]);
      --bi;
      --bj;
    } else if (p[bi][bj] == 0) {
      --bi;
    } else {
      --bj;
    }
  }
  reverse(all(r));
  out << r << "\n";
}

// #include <fstream>

int main() {
  //  ifstream fin("bridges.in");
  //  ofstream fout("bridges.out");
  solve(cin, cout);
  //  solve(fin, fout);
  return 0;
}