Problem p03165 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <bits/stdc++.h>
#include <cmath>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<ull, ull> pullull;
typedef pair<ll, int> plli;
typedef pair<int, pii> pipii;
typedef vector<vector<int>> mati;
typedef vector<vector<double>> matd;
typedef vector<ll> vll;
typedef vector<vector<ll>> vvll;

#define rep(i, x, y) for (int i = (x); i < (y); ++i)

template <typename T> void vec_print(vector<T> VEC) {
  rep(i, 0, VEC.size()) { cout << VEC[i] << " "; }
  cout << "\n";
};

template <typename T>
void mat_print(vector<vector<T>> MAT){
    rep(i, 0, MAT.size()){rep(j, 0, MAT[i].size()){cout << MAT[i][j] << " ";
}
cout << "\n";
}
}
;

template <typename CLASS1, typename CLASS2> class HOGE {
public:
  CLASS1 key;
  CLASS2 value;
  HOGE(ll key, ll value) {
    this->key = index;
    this->value = value;
  };
  ~HOGE(void) { return; };

  void print(void) {
    cout << "key : " << key << ", value : " << value << "\n";
    return;
  };

  bool operator==(const HOGE &obj) { return (this->value == obj.value); };
  bool operator<(const HOGE &obj) { return (this->value < obj.value); };
  bool operator>(const HOGE &obj) { return (this->value > obj.value); };
};

constexpr int INF = (1 << 30);
constexpr ll INFLL = 1LL << 62;
constexpr long double EPS = 1e-12;

typedef pair<vector<int>, int> P;

/*
void long_search(const string& s, const string& t, const int st_start,
vector<string>& dp1, vector<string>& dp2){

  if(st_start+1 <= s.size() + t.size() + 2) long_search(s, t, st_start+1, dp1,
dp2);

  int hight = min(st_start+1, (int)s.size());
  for(int s_start=max(0, st_start-(int)t.size()+1); s_start<hight; ++s_start){
    int t_start = st_start-s_start;

    if(s[s_start] == t[t_start]){
      dp2[s_start] = s[s_start] + dp2[s_start+1];
    }else if(dp1[s_start].size() > dp1[s_start+1].size()){
      dp2[s_start] = dp1[s_start];
    }else{
      dp2[s_start] = dp1[s_start+1];
    }
    swap(dp1[s_start], dp2[s_start]);
  }
  swap(dp1[hight], dp2[hight]);

  return;
}
*/
void long_search2(const string &s, const string &t, const int s_start,
                  const int t_start, vector<vector<short int>> &dp) {

  if (s_start > s.size() || t_start > t.size())
    return;

  if (s_start == s.size() || t_start == t.size()) {
    dp[s_start][t_start] = 0;
    return;
  }

  if (s[s_start] == t[t_start]) {
    long_search2(s, t, s_start + 1, t_start + 1, dp);
    dp[s_start][t_start] = dp[s_start + 1][t_start + 1] + 1;
  } else {
    long_search2(s, t, s_start + 1, t_start, dp);
    long_search2(s, t, s_start, t_start + 1, dp);
    dp[s_start][t_start] =
        max(dp[s_start + 1][t_start], dp[s_start][t_start + 1]);
  }

  return;
}

int main() {
  cin.tie(0); // cut the cin and cout (default, std::flush is performed after
              // std::cin)
  ios::sync_with_stdio(
      false); // cut the iostream and stdio (DON'T endl; BUT "\n";)

  string s, t;
  cin >> s >> t;
  if (s.size() > t.size())
    swap(s, t);
  /*
  vector<string> dp1(s.size()+2, "");
  vector<string> dp2(s.size()+2, "");
  long_search(s, t, 0, dp1, dp2);
  */
  vector<vector<short int>> dp(s.size() + 1,
                               vector<short int>(t.size() + 1, -1));
  long_search2(s, t, 0, 0, dp);

  string ans = "";
  int s_start = 0, t_start = 0;
  while (true) {
    if (s_start >= s.size() || t_start >= t.size())
      break;
    if (s[s_start] == t[t_start]) {
      ans += s[s_start];
      s_start++;
      t_start++;
    } else if (dp[s_start + 1][t_start] > dp[s_start][t_start + 1]) {
      s_start++;
    } else {
      t_start++;
    }
  }

  cout << ans << "\n";

  return 0;
}


Fixed submission:
#include <algorithm>
#include <bits/stdc++.h>
#include <cmath>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<ull, ull> pullull;
typedef pair<ll, int> plli;
typedef pair<int, pii> pipii;
typedef vector<vector<int>> mati;
typedef vector<vector<double>> matd;
typedef vector<ll> vll;
typedef vector<vector<ll>> vvll;

#define rep(i, x, y) for (int i = (x); i < (y); ++i)

template <typename T> void vec_print(vector<T> VEC) {
  rep(i, 0, VEC.size()) { cout << VEC[i] << " "; }
  cout << "\n";
};

template <typename T>
void mat_print(vector<vector<T>> MAT){
    rep(i, 0, MAT.size()){rep(j, 0, MAT[i].size()){cout << MAT[i][j] << " ";
}
cout << "\n";
}
}
;

template <typename CLASS1, typename CLASS2> class HOGE {
public:
  CLASS1 key;
  CLASS2 value;
  HOGE(ll key, ll value) {
    this->key = index;
    this->value = value;
  };
  ~HOGE(void) { return; };

  void print(void) {
    cout << "key : " << key << ", value : " << value << "\n";
    return;
  };

  bool operator==(const HOGE &obj) { return (this->value == obj.value); };
  bool operator<(const HOGE &obj) { return (this->value < obj.value); };
  bool operator>(const HOGE &obj) { return (this->value > obj.value); };
};

constexpr int INF = (1 << 30);
constexpr ll INFLL = 1LL << 62;
constexpr long double EPS = 1e-12;

typedef pair<vector<int>, int> P;

/*
void long_search(const string& s, const string& t, const int st_start,
vector<string>& dp1, vector<string>& dp2){

  if(st_start+1 <= s.size() + t.size() + 2) long_search(s, t, st_start+1, dp1,
dp2);

  int hight = min(st_start+1, (int)s.size());
  for(int s_start=max(0, st_start-(int)t.size()+1); s_start<hight; ++s_start){
    int t_start = st_start-s_start;

    if(s[s_start] == t[t_start]){
      dp2[s_start] = s[s_start] + dp2[s_start+1];
    }else if(dp1[s_start].size() > dp1[s_start+1].size()){
      dp2[s_start] = dp1[s_start];
    }else{
      dp2[s_start] = dp1[s_start+1];
    }
    swap(dp1[s_start], dp2[s_start]);
  }
  swap(dp1[hight], dp2[hight]);

  return;
}
*/
void long_search2(const string &s, const string &t, const int s_start,
                  const int t_start, vector<vector<short int>> &dp) {

  if (s_start > s.size() || t_start > t.size())
    return;

  if (dp[s_start][t_start] >= 0)
    return;

  if (s_start == s.size() || t_start == t.size()) {
    dp[s_start][t_start] = 0;
    return;
  }

  if (s[s_start] == t[t_start]) {
    long_search2(s, t, s_start + 1, t_start + 1, dp);
    dp[s_start][t_start] = dp[s_start + 1][t_start + 1] + 1;
  } else {
    long_search2(s, t, s_start + 1, t_start, dp);
    long_search2(s, t, s_start, t_start + 1, dp);
    dp[s_start][t_start] =
        max(dp[s_start + 1][t_start], dp[s_start][t_start + 1]);
  }

  return;
}

int main() {
  cin.tie(0); // cut the cin and cout (default, std::flush is performed after
              // std::cin)
  ios::sync_with_stdio(
      false); // cut the iostream and stdio (DON'T endl; BUT "\n";)

  string s, t;
  cin >> s >> t;
  if (s.size() > t.size())
    swap(s, t);
  /*
  vector<string> dp1(s.size()+2, "");
  vector<string> dp2(s.size()+2, "");
  long_search(s, t, 0, dp1, dp2);
  */
  vector<vector<short int>> dp(s.size() + 1,
                               vector<short int>(t.size() + 1, -1));
  long_search2(s, t, 0, 0, dp);

  string ans = "";
  int s_start = 0, t_start = 0;
  while (true) {
    if (s_start >= s.size() || t_start >= t.size())
      break;
    if (s[s_start] == t[t_start]) {
      ans += s[s_start];
      s_start++;
      t_start++;
    } else if (dp[s_start + 1][t_start] > dp[s_start][t_start + 1]) {
      s_start++;
    } else {
      t_start++;
    }
  }

  cout << ans << "\n";

  return 0;
}