Problem p03165 (C++)

Buggy submission (status: Runtime Error):
# 1 "f.cpp"
# 1 "<built-in>"
# 1 "<command-line>"
# 1 "f.cpp"
# 1 "/home/tysm/dev/cpplib/bin/../include/cpplib/stdinc.hpp" 1

# 12 "/home/tysm/dev/cpplib/bin/../include/cpplib/stdinc.hpp"
# 1 "/home/tysm/dev/cpplib/bin/../stdlib/bits/stdc++.h" 1
#include <bits/stdc++.h>
# 13 "/home/tysm/dev/cpplib/bin/../include/cpplib/stdinc.hpp" 2
# 1 "/home/tysm/dev/cpplib/bin/../stdlib/ext/pb_ds/assoc_container.hpp" 1
#include <ext/pb_ds/assoc_container.hpp>
# 14 "/home/tysm/dev/cpplib/bin/../include/cpplib/stdinc.hpp" 2
# 1 "/home/tysm/dev/cpplib/bin/../stdlib/ext/pb_ds/tree_policy.hpp" 1
#include <ext/pb_ds/tree_policy.hpp>
# 15 "/home/tysm/dev/cpplib/bin/../include/cpplib/stdinc.hpp" 2

using namespace std;
using namespace __gnu_pbds;

using ii = pair<long long, long long>;
using vi = vector<long long>;
using vd = vector<long double>;
using vb = vector<bool>;
using vii = vector<ii>;
using vvi = vector<vi>;
using vvd = vector<vd>;
using vvb = vector<vb>;
using vvii = vector<vii>;
# 50 "/home/tysm/dev/cpplib/bin/../include/cpplib/stdinc.hpp"
inline long long mod(const long long n,
                     const long long m = ((long long)1e9 + 7)) {
  return (n % m + m) % m;
}

/**
 * Euclidean GCD.
 *
 * Time Complexity: O(log(min(a, b))).
 * Space Complexity: O(log(min(a, b))).
 */
long long gcd(const long long a, const long long b) {
  if (a < 0 or b < 0)
    return gcd(abs(a), abs(b));
  if (a == 0)
    return b;
  return gcd(b % a, a);
}

/**
 * Euclidean derivated LCM.
 *
 * Time Complexity: O(log(min(a, b))).
 * Space Complexity: O(log(min(a, b))).
 */
long long lcm(const long long a, const long long b) {
  // same as a*b/gcd(a, b) but avoiding overflow.
  return a / gcd(a, b) * b;
}
# 2 "f.cpp" 2
# 1 "/home/tysm/dev/cpplib/bin/../include/cpplib/string/longest-commom-subsequence.hpp" 1

# 1 "/home/tysm/dev/cpplib/bin/../include/cpplib/array/longest-commom-subsequence.hpp" 1

vi lcs(const vi &a, const vi &b) {
  long long n = a.size(), m = b.size();
  vvi dis(n + 1, vi(m + 1));
  for (long long i = 0; i <= n; ++i) {
    for (long long j = 0; j <= m; ++j) {
      if (!i or !j)
        dis[i][j] = max(i, j);
      else {
        dis[i][j] = min(
            {(a[i - 1] == b[j - 1] ? dis[i - 1][j - 1] : ((long long)1e9 + 1)),
             dis[i][j - 1] + 1, dis[i - 1][j] + 1});
      }
    }
  }
  vi res;
  long long i = n, j = m;
  while (i or j) {
    if (a[i - 1] == b[j - 1]) {
      res.push_back(a[i - 1]);
      i--;
      j--;
    } else if (dis[i - 1][j] <= dis[i][j - 1])
      i--;
    else
      j--;
  }
  reverse(res.begin(), res.end());
  return res;
}
# 4 "/home/tysm/dev/cpplib/bin/../include/cpplib/string/longest-commom-subsequence.hpp" 2

string lcs(const string &a, const string &b) {
  vi arr_a, arr_b;
  for (char c : a)
    arr_a.push_back(c);
  for (char c : b)
    arr_b.push_back(c);
  string res;
  for (char i : lcs(arr_a, arr_b))
    res += i;
  return res;
}
# 3 "f.cpp" 2

int32_t main() {
  (ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL));
  string a, b;
  cin >> a >> b;
  cout << lcs(a, b) << "\n";
  return 0;
}


Fixed submission:
# 1 "f.cpp"
# 1 "<built-in>"
# 1 "<command-line>"
# 1 "f.cpp"
# 1 "/home/tysm/dev/cpplib/bin/../include/cpplib/stdinc.hpp" 1

# 12 "/home/tysm/dev/cpplib/bin/../include/cpplib/stdinc.hpp"
# 1 "/home/tysm/dev/cpplib/bin/../stdlib/bits/stdc++.h" 1
#include <bits/stdc++.h>
# 13 "/home/tysm/dev/cpplib/bin/../include/cpplib/stdinc.hpp" 2
# 1 "/home/tysm/dev/cpplib/bin/../stdlib/ext/pb_ds/assoc_container.hpp" 1
#include <ext/pb_ds/assoc_container.hpp>
# 14 "/home/tysm/dev/cpplib/bin/../include/cpplib/stdinc.hpp" 2
# 1 "/home/tysm/dev/cpplib/bin/../stdlib/ext/pb_ds/tree_policy.hpp" 1
#include <ext/pb_ds/tree_policy.hpp>
# 15 "/home/tysm/dev/cpplib/bin/../include/cpplib/stdinc.hpp" 2

using namespace std;
using namespace __gnu_pbds;

using ii = pair<long long, long long>;
using vi = vector<long long>;
using vd = vector<long double>;
using vb = vector<bool>;
using vii = vector<ii>;
using vvi = vector<vi>;
using vvd = vector<vd>;
using vvb = vector<vb>;
using vvii = vector<vii>;
# 50 "/home/tysm/dev/cpplib/bin/../include/cpplib/stdinc.hpp"
inline long long mod(const long long n,
                     const long long m = ((long long)1e9 + 7)) {
  return (n % m + m) % m;
}

/**
 * Euclidean GCD.
 *
 * Time Complexity: O(log(min(a, b))).
 * Space Complexity: O(log(min(a, b))).
 */
long long gcd(const long long a, const long long b) {
  if (a < 0 or b < 0)
    return gcd(abs(a), abs(b));
  if (a == 0)
    return b;
  return gcd(b % a, a);
}

/**
 * Euclidean derivated LCM.
 *
 * Time Complexity: O(log(min(a, b))).
 * Space Complexity: O(log(min(a, b))).
 */
long long lcm(const long long a, const long long b) {
  // same as a*b/gcd(a, b) but avoiding overflow.
  return a / gcd(a, b) * b;
}
# 2 "f.cpp" 2
# 1 "/home/tysm/dev/cpplib/bin/../include/cpplib/string/longest-commom-subsequence.hpp" 1

# 1 "/home/tysm/dev/cpplib/bin/../include/cpplib/array/longest-commom-subsequence.hpp" 1

vi lcs(const vi &a, const vi &b) {
  long long n = a.size(), m = b.size();
  vvi dis(n + 1, vi(m + 1));
  for (long long i = 0; i <= n; ++i) {
    for (long long j = 0; j <= m; ++j) {
      if (!i or !j)
        dis[i][j] = max(i, j);
      else {
        dis[i][j] = min(
            {(a[i - 1] == b[j - 1] ? dis[i - 1][j - 1] : ((long long)1e9 + 1)),
             dis[i][j - 1] + 1, dis[i - 1][j] + 1});
      }
    }
  }
  vi res;
  long long i = n, j = m;
  while (i and j) {
    if (a[i - 1] == b[j - 1]) {
      res.push_back(a[i - 1]);
      i--;
      j--;
    } else if (dis[i - 1][j] <= dis[i][j - 1])
      i--;
    else
      j--;
  }
  reverse(res.begin(), res.end());
  return res;
}
# 4 "/home/tysm/dev/cpplib/bin/../include/cpplib/string/longest-commom-subsequence.hpp" 2

string lcs(const string &a, const string &b) {
  vi arr_a, arr_b;
  for (char c : a)
    arr_a.push_back(c);
  for (char c : b)
    arr_b.push_back(c);
  string res;
  for (char i : lcs(arr_a, arr_b))
    res += i;
  return res;
}
# 3 "f.cpp" 2

int32_t main() {
  (ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL));
  string a, b;
  cin >> a >> b;
  cout << lcs(a, b) << "\n";
  return 0;
}