Problem p03165 (C++)

Buggy submission (status: Runtime Error):

// Target Expert

/*
 * Author    : raj1307 - Raj Singh
 * Institute : Jalpaiguri Government Engineering College
 * Date      : 3.08.19
 */

#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#define _CRT_SECURE_NO_WARNINGS
#include <algorithm>
#include <assert.h>
#include <complex>
#include <ctime>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <math.h>
#include <memory.h>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
#include <vector>
using namespace std;

// #define DEBUG
#ifdef DEBUG
#define deb(...)                                                               \
  cerr << "Line:" << __LINE__ << " ";                                          \
  __f(#__VA_ARGS__, __VA_ARGS__)
#define debarr(a, n)                                                           \
  cerr << #a << " : ";                                                         \
  for (int i = 0; i < n; i++)                                                  \
    cerr << a[i] << " ";                                                       \
  cerr << endl;
#define debmat(mat, row, col)                                                  \
  cerr << #mat << " :\n";                                                      \
  for (int i = 0; i < row; i++) {                                              \
    for (int j = 0; j < col; j++)                                              \
      cerr << mat[i][j] << " ";                                                \
    cerr << endl;                                                              \
  }
template <typename Arg1> void __f(const char *name, Arg1 &&arg1) {
  cerr << name << " : " << arg1 << endl;
}
template <typename Arg1, typename... Args>
void __f(const char *names, Arg1 &&arg1, Args &&...args) {
  const char *comma = strchr(names + 1, ',');
  cerr.write(names, comma - names) << " : " << arg1 << " | ";
  __f(comma + 1, args...);
}

#else
#define deb(...)                                                               \
  {}
#define debarr(a, n)                                                           \
  {}
#define debmat(mat, row, col)                                                  \
  {}
#define endl "\n"
#endif

#define fio                                                                    \
  ios::sync_with_stdio(0);                                                     \
  cin.tie(0);                                                                  \
  cout.tie(0)
#define fr(i, a, b) for (int i = (a); i < (b); ++i)
#define fb(i, b, a) for (int i = (b); i > (a); --i)
#define rep(i, a, b) for (int i = (a); i <= (b); ++i)
#define rr return
#define int long long
#define pb push_back
#define sz(x) ((int)x.size())
int max(int a, int b) {
  if (a > b)
    return a;
  else
    return b;
}
int min(int a, int b) {
  if (a > b)
    return b;
  else
    return a;
}

const int mod = 1000 * 1000 * 1000 + 7;
const int INF = 1e18 + 5;

int powm(int a, int b) {
  int res = 1;
  while (b) {
    if (b & 1)
      res = (res * a) % mod;
    a = (a * a) % mod;
    b >>= 1;
  }
  return res;
}

const int N = 5e5 + 5;
int n, W;

int cache[3005][3005];
string a, b, output = "";

int dp(int x, int y) {

  if (x < 0 or y < 0)
    return 0;

  else if (cache[x][y] != -1)
    return cache[x][y];

  else if (a[x] == b[y])
    return 1 + dp(x - 1, y - 1);

  else {

    cache[x][y] = max(dp(x - 1, y), dp(x, y - 1));
    return cache[x][y];
  }
}

void path(int x, int y) {

  if (x < 0 or y < 0)
    return;

  int ans = dp(x, y);

  if (a[x] == b[y]) {
    output += a[x];
    return path(x - 1, y - 1);
  } else {

    if (ans == dp(x - 1, y))
      return path(x - 1, y);
    return path(x, y - 1);
  }
}

void solve() {

  cin >> a >> b;

  memset(cache, -1, sizeof(cache));

  dp(sz(a) - 1, sz(b) - 1);
  path(sz(a) - 1, sz(b) - 1);

  reverse(output.begin(), output.end());
  ;
  cout << output;
}

signed main() {
  fio;
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
  freopen("error.txt", "w", stderr);
#endif
  cout << fixed << setprecision(10);
  int t = 1;
  //	cin>>t;
  while (t--) {
    solve();
  }

  return 0;
}


Fixed submission:

// Target Expert

/*
 * Author    : raj1307 - Raj Singh
 * Institute : Jalpaiguri Government Engineering College
 * Date      : 3.08.19
 */

#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#define _CRT_SECURE_NO_WARNINGS
#include <algorithm>
#include <assert.h>
#include <complex>
#include <ctime>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <math.h>
#include <memory.h>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
#include <vector>
using namespace std;

// #define DEBUG
#ifdef DEBUG
#define deb(...)                                                               \
  cerr << "Line:" << __LINE__ << " ";                                          \
  __f(#__VA_ARGS__, __VA_ARGS__)
#define debarr(a, n)                                                           \
  cerr << #a << " : ";                                                         \
  for (int i = 0; i < n; i++)                                                  \
    cerr << a[i] << " ";                                                       \
  cerr << endl;
#define debmat(mat, row, col)                                                  \
  cerr << #mat << " :\n";                                                      \
  for (int i = 0; i < row; i++) {                                              \
    for (int j = 0; j < col; j++)                                              \
      cerr << mat[i][j] << " ";                                                \
    cerr << endl;                                                              \
  }
template <typename Arg1> void __f(const char *name, Arg1 &&arg1) {
  cerr << name << " : " << arg1 << endl;
}
template <typename Arg1, typename... Args>
void __f(const char *names, Arg1 &&arg1, Args &&...args) {
  const char *comma = strchr(names + 1, ',');
  cerr.write(names, comma - names) << " : " << arg1 << " | ";
  __f(comma + 1, args...);
}

#else
#define deb(...)                                                               \
  {}
#define debarr(a, n)                                                           \
  {}
#define debmat(mat, row, col)                                                  \
  {}
#define endl "\n"
#endif

#define fio                                                                    \
  ios::sync_with_stdio(0);                                                     \
  cin.tie(0);                                                                  \
  cout.tie(0)
#define fr(i, a, b) for (int i = (a); i < (b); ++i)
#define fb(i, b, a) for (int i = (b); i > (a); --i)
#define rep(i, a, b) for (int i = (a); i <= (b); ++i)
#define rr return
#define int long long
#define pb push_back
#define sz(x) ((int)x.size())
int max(int a, int b) {
  if (a > b)
    return a;
  else
    return b;
}
int min(int a, int b) {
  if (a > b)
    return b;
  else
    return a;
}

const int mod = 1000 * 1000 * 1000 + 7;
const int INF = 1e18 + 5;

int powm(int a, int b) {
  int res = 1;
  while (b) {
    if (b & 1)
      res = (res * a) % mod;
    a = (a * a) % mod;
    b >>= 1;
  }
  return res;
}

const int N = 5e5 + 5;
int n, W;

int cache[3005][3005];
string a, b, output = "";

int dp(int x, int y) {

  if (x < 0 or y < 0)
    return 0;

  else if (cache[x][y] != -1)
    return cache[x][y];

  else if (a[x] == b[y])
    return 1 + dp(x - 1, y - 1);

  else {

    cache[x][y] = max(dp(x - 1, y), dp(x, y - 1));
    return cache[x][y];
  }
}

void path(int x, int y) {

  if (x < 0 or y < 0)
    return;

  int ans = dp(x, y);

  if (a[x] == b[y]) {
    output += a[x];
    return path(x - 1, y - 1);
  } else {

    if (ans == dp(x - 1, y))
      return path(x - 1, y);
    return path(x, y - 1);
  }
}

void solve() {

  cin >> a >> b;

  memset(cache, -1, sizeof(cache));

  dp(sz(a) - 1, sz(b) - 1);
  path(sz(a) - 1, sz(b) - 1);

  reverse(output.begin(), output.end());
  ;
  cout << output;
}

signed main() {
  fio;

  cout << fixed << setprecision(10);
  int t = 1;
  //	cin>>t;
  while (t--) {
    solve();
  }

  return 0;
}