Problem p03165 (C++)

Buggy submission (status: Runtime Error):
/*==========================================================================================*\
**                        _           _ _   _     _  _         _ **
**                       | |__  _   _/ | |_| |__ | || |  _ __ | |__ **
**                       | '_ \| | | | | __| '_ \| || |_| '_ \| '_ \ **
**                       | |_) | |_| | | |_| | | |__   _| | | | | | | **
**                       |_.__/ \__,_|_|\__|_| |_|  |_| |_| |_|_| |_| **
\*==========================================================================================*/
//=====================================
// Solution Briefing - Foreword

//=====================================
// Libraries and namespaces
// #include <bits/stdc++.h>
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <tuple>
#include <utility>
#include <vector>

#if __cplusplus >= 201103L
#include <chrono>
#include <random>
#include <unordered_map>
#include <unordered_set>
#endif // __cplusplus

using namespace std;

// #define DEBUG
#define OPTIONAL_FEATURE

//=====================================
// Macroes
#define sp ' '
#define el '\n'
#define task ""
#define maxinp ()
#define fi first
#define se second
#define pb push_back
#define whole(x) x.begin(), x.end()
#define whole_1(x) x.begin() + 1, x.end()
#define r_whole(x) x.rbegin(), x.rend()
#define FOR(i, x, y) for (auto i = x; i <= y; ++i)
#define FORl(i, x, y) for (auto i = x; i < y; ++i)
#define FORb(i, x, y) for (auto i = x; i >= y; --i)
#define FORlb(i, x, y) for (auto i = x; i > y; --i)
#define MEMS(x, val) memset(x, val, sizeof(x))
#define what_is(x) cerr << #x << " is " << x << endl;
#define FILEOP()                                                               \
  {                                                                            \
    freopen(task ".inp", "r", stdin);                                          \
    freopen(task ".out", "w", stdout);                                         \
  }
#define FILEOP_DEBUG()                                                         \
  {                                                                            \
    freopen(task ".inp", "r", stdin);                                          \
    freopen(task ".out", "w", stdout);                                         \
    freopen(task ".err", "w", stderr);                                         \
  }

// Macroes - Optional
#ifdef OPTIONAL_FEATURE
#define pc(x) putchar(x)
#define gc() getchar()
#endif

//=====================================
// Auxilary Functions and Fast I/O
#ifdef OPTIONAL_FEATURE
template <class T, class R> T max(const T &__X, const R &__Y) {
  return __X > __Y ? __X : __Y;
}
template <class T, class R> T min(const T &__X, const R &__Y) {
  return __X < __Y ? __X : __Y;
}
template <class T, class R> void maximize(T &__X, R __Y) {
  __X = __X > __Y ? __X : __Y;
}
template <class T, class R> void minimize(T &__X, R __Y) {
  __X = __X < __Y ? __X : __Y;
}
template <class T> int getBit(T &__X, int __i) {
  return ((__X >> __i) & 1) == 1;
}
template <class T> bool inRange(T __L, T __R, T __X) {
  return __L <= __X && __X <= __R;
}
template <class T> T __abs(T __X) { return (__X < 0) ? -__X : __X; }
#endif
// Fast I/O
template <class T> inline void scan(T &__ret) {
  __ret = T();
  char c = 0;
  bool neg = 0;

  while (isdigit(c) == 0 && c != '-')
    c = getchar();
  if (c == '-') {
    neg = 1;
    c = getchar();
  }

  for (; isdigit(c) != 0; c = getchar())
    __ret = __ret * 10 + c - '0';
  __ret = (neg) ? -__ret : __ret;
}
template <class T> void print(T __X) {
  if (__X < 0) {
    putchar('-');
    __X *= -1;
  }

  if (__X > 9)
    print(__X / 10);
  putchar(__X % 10 + '0');
}

//=====================================
// Constants

//=====================================
// Typedefs
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> ii;
typedef vector<bool> vb;
typedef vector<int> vi;
typedef vector<ii> vii;
typedef vector<vi> vvi;
typedef vector<vb> vvb;
typedef vector<vii> vvii;
string s, t, res;
int m, n;
vvi dp;

//=====================================
// Functions and procedures
// File I/O and utilities
void FileInit() { FILEOP() }
void FileDebug() {
#ifdef DEBUG
  FILEOP_DEBUG()
#else
  FILEOP()
#endif
}
void FileClose() {
  fclose(stdin);
  fclose(stdout);
}

// Enter
void Enter() {
  cin >> s >> t;
  s = '$' + s;
  t = '#' + t;

  n = s.size() - 1;
  m = t.size() - 1;

  dp = vvi(n + 2, vi(n + 2, 0));
}

// Check
void Solve() {
  FOR(i, 1, n) {
    FOR(j, 1, m) {
      maximize(dp[i][j], max(dp[i - 1][j], dp[i][j - 1]));
      if (s[i] == t[j])
        maximize(dp[i][j], dp[i - 1][j - 1] + 1);
    }
  }
  int i = n, j = m;
  while (i > 0 && j > 0) {
    if (s[i] == t[j])
      res = s[i] + res, --j, --i;
    else if (dp[i][j] == dp[i][j - 1])
      --j;
    else
      --i;
  }
  cout << res << el;
}

// Main Procedure
int main() {
  Enter();
  Solve();
  return 0;
}

//=============================================================================//
/**    CTB, you are always in my heart and in my code <3       #30yearsCTB **/
//=============================================================================//


Fixed submission:
/*==========================================================================================*\
**                        _           _ _   _     _  _         _ **
**                       | |__  _   _/ | |_| |__ | || |  _ __ | |__ **
**                       | '_ \| | | | | __| '_ \| || |_| '_ \| '_ \ **
**                       | |_) | |_| | | |_| | | |__   _| | | | | | | **
**                       |_.__/ \__,_|_|\__|_| |_|  |_| |_| |_|_| |_| **
\*==========================================================================================*/
//=====================================
// Solution Briefing - Foreword

//=====================================
// Libraries and namespaces
// #include <bits/stdc++.h>
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <tuple>
#include <utility>
#include <vector>

#if __cplusplus >= 201103L
#include <chrono>
#include <random>
#include <unordered_map>
#include <unordered_set>
#endif // __cplusplus

using namespace std;

// #define DEBUG
#define OPTIONAL_FEATURE

//=====================================
// Macroes
#define sp ' '
#define el '\n'
#define task ""
#define maxinp ()
#define fi first
#define se second
#define pb push_back
#define whole(x) x.begin(), x.end()
#define whole_1(x) x.begin() + 1, x.end()
#define r_whole(x) x.rbegin(), x.rend()
#define FOR(i, x, y) for (auto i = x; i <= y; ++i)
#define FORl(i, x, y) for (auto i = x; i < y; ++i)
#define FORb(i, x, y) for (auto i = x; i >= y; --i)
#define FORlb(i, x, y) for (auto i = x; i > y; --i)
#define MEMS(x, val) memset(x, val, sizeof(x))
#define what_is(x) cerr << #x << " is " << x << endl;
#define FILEOP()                                                               \
  {                                                                            \
    freopen(task ".inp", "r", stdin);                                          \
    freopen(task ".out", "w", stdout);                                         \
  }
#define FILEOP_DEBUG()                                                         \
  {                                                                            \
    freopen(task ".inp", "r", stdin);                                          \
    freopen(task ".out", "w", stdout);                                         \
    freopen(task ".err", "w", stderr);                                         \
  }

// Macroes - Optional
#ifdef OPTIONAL_FEATURE
#define pc(x) putchar(x)
#define gc() getchar()
#endif

//=====================================
// Auxilary Functions and Fast I/O
#ifdef OPTIONAL_FEATURE
template <class T, class R> T max(const T &__X, const R &__Y) {
  return __X > __Y ? __X : __Y;
}
template <class T, class R> T min(const T &__X, const R &__Y) {
  return __X < __Y ? __X : __Y;
}
template <class T, class R> void maximize(T &__X, R __Y) {
  __X = __X > __Y ? __X : __Y;
}
template <class T, class R> void minimize(T &__X, R __Y) {
  __X = __X < __Y ? __X : __Y;
}
template <class T> int getBit(T &__X, int __i) {
  return ((__X >> __i) & 1) == 1;
}
template <class T> bool inRange(T __L, T __R, T __X) {
  return __L <= __X && __X <= __R;
}
template <class T> T __abs(T __X) { return (__X < 0) ? -__X : __X; }
#endif
// Fast I/O
template <class T> inline void scan(T &__ret) {
  __ret = T();
  char c = 0;
  bool neg = 0;

  while (isdigit(c) == 0 && c != '-')
    c = getchar();
  if (c == '-') {
    neg = 1;
    c = getchar();
  }

  for (; isdigit(c) != 0; c = getchar())
    __ret = __ret * 10 + c - '0';
  __ret = (neg) ? -__ret : __ret;
}
template <class T> void print(T __X) {
  if (__X < 0) {
    putchar('-');
    __X *= -1;
  }

  if (__X > 9)
    print(__X / 10);
  putchar(__X % 10 + '0');
}

//=====================================
// Constants

//=====================================
// Typedefs
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> ii;
typedef vector<bool> vb;
typedef vector<int> vi;
typedef vector<ii> vii;
typedef vector<vi> vvi;
typedef vector<vb> vvb;
typedef vector<vii> vvii;
string s, t, res;
int m, n;
vvi dp;

//=====================================
// Functions and procedures
// File I/O and utilities
void FileInit() { FILEOP() }
void FileDebug() {
#ifdef DEBUG
  FILEOP_DEBUG()
#else
  FILEOP()
#endif
}
void FileClose() {
  fclose(stdin);
  fclose(stdout);
}

// Enter
void Enter() {
  cin >> s >> t;
  s = '$' + s;
  t = '#' + t;

  n = s.size() - 1;
  m = t.size() - 1;

  dp = vvi(n + 2, vi(m + 2, 0));
}

// Check
void Solve() {
  FOR(i, 1, n) {
    FOR(j, 1, m) {
      maximize(dp[i][j], max(dp[i - 1][j], dp[i][j - 1]));
      if (s[i] == t[j])
        maximize(dp[i][j], dp[i - 1][j - 1] + 1);
    }
  }
  int i = n, j = m;
  while (i > 0 && j > 0) {
    if (s[i] == t[j])
      res = s[i] + res, --j, --i;
    else if (dp[i][j] == dp[i][j - 1])
      --j;
    else
      --i;
  }
  cout << res << el;
}

// Main Procedure
int main() {
  Enter();
  Solve();
  return 0;
}

//=============================================================================//
/**    CTB, you are always in my heart and in my code <3       #30yearsCTB **/
//=============================================================================//