Problem p03165 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using ll = long long;
using graph = std::vector<std::vector<ll>>;
using wGraph = std::vector<std::vector<std::pair<ll, ll>>>;
#define rep(i, n) for (int i = 0; i < int(n); i++)

using namespace std;
vector<ll> InputVec(ll N) {
  vector<ll> A(N);
  for (int i = 0; i < N; ++i) {
    cin >> A[i];
  }
  return A;
}
void OutputVec(vector<ll> A) {
  for (int i = 0; i < A.size(); ++i) {
    cout << A[i] << ",";
  }
  cout << endl;
}
vector<vector<ll>> InputVec2d(ll H, ll W) {
  vector<vector<ll>> A(H);
  for (int yi = 0; yi < H; ++yi) {
    A[yi] = vector<ll>(W);
  }
  for (int yi = 0; yi < H; ++yi) {
    for (int xi = 0; xi < W; ++xi) {
      cin >> A[yi][xi];
    }
  }
}

void OutputVec2d(vector<vector<ll>> A) {
  for (int yi = 0; yi < A.size(); ++yi) {
    for (int xi = 0; xi < A[yi].size(); ++xi) {
      cout << A[yi][xi] << ",";
    }
    cout << endl;
  }
}

int main() {
  string s, t;
  cin >> s >> t;
  ll S = s.size();
  ll T = t.size();
  vector<vector<ll>> memo(S);
  for (int i = 0; i < S; ++i) {
    memo[i] = vector<ll>(T);
  }
  for (int yi = 0; yi < S; ++yi) {
    for (int xi = 0; xi < T; ++xi) {
      memo[yi][xi] = -1;
    }
  }

  bool judge = false;
  for (int yi = 0; yi < S; ++yi) {
    if (s[yi] == t[0] || judge) {
      memo[yi][0] = 1;
      judge = true;
    } else {
      memo[yi][0] = 0;
    }
  }
  judge = false;
  for (int xi = 1; xi < T; ++xi) {
    if (s[0] == t[xi] || judge) {
      memo[0][xi] = 1;
      judge = true;
    } else {
      memo[0][xi] = 0;
    }
  }
  for (int yi = 1; yi < S; ++yi) {
    for (int xi = 1; xi < T; ++xi) {
      if (s[yi] == t[xi]) {
        memo[yi][xi] = memo[yi - 1][xi - 1] + 1;
      } else {
        memo[yi][xi] = max(memo[yi - 1][xi], memo[yi][xi - 1]);
      }
    }
  }
  ll x, y;
  y = S - 1;
  x = T - 1;
  stack<char> stack;
  if (memo[S - 1][T - 1] == 0) {
    cout << "" << endl;
    return 0;
  }
  while (memo[y][x] > 0) {
    if (y != 0 && memo[y][x] == memo[y - 1][x]) {
      y--;
      continue;
    }
    if (x != 0 && memo[y][x] == memo[y][x - 1]) {
      x--;
      continue;
    }
    stack.push(s[y]);
    y--;
    x--;
  }
  // OutputVec2d(memo);
  while (!stack.empty()) {
    cout << stack.top();
    stack.pop();
  }
  cout << endl;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using ll = long long;
using graph = std::vector<std::vector<ll>>;
using wGraph = std::vector<std::vector<std::pair<ll, ll>>>;
#define rep(i, n) for (int i = 0; i < int(n); i++)

using namespace std;
vector<ll> InputVec(ll N) {
  vector<ll> A(N);
  for (int i = 0; i < N; ++i) {
    cin >> A[i];
  }
  return A;
}
void OutputVec(vector<ll> A) {
  for (int i = 0; i < A.size(); ++i) {
    cout << A[i] << ",";
  }
  cout << endl;
}
vector<vector<ll>> InputVec2d(ll H, ll W) {
  vector<vector<ll>> A(H);
  for (int yi = 0; yi < H; ++yi) {
    A[yi] = vector<ll>(W);
  }
  for (int yi = 0; yi < H; ++yi) {
    for (int xi = 0; xi < W; ++xi) {
      cin >> A[yi][xi];
    }
  }
}

void OutputVec2d(vector<vector<ll>> A) {
  for (int yi = 0; yi < A.size(); ++yi) {
    for (int xi = 0; xi < A[yi].size(); ++xi) {
      cout << A[yi][xi] << ",";
    }
    cout << endl;
  }
}

int main() {
  string s, t;
  cin >> s >> t;
  ll S = s.size();
  ll T = t.size();
  vector<vector<ll>> memo(S);
  for (int i = 0; i < S; ++i) {
    memo[i] = vector<ll>(T);
  }
  for (int yi = 0; yi < S; ++yi) {
    for (int xi = 0; xi < T; ++xi) {
      memo[yi][xi] = -1;
    }
  }

  bool judge = false;
  for (int yi = 0; yi < S; ++yi) {
    if (s[yi] == t[0] || judge) {
      memo[yi][0] = 1;
      judge = true;
    } else {
      memo[yi][0] = 0;
    }
  }
  judge = false;
  for (int xi = 1; xi < T; ++xi) {
    if (s[0] == t[xi] || judge) {
      memo[0][xi] = 1;
      judge = true;
    } else {
      memo[0][xi] = 0;
    }
  }
  for (int yi = 1; yi < S; ++yi) {
    for (int xi = 1; xi < T; ++xi) {
      if (s[yi] == t[xi]) {
        memo[yi][xi] = memo[yi - 1][xi - 1] + 1;
      } else {
        memo[yi][xi] = max(memo[yi - 1][xi], memo[yi][xi - 1]);
      }
    }
  }
  ll x, y;
  y = S - 1;
  x = T - 1;
  stack<char> stack;
  if (memo[S - 1][T - 1] == 0) {
    cout << "" << endl;
    return 0;
  }
  while ((y >= 0 && x >= 0) && memo[y][x] > 0) {
    if (y != 0 && memo[y][x] == memo[y - 1][x]) {
      y--;
      continue;
    }
    if (x != 0 && memo[y][x] == memo[y][x - 1]) {
      x--;
      continue;
    }
    stack.push(s[y]);
    y--;
    x--;
  }
  // OutputVec2d(memo);
  while (!stack.empty()) {
    cout << stack.top();
    stack.pop();
  }
  cout << endl;
}