Problem p03165 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

using namespace std;

#define debug(n) cerr << #n << ':' << n << endl;
#define dline cerr << __LINE__ << endl;

using ll = long long;
template <class T, class U> using P = pair<T, U>;
template <class T> using Heap = priority_queue<T>;
template <class T> using heaP = priority_queue<T, vector<T>, greater<T>>;
template <class T, class U> using umap = unordered_map<T, U>;
template <class T> using uset = unordered_set<T>;

template <class T> bool ChangeMax(T &a, const T &b) {
  if (a >= b)
    return false;
  a = b;
  return true;
}

template <class T> bool ChangeMin(T &a, const T &b) {
  if (a <= b)
    return false;
  a = b;
  return true;
}

template <class T, size_t N, class U> void Fill(T (&a)[N], const U &v) {
  fill((U *)a, (U *)(a + N), v);
}

template <class T> istream &operator>>(istream &is, vector<T> &v) {
  for (auto &e : v)
    is >> e;
  return is;
}

int main() {
  string s, t;
  cin >> s >> t;
  const int slen = (int)s.size(), tlen = (int)t.size();
  vector<vector<int>> dp(slen + 1, vector<int>(tlen + 1, 0));
  for (int i = 1; i <= slen; ++i) {
    for (int j = 1; j <= tlen; ++j) {
      dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]);
      if (s[i - 1] == t[j - 1]) {
        ChangeMax(dp[i][j], dp[i - 1][j - 1] + 1);
      }
    }
  }
  string ans;
  int spos = slen, tpos = tlen;
  while (spos > 0 || tpos > 0) {
    if (s[spos - 1] == t[tpos - 1]) {
      ans += s[spos - 1];
      spos--;
      tpos--;
    } else {
      if (dp[spos - 1][tpos] > dp[spos][tpos - 1])
        spos--;
      else
        tpos--;
    }
  }
  if (ans.size() > 0)
    reverse(ans.begin(), ans.end());
  cout << ans << endl;
  return 0;
}

// int main(){
//   string s,t; cin >> s >> t;
//   static string mp[3001][3001] = {};
//   int slen = (int)s.size(), tlen = (int)t.size();
//   vector<vector<int>> dp(slen+1,vector<int>(tlen+1,0));
//   for(int i = 1; i <= slen; ++i){
//     for(int j = 1; j <= tlen; ++j){
//       dp[i][j] = max(dp[i-1][j],dp[i][j-1]);
//       mp[i][j] = (dp[i-1][j] < dp[i][j-1] ? mp[i][j-1] : mp[i-1][j]);
//       if(s[i-1] == t[j-1]){
// 	if(ChangeMax(dp[i][j],dp[i-1][j-1] + 1)) mp[i][j] = mp[i-1][j-1] +
// s[i-1];
//       }
//       mp[i-1][j-1]="";
//       mp[i-1][j-1].shrink_to_fit();
//     }
//   }
//   cout << mp[slen][tlen] << endl;
//   return 0;
// }


Fixed submission:
#include <bits/stdc++.h>

using namespace std;

#define debug(n) cerr << #n << ':' << n << endl;
#define dline cerr << __LINE__ << endl;

using ll = long long;
template <class T, class U> using P = pair<T, U>;
template <class T> using Heap = priority_queue<T>;
template <class T> using heaP = priority_queue<T, vector<T>, greater<T>>;
template <class T, class U> using umap = unordered_map<T, U>;
template <class T> using uset = unordered_set<T>;

template <class T> bool ChangeMax(T &a, const T &b) {
  if (a >= b)
    return false;
  a = b;
  return true;
}

template <class T> bool ChangeMin(T &a, const T &b) {
  if (a <= b)
    return false;
  a = b;
  return true;
}

template <class T, size_t N, class U> void Fill(T (&a)[N], const U &v) {
  fill((U *)a, (U *)(a + N), v);
}

template <class T> istream &operator>>(istream &is, vector<T> &v) {
  for (auto &e : v)
    is >> e;
  return is;
}

int main() {
  string s, t;
  cin >> s >> t;
  const int slen = (int)s.size(), tlen = (int)t.size();
  vector<vector<int>> dp(slen + 1, vector<int>(tlen + 1, 0));
  for (int i = 1; i <= slen; ++i) {
    for (int j = 1; j <= tlen; ++j) {
      dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]);
      if (s[i - 1] == t[j - 1]) {
        ChangeMax(dp[i][j], dp[i - 1][j - 1] + 1);
      }
    }
  }
  string ans;
  int spos = slen, tpos = tlen;
  while (spos > 0 && tpos > 0) {
    if (s[spos - 1] == t[tpos - 1]) {
      ans += s[spos - 1];
      spos--;
      tpos--;
    } else {
      if (dp[spos - 1][tpos] > dp[spos][tpos - 1])
        spos--;
      else
        tpos--;
    }
  }
  if (ans.size() > 0)
    reverse(ans.begin(), ans.end());
  cout << ans << endl;
  return 0;
}

// int main(){
//   string s,t; cin >> s >> t;
//   static string mp[3001][3001] = {};
//   int slen = (int)s.size(), tlen = (int)t.size();
//   vector<vector<int>> dp(slen+1,vector<int>(tlen+1,0));
//   for(int i = 1; i <= slen; ++i){
//     for(int j = 1; j <= tlen; ++j){
//       dp[i][j] = max(dp[i-1][j],dp[i][j-1]);
//       mp[i][j] = (dp[i-1][j] < dp[i][j-1] ? mp[i][j-1] : mp[i-1][j]);
//       if(s[i-1] == t[j-1]){
// 	if(ChangeMax(dp[i][j],dp[i-1][j-1] + 1)) mp[i][j] = mp[i-1][j-1] +
// s[i-1];
//       }
//       mp[i-1][j-1]="";
//       mp[i-1][j-1].shrink_to_fit();
//     }
//   }
//   cout << mp[slen][tlen] << endl;
//   return 0;
// }