prithivMLmods/Coder-Stat huggingface dataset:
Problem p03165 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define all(x) x.begin(), x.end()
#define fi first
#define se second
#define endl '\n'
const int mod = 1e9 + 7, u = 1e5 + 5;

signed main() {
  ios::sync_with_stdio(0);
  cin.tie(0);

  string s, t;
  cin >> s >> t;
  int n = s.size(), m = t.size();

  int dp[n + 1][m + 1];
  for (int i = 0; i <= n; i++) {
    dp[i][0] = 0;
  }
  for (int i = 0; i <= m; i++) {
    dp[0][i] = 0;
  }

  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      if (s[i - 1] == t[j - 1]) {
        dp[i][j] = dp[i - 1][j - 1] + 1;
      } else {
        dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]);
      }
    }
  }

  int x = n, y = m;
  string ans;
  while (n > 0 and y > 0) {
    if (s[x - 1] == t[y - 1]) {
      ans += s[x - 1];
      x--;
      y--;
    } else if (dp[x][y] == dp[x - 1][y]) {
      x--;
    } else {
      y--;
    }
  }
  reverse(all(ans));
  cout << ans;

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define all(x) x.begin(), x.end()
#define fi first
#define se second
#define endl '\n'
const int mod = 1e9 + 7, u = 1e5 + 5;

signed main() {
  ios::sync_with_stdio(0);
  cin.tie(0);

  string s, t;
  cin >> s >> t;
  int n = s.size(), m = t.size();

  int dp[n + 1][m + 1];
  for (int i = 0; i <= n; i++) {
    dp[i][0] = 0;
  }
  for (int i = 0; i <= m; i++) {
    dp[0][i] = 0;
  }

  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      if (s[i - 1] == t[j - 1]) {
        dp[i][j] = dp[i - 1][j - 1] + 1;
      } else {
        dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]);
      }
    }
  }

  int x = n, y = m;
  string ans;
  while (x > 0 and y > 0) {
    if (s[x - 1] == t[y - 1]) {
      ans += s[x - 1];
      x--;
      y--;
    } else if (dp[x][y] == dp[x - 1][y]) {
      x--;
    } else {
      y--;
    }
  }
  reverse(all(ans));
  cout << ans;

  return 0;
}
Problem p03165 (C++)

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

#define pb push_back
#define f(i, n) for (int i = 0; i < n; i++)
#define fast                                                                   \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0);                                                                  \
  cout.tie(0)

const int N = 3005;

int dp[N][N], n, m;
string s, t;

void solve() {
  cin >> s >> t;

  n = s.length();
  m = t.length();

  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++)
      if (s[i - 1] == t[j - 1])
        dp[i][j] = dp[i - 1][j - 1] + 1;
      else
        dp[i][j] = max(dp[i][j - 1], dp[i - 1][j]);

  int nn = n;
  int mm = m;

  vector<char> v;

  while (nn >= 0 && mm >= 0) {
    if (s[nn - 1] == t[mm - 1]) {
      v.pb(s[nn - 1]);
      nn--;
      mm--;
    } else if (dp[nn][mm - 1] > dp[nn - 1][mm])
      mm--;
    else
      nn--;
  }

  reverse(v.begin(), v.end());

  for (auto z : v)
    cout << z;
}

signed main() {
  fast;

  int t = 1;

  //    cin >> t;

  while (t--)

    solve();
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;

#define pb push_back
#define f(i, n) for (int i = 0; i < n; i++)
#define fast                                                                   \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0);                                                                  \
  cout.tie(0)

const int N = 3005;

int dp[N][N], n, m;
string s, t;

void solve() {
  cin >> s >> t;

  n = s.length();
  m = t.length();

  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++)
      if (s[i - 1] == t[j - 1])
        dp[i][j] = dp[i - 1][j - 1] + 1;
      else
        dp[i][j] = max(dp[i][j - 1], dp[i - 1][j]);

  int nn = n;
  int mm = m;

  vector<char> v;

  while (nn >= 1 && mm >= 1) {
    if (s[nn - 1] == t[mm - 1]) {
      v.pb(s[nn - 1]);
      nn--;
      mm--;
    } else if (dp[nn][mm - 1] > dp[nn - 1][mm])
      mm--;
    else
      nn--;
  }

  reverse(v.begin(), v.end());

  for (auto z : v)
    cout << z;
}

signed main() {
  fast;

  int t = 1;

  //    cin >> t;

  while (t--)

    solve();
}
Problem p03165 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#include <string>
#include <utility>
using namespace std;
typedef pair<int, int> P;
typedef pair<int, P> PP;

int main() {
  string s, t;
  cin >> s >> t;
  int n = s.size(), m = t.size();
  PP dp[100][100];
  fill(dp[0], dp[0] + 100, PP(0, P(-1, -1)));
  for (int i = 0; i < 100; i++) {
    dp[i][0] = PP(0, P(-1, -1));
  }
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      if (s[i - 1] == t[j - 1])
        dp[i][j] = PP(dp[i - 1][j - 1].first + 1, P(i - 1, j - 1));
      else {
        if (dp[i - 1][j].first >= dp[i][j - 1].first)
          dp[i][j] = dp[i - 1][j];
        else
          dp[i][j] = dp[i][j - 1];
      }
    }
  }
  P p = dp[n][m].second;
  char c[3002];
  while (p.first >= 0 && p.second >= 0) {
    c[dp[p.first][p.second].first] = s[p.first];
    p = dp[p.first][p.second].second;
  }
  for (int i = 0; i < dp[n][m].first; i++)
    cout << c[i];
  cout << endl;
}

Fixed submission:
#include <iostream>
#include <string>
#include <utility>
using namespace std;
typedef pair<int, int> P;
typedef pair<int, P> PP;

int main() {
  string s, t;
  cin >> s >> t;
  int n = s.size(), m = t.size();
  PP dp[3002][3002];
  fill(dp[0], dp[0] + 3002, PP(0, P(-1, -1)));
  for (int i = 0; i < 3002; i++) {
    dp[i][0] = PP(0, P(-1, -1));
  }
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      if (s[i - 1] == t[j - 1])
        dp[i][j] = PP(dp[i - 1][j - 1].first + 1, P(i - 1, j - 1));
      else {
        if (dp[i - 1][j].first >= dp[i][j - 1].first)
          dp[i][j] = dp[i - 1][j];
        else
          dp[i][j] = dp[i][j - 1];
      }
    }
  }
  P p = dp[n][m].second;
  char c[3002];
  while (p.first >= 0 && p.second >= 0) {
    c[dp[p.first][p.second].first] = s[p.first];
    p = dp[p.first][p.second].second;
  }
  for (int i = 0; i < dp[n][m].first; i++)
    cout << c[i];
  cout << endl;
}
Problem p03165 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
string str1, str2;
int dp[1000][1000], n, m;

int solve(int id1, int id2) {
  if (id1 == n || id2 == m)
    return 0;
  if (dp[id1][id2] != -1)
    return dp[id1][id2];
  int ans = 0;
  if (str1[id1] == str2[id2])
    ans = max(ans, 1 + solve(id1 + 1, id2 + 1));
  else
    ans = max(solve(id1 + 1, id2), solve(id1, id2 + 1));
  return dp[id1][id2] = ans;
}

string printLCS() {
  int id1 = 0, id2 = 0;
  string str = "";
  while (id1 < n && id2 < m) {
    if (str1[id1] == str2[id2]) {
      str += str1[id1];
      id1++;
      id2++;
    } else if (dp[id1 + 1][id2] > dp[id1][id2 + 1])
      id1++;
    else
      id2++;
  }
  return str;
}

int main() {
  cin >> str1 >> str2;
  n = str1.size();
  m = str2.size();
  memset(dp, -1, sizeof dp);
  solve(0, 0);
  cout << printLCS() << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
string str1, str2;
int dp[5000][5000], n, m;

int solve(int id1, int id2) {
  if (id1 == n || id2 == m)
    return 0;
  if (dp[id1][id2] != -1)
    return dp[id1][id2];
  int ans = 0;
  if (str1[id1] == str2[id2])
    ans = max(ans, 1 + solve(id1 + 1, id2 + 1));
  else
    ans = max(solve(id1 + 1, id2), solve(id1, id2 + 1));
  return dp[id1][id2] = ans;
}

string printLCS() {
  int id1 = 0, id2 = 0;
  string str = "";
  while (id1 < n && id2 < m) {
    if (str1[id1] == str2[id2]) {
      str += str1[id1];
      id1++;
      id2++;
    } else if (dp[id1 + 1][id2] > dp[id1][id2 + 1])
      id1++;
    else
      id2++;
  }
  return str;
}

int main() {
  cin >> str1 >> str2;
  n = str1.size();
  m = str2.size();
  memset(dp, -1, sizeof dp);
  solve(0, 0);
  cout << printLCS() << endl;
  return 0;
}
Problem p03165 (C++)

Buggy submission (status: Runtime Error):
#pragma GCC optimize("O3")
#include <bits/stdc++.h>
#define ll long long
#define ld long double
#define endl '\n'
#define INF 10000000000000000
#define pll pair<ll, ll>
#define ff first
#define ss second

using namespace std;
const ll N = 200005;
const ll MOD = 998244353;
ll dp[305][305];
pll p[305][305];
int main() {
  // freopen("input.txt","r",stdin);
  //   freopen("output.txt","w",stdout);
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  string s, t;
  cin >> s >> t;
  ll n = s.size();
  ll m = t.size();
  for (ll i = 0; i < n; i++) {
    for (ll j = 0; j < m; j++) {
      if (s[i] == t[j]) {
        if (dp[i + 1][j + 1] < dp[i][j] + 1) {
          dp[i + 1][j + 1] = dp[i][j] + 1;
          p[i + 1][j + 1] = {i, j};
        }
      }
      if (dp[i][j + 1] < dp[i][j]) {
        dp[i][j + 1] = dp[i][j];
        p[i][j + 1] = {i, j};
      }
      if (dp[i + 1][j] < dp[i][j]) {
        dp[i + 1][j] = dp[i][j];
        p[i + 1][j] = {i, j};
      }
    }
  }
  ll i1 = 0;
  ll j1 = 0;
  for (ll i = 0; i < n + 1; i++) {
    for (ll j = 0; j < m + 1; j++) {
      if (dp[i][j] > dp[i1][j1]) {
        i1 = i;
        j1 = j;
      }
    }
  }
  string an = "";
  while (i1 != 0 && j1 != 0) {
    pll k = p[i1][j1];
    if (dp[i1][j1] > dp[k.ff][k.ss])
      an += s[k.ff];
    i1 = k.ff;
    j1 = k.ss;
  }
  reverse(an.begin(), an.end());
  cout << an;
}


Fixed submission:
#pragma GCC optimize("O3")
#include <bits/stdc++.h>
#define ll long long
#define ld long double
#define endl '\n'
#define INF 10000000000000000
#define pll pair<ll, ll>
#define ff first
#define ss second

using namespace std;
const ll N = 200005;
const ll MOD = 998244353;
ll dp[3005][3005];
pll p[3005][3005];
int main() {
  // freopen("input.txt","r",stdin);
  //   freopen("output.txt","w",stdout);
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  string s, t;
  cin >> s >> t;
  ll n = s.size();
  ll m = t.size();
  for (ll i = 0; i < n; i++) {
    for (ll j = 0; j < m; j++) {
      if (s[i] == t[j]) {
        if (dp[i + 1][j + 1] < dp[i][j] + 1) {
          dp[i + 1][j + 1] = dp[i][j] + 1;
          p[i + 1][j + 1] = {i, j};
        }
      }
      if (dp[i][j + 1] < dp[i][j]) {
        dp[i][j + 1] = dp[i][j];
        p[i][j + 1] = {i, j};
      }
      if (dp[i + 1][j] < dp[i][j]) {
        dp[i + 1][j] = dp[i][j];
        p[i + 1][j] = {i, j};
      }
    }
  }
  ll i1 = 0;
  ll j1 = 0;
  for (ll i = 0; i < n + 1; i++) {
    for (ll j = 0; j < m + 1; j++) {
      if (dp[i][j] > dp[i1][j1]) {
        i1 = i;
        j1 = j;
      }
    }
  }
  string an = "";
  while (i1 != 0 && j1 != 0) {
    pll k = p[i1][j1];
    if (dp[i1][j1] > dp[k.ff][k.ss])
      an += s[k.ff];
    i1 = k.ff;
    j1 = k.ss;
  }
  reverse(an.begin(), an.end());
  cout << an;
}
Problem p03165 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
int ans = 0;
int main() {
  string s;
  string t;
  cin >> s >> t;
  int dp[100][100];
  for (int i = 0; i <= s.length(); i++) {
    for (int j = 0; j <= t.length(); j++) {
      if (i == 0 || j == 0) {
        dp[i][j] = 0;
        continue;
      }
      if (s[i - 1] == t[j - 1]) {
        dp[i][j] = dp[i - 1][j - 1] + 1;
      } else {
        dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]);
      }
    }
  }
  // cout << dp[s.length()][t.length()];
  int i = s.length(), j = t.length();
  char ans[dp[i][j] + 1];
  ans[dp[i][j]] = '\0';

  int k = dp[s.length()][t.length()] - 1;
  while (i >= 0 && j >= 0) {
    if (dp[i][j] != dp[i - 1][j] && dp[i][j] != dp[i][j - 1]) {
      ans[k--] = s[i - 1];
      i--;
      j--;
    } else if (dp[i][j] == dp[i - 1][j]) {
      i--;
    } else {
      j--;
    }
  }
  cout << ans << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int ans = 0;
int main() {
  string s;
  string t;
  cin >> s >> t;

  int m = s.length();
  int n = t.length();
  int dp[m + 5][n + 5];
  for (int i = 0; i <= m; i++) {
    for (int j = 0; j <= n; j++) {
      if (i == 0 || j == 0) {
        dp[i][j] = 0;
        continue;
      }
      if (s[i - 1] == t[j - 1]) {
        dp[i][j] = dp[i - 1][j - 1] + 1;
      } else {
        dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]);
      }
    }
  }
  // cout << dp[s.length()][t.length()];
  int i = s.length(), j = t.length();
  char ans[dp[i][j] + 1];
  ans[dp[i][j]] = '\0';

  int k = dp[s.length()][t.length()] - 1;
  while (i >= 0 && j >= 0) {
    if (dp[i][j] != dp[i - 1][j] && dp[i][j] != dp[i][j - 1]) {
      ans[k--] = s[i - 1];
      i--;
      j--;
    } else if (dp[i][j] == dp[i - 1][j]) {
      i--;
    } else {
      j--;
    }
  }
  cout << ans << endl;
}
Problem p03165 (C++)

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

#define FOR(i, a, b) for (int i = (a); i < int(b); ++i)
#define RFOR(i, a, b) for (int i = (b)-1; i >= int(a); --i)
#define rep(i, n) FOR(i, 0, n)
#define rep1(i, n) FOR(i, 1, int(n) + 1)
#define rrep(i, n) RFOR(i, 0, n)
#define rrep1(i, n) RFOR(i, 1, int(n) + 1)
#define all(c) begin(c), end(c)

template <typename String> String lcs(String const &a, String const &b) {
  const int n = a.size(), m = b.size();
  static int X[2048][2048], Y[2048][2048]; // 16MB x 2
  rep(i, n + 1) X[i][0] = 0;
  rep(i, m + 1) Y[0][i] = 0;
  rep(i, n) rep(j, m) {
    if (a[i] == b[j]) {
      X[i + 1][j + 1] = X[i][j] + 1;
      Y[i + 1][j + 1] = 0;
    } else if (X[i + 1][j] < X[i][j + 1]) {
      X[i + 1][j + 1] = X[i][j + 1];
      Y[i + 1][j + 1] = +1;
    } else {
      X[i + 1][j + 1] = X[i + 1][j];
      Y[i + 1][j + 1] = -1;
    }
  }
  String c;
  for (int i = n, j = m; i > 0 && j > 0;) {
    if (Y[i][j] > 0)
      --i;
    else if (Y[i][j] < 0)
      --j;
    else {
      c.push_back(a[i - 1]);
      --i;
      --j;
    }
  }
  reverse(c.begin(), c.end());
  return c;
}
signed main() {

  //(1ll<<N)シフト演算のオーバーフローに気をつける
  std::string x, y;
  std::cin >> x >> y;
  std::cout << lcs(x, y) << std::endl;

  return 0;
}

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

#define FOR(i, a, b) for (int i = (a); i < int(b); ++i)
#define RFOR(i, a, b) for (int i = (b)-1; i >= int(a); --i)
#define rep(i, n) FOR(i, 0, n)
#define rep1(i, n) FOR(i, 1, int(n) + 1)
#define rrep(i, n) RFOR(i, 0, n)
#define rrep1(i, n) RFOR(i, 1, int(n) + 1)
#define all(c) begin(c), end(c)

template <typename String> String lcs(String const &a, String const &b) {
  const int n = a.size(), m = b.size();
  static int X[3030][3030], Y[3030][3030]; // 16MB x 2
  rep(i, n + 1) X[i][0] = 0;
  rep(i, m + 1) Y[0][i] = 0;
  rep(i, n) rep(j, m) {
    if (a[i] == b[j]) {
      X[i + 1][j + 1] = X[i][j] + 1;
      Y[i + 1][j + 1] = 0;
    } else if (X[i + 1][j] < X[i][j + 1]) {
      X[i + 1][j + 1] = X[i][j + 1];
      Y[i + 1][j + 1] = +1;
    } else {
      X[i + 1][j + 1] = X[i + 1][j];
      Y[i + 1][j + 1] = -1;
    }
  }
  String c;
  for (int i = n, j = m; i > 0 && j > 0;) {
    if (Y[i][j] > 0)
      --i;
    else if (Y[i][j] < 0)
      --j;
    else {
      c.push_back(a[i - 1]);
      --i;
      --j;
    }
  }
  reverse(c.begin(), c.end());
  return c;
}
signed main() {

  //(1ll<<N)シフト演算のオーバーフローに気をつける
  std::string x, y;
  std::cin >> x >> y;
  std::cout << lcs(x, y) << std::endl;

  return 0;
}
Problem p03165 (C++)

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

using namespace std;
const int N = 1e3 + 7;

int dp[N][N]; // dp[i][j] 到 i，j为止最长的公共子序列长度

int main() {
  string s, t;
  cin >> s >> t;
  s = "0" + s;
  t = "0" + t;
  int ls = s.size(), lt = t.size();

  for (int i = 1; i < ls; i++) {
    for (int j = 1; j < lt; j++) {
      dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]);
      if (s[i] == t[j])
        dp[i][j] = max(dp[i - 1][j - 1] + 1, dp[i][j]);
    }
  }
  string ans = "";
  for (int i = ls - 1, j = lt - 1; i && j;) {
    if (s[i] == t[j])
      ans += s[i], i--, j--;
    else if (dp[i - 1][j] > dp[i][j - 1])
      i--;
    else
      j--;
  }
  reverse(ans.begin(), ans.end());
  cout << ans << "\n";
  return 0;
}

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

using namespace std;
const int N = 3e3 + 7;

int dp[N][N]; // dp[i][j] 到 i，j为止最长的公共子序列长度

int main() {
  string s, t;
  cin >> s >> t;
  s = "0" + s;
  t = "0" + t;
  int ls = s.size(), lt = t.size();

  for (int i = 1; i < ls; i++) {
    for (int j = 1; j < lt; j++) {
      dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]);
      if (s[i] == t[j])
        dp[i][j] = max(dp[i - 1][j - 1] + 1, dp[i][j]);
    }
  }
  string ans = "";
  for (int i = ls - 1, j = lt - 1; i && j;) {
    if (s[i] == t[j])
      ans += s[i], i--, j--;
    else if (dp[i - 1][j] > dp[i][j - 1])
      i--;
    else
      j--;
  }
  reverse(ans.begin(), ans.end());
  cout << ans << "\n";
  return 0;
}
Problem p03165 (C++)

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

using namespace std;

int memo[1005][1005]; /// ... for storing DP values ...

string a, b; /// ... Two Strings ...

int call(int len1, int len2) {
  /// if two string come to the end then there are no substring can create ...
  /// so we return Zero.
  if (len1 == (int)a.size() || len2 == (int)b.size())
    return 0;
  /// ... if the value is saved before then return it ...
  if (memo[len1][len2] != -1)
    return memo[len1][len2];
  /// ... if two charecture is matched ... then the length is increased by one
  /// and we go to the next phase (i + 1, j + 1) ...
  if (a[len1] == b[len2]) {
    return memo[len1][len2] = 1 + call(len1 + 1, len2 + 1);
  }
  /**
      if not then we go to either by removing charecture of first or form
  second. then we collect the maximun the two function returns and saves it.
  **/
  else
    return memo[len1][len2] = max(call(len1 + 1, len2), call(len1, len2 + 1));
}

/// ... printing LCS ...
vector<char> res;
void PrintLCS(int len1, int len2) {
  if (len1 == (int)a.size() || len2 == (int)b.size())
    return;
  if (a[len1] == b[len2]) {
    cout << a[len1];
    PrintLCS(len1 + 1, len2 + 1);
  } else {
    if (memo[len1 + 1][len2] > memo[len1][len2 + 1])
      PrintLCS(len1 + 1, len2);
    else
      PrintLCS(len1, len2 + 1);
  }
}

int main() {
  memset(memo, -1, sizeof(memo));
  cin >> a >> b;
  call(0, 0);
  PrintLCS(0, 0);
}


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

using namespace std;

int memo[3005][3005]; /// ... for storing DP values ...

string a, b; /// ... Two Strings ...

int call(int len1, int len2) {
  /// if two string come to the end then there are no substring can create ...
  /// so we return Zero.
  if (len1 == (int)a.size() || len2 == (int)b.size())
    return 0;
  /// ... if the value is saved before then return it ...
  if (memo[len1][len2] != -1)
    return memo[len1][len2];
  /// ... if two charecture is matched ... then the length is increased by one
  /// and we go to the next phase (i + 1, j + 1) ...
  if (a[len1] == b[len2]) {
    return memo[len1][len2] = 1 + call(len1 + 1, len2 + 1);
  }
  /**
      if not then we go to either by removing charecture of first or form
  second. then we collect the maximun the two function returns and saves it.
  **/
  else
    return memo[len1][len2] = max(call(len1 + 1, len2), call(len1, len2 + 1));
}

/// ... printing LCS ...
vector<char> res;
void PrintLCS(int len1, int len2) {
  if (len1 == (int)a.size() || len2 == (int)b.size())
    return;
  if (a[len1] == b[len2]) {
    cout << a[len1];
    PrintLCS(len1 + 1, len2 + 1);
  } else {
    if (memo[len1 + 1][len2] > memo[len1][len2 + 1])
      PrintLCS(len1 + 1, len2);
    else
      PrintLCS(len1, len2 + 1);
  }
}

int main() {
  memset(memo, -1, sizeof(memo));
  cin >> a >> b;
  call(0, 0);
  PrintLCS(0, 0);
}
Problem p03165 (C++)

Buggy submission (status: Runtime Error):
#include <cstdio>
#include <iostream>
#include <string>
using namespace std;

int main() {
  // freopen("LCS.inp","r",stdin);
  int i, j;
  string s, t, k = "";
  getline(cin, s);
  getline(cin, t);
  int f[s.length()][t.length()];
  for (i = 0; i <= s.length(); i++)
    f[i][0] = 0;
  for (j = 0; j <= t.length(); j++)
    f[0][j] = 0;

  for (i = 1; i <= s.length(); i++)
    for (j = 1; j <= t.length(); j++) {
      if (s[i - 1] == t[j - 1])
        f[i][j] = f[i - 1][j - 1] + 1;
      else
        f[i][j] = max(f[i - 1][j], f[i][j - 1]);
    }
  i = s.length();
  j = t.length();
  while (i > 0 && j > 0) {
    if (s[i - 1] == t[j - 1]) {
      k = char(s[i - 1]) + k;
      // cout<<"chon "<<i<<j<<s[i-1]<<endl;
      i--;
      j--;
    } else {
      if (f[i][j - 1] == f[i][j])
        j--;
      else
        i--;
    }
  }
  cout << k;

  return 0;
}


Fixed submission:
#include <cstdio>
#include <iostream>
#include <string>
using namespace std;

int main() {
  // freopen("LCS.inp","r",stdin);
  int i, j;
  string s, t, k = "";
  getline(cin, s);
  getline(cin, t);
  int f[s.length() + 1][t.length() + 1];
  for (i = 0; i <= s.length(); i++)
    f[i][0] = 0;
  for (j = 0; j <= t.length(); j++)
    f[0][j] = 0;

  for (i = 1; i <= s.length(); i++)
    for (j = 1; j <= t.length(); j++) {
      if (s[i - 1] == t[j - 1])
        f[i][j] = f[i - 1][j - 1] + 1;
      else
        f[i][j] = max(f[i - 1][j], f[i][j - 1]);
    }
  i = s.length();
  j = t.length();
  while (i > 0 && j > 0) {
    if (s[i - 1] == t[j - 1]) {
      k = char(s[i - 1]) + k;
      // cout<<"chon "<<i<<j<<s[i-1]<<endl;
      i--;
      j--;
    } else {
      if (f[i][j - 1] == f[i][j])
        j--;
      else
        i--;
    }
  }
  cout << k;

  return 0;
}
Problem p03165 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define F first
#define S second
#define mp make_pair
#define pb push_back
#define ll long long
#define LEFT(a) ((a) << 1)
#define RIGHT(a) (LEFT(a) + 1)
#define MID(a, b) ((a + b) >> 1)
#define MAX(a, b) ((a) > (b) ? (a) : (b))
#define MIN(a, b) ((a) < (b) ? (a) : (b))

using namespace std;

const ll N = 1e3 + 5;

string A, B;
int n, m;
int dp[N][N];
string S;

void go(int x, int y) {
  if (dp[x][y] == 0)
    return;
  if (dp[x][y] == dp[x - 1][y]) {
    go(x - 1, y);
    return;
  }
  if (dp[x][y] == dp[x][y - 1]) {
    go(x, y - 1);
    return;
  }
  S += A[x];
  go(x - 1, y - 1);
}

int main() {
  ios::sync_with_stdio(false);
  cin >> A >> B;
  n = (int)A.size();
  m = (int)B.size();
  A = "#" + A;
  B = "#" + B;
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++) {
      dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]);
      if (A[i] == B[j])
        dp[i][j] = max(dp[i][j], dp[i - 1][j - 1] + 1);
    }
  go(n, m);
  for (int i = dp[n][m] - 1; i >= 0; i--)
    cout << S[i];
  cout << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define F first
#define S second
#define mp make_pair
#define pb push_back
#define ll long long
#define LEFT(a) ((a) << 1)
#define RIGHT(a) (LEFT(a) + 1)
#define MID(a, b) ((a + b) >> 1)
#define MAX(a, b) ((a) > (b) ? (a) : (b))
#define MIN(a, b) ((a) < (b) ? (a) : (b))

using namespace std;

const ll N = 3e3 + 5;

string A, B;
int n, m;
int dp[N][N];
string S;

void go(int x, int y) {
  if (dp[x][y] == 0)
    return;
  if (dp[x][y] == dp[x - 1][y]) {
    go(x - 1, y);
    return;
  }
  if (dp[x][y] == dp[x][y - 1]) {
    go(x, y - 1);
    return;
  }
  S += A[x];
  go(x - 1, y - 1);
}

int main() {
  ios::sync_with_stdio(false);
  cin >> A >> B;
  n = (int)A.size();
  m = (int)B.size();
  A = "#" + A;
  B = "#" + B;
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++) {
      dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]);
      if (A[i] == B[j])
        dp[i][j] = max(dp[i][j], dp[i - 1][j - 1] + 1);
    }
  go(n, m);
  for (int i = dp[n][m] - 1; i >= 0; i--)
    cout << S[i];
  cout << endl;
  return 0;
}
Problem p03165 (C++)

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

using namespace std;

#define li long long int

const int N = 1e3 + 5;
const li mod = 1e9 + 7;

string a, b;
int n, m;
int dp[N][N];

int go(int x, int y) {
  if (x >= n or y >= m)
    return dp[x][y] = 0;
  if (dp[x][y] != -1)
    return dp[x][y];
  int ans = 0;
  if (a[x] == b[y])
    ans = 1 + go(x + 1, y + 1);
  else
    ans = max(go(x, y + 1), go(x + 1, y));
  return dp[x][y] = ans;
}

void reconstruct(int x, int y) {
  if (x >= n or y >= m)
    exit(0);
  if (dp[x][y + 1] != -1 and dp[x][y] == dp[x][y + 1]) {
    reconstruct(x, y + 1);
  } else if (dp[x + 1][y] != -1 and dp[x][y] == dp[x + 1][y]) {
    reconstruct(x + 1, y);
  } else if (dp[x + 1][y + 1] != -1 and dp[x][y] == 1 + dp[x + 1][y + 1]) {
    cout << a[x];
    reconstruct(x + 1, y + 1);
  }
}

void solve() {
  cin >> a >> b;
  n = (int)a.length();
  m = (int)b.length();
  memset(dp, -1, sizeof(dp));
  go(0, 0);
  reconstruct(0, 0);
}

int main() {
  int t = 1;
  // cin >> t;
  while (t--) {
    solve();
  }
  return 0;
}

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

using namespace std;

#define li long long int

const int N = 3e3 + 5;
const li mod = 1e9 + 7;

string a, b;
int n, m;
int dp[N][N];

int go(int x, int y) {
  if (x >= n or y >= m)
    return dp[x][y] = 0;
  if (dp[x][y] != -1)
    return dp[x][y];
  int ans = 0;
  if (a[x] == b[y])
    ans = 1 + go(x + 1, y + 1);
  else
    ans = max(go(x, y + 1), go(x + 1, y));
  return dp[x][y] = ans;
}

void reconstruct(int x, int y) {
  if (x >= n or y >= m)
    exit(0);
  if (dp[x][y + 1] != -1 and dp[x][y] == dp[x][y + 1]) {
    reconstruct(x, y + 1);
  } else if (dp[x + 1][y] != -1 and dp[x][y] == dp[x + 1][y]) {
    reconstruct(x + 1, y);
  } else if (dp[x + 1][y + 1] != -1 and dp[x][y] == 1 + dp[x + 1][y + 1]) {
    cout << a[x];
    reconstruct(x + 1, y + 1);
  }
}

void solve() {
  cin >> a >> b;
  n = (int)a.length();
  m = (int)b.length();
  memset(dp, -1, sizeof(dp));
  go(0, 0);
  reconstruct(0, 0);
}

int main() {
  int t = 1;
  // cin >> t;
  while (t--) {
    solve();
  }
  return 0;
}
Problem p03165 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define F first
#define S second
#define ll long long
#define PB push_back
#define PII pair<long long, long long>
#define FAST                                                                   \
  ios::sync_with_stdio(false);                                                 \
  cin.tie(0);                                                                  \
  cout.tie(0);

const ll MAXN = 1e3 + 10, INF = 8e18, MOD = 1e9 + 10;
int lcs[MAXN][MAXN];
pair<int, int> parent[MAXN][MAXN];

int main() {
  string a, b;
  cin >> a >> b;
  memset(lcs, 0, sizeof(lcs));
  int n = (int)a.size(), m = (int)b.size();
  for (int i = 1; i <= n; ++i) {
    for (int j = 1; j <= m; ++j) {
      lcs[i][j] = max(lcs[i - 1][j], lcs[i][j - 1]);
      if (a[i - 1] == b[j - 1])
        lcs[i][j] = lcs[i - 1][j - 1] + 1;
    }
  }
  vector<char> v;
  while (n && m) {
    if (lcs[n][m] == lcs[n - 1][m])
      n--;
    else if (lcs[n][m] == lcs[n][m - 1])
      m--;
    else {
      v.push_back(a[n - 1]);
      n--, m--;
    }
  }
  reverse(v.begin(), v.end());
  for (auto u : v)
    cout << u;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define F first
#define S second
#define ll long long
#define PB push_back
#define PII pair<long long, long long>
#define FAST                                                                   \
  ios::sync_with_stdio(false);                                                 \
  cin.tie(0);                                                                  \
  cout.tie(0);

const ll MAXN = 3e3 + 10, INF = 8e18, MOD = 1e9 + 10;
int lcs[MAXN][MAXN];
pair<int, int> parent[MAXN][MAXN];

int main() {
  string a, b;
  cin >> a >> b;
  memset(lcs, 0, sizeof(lcs));
  int n = (int)a.size(), m = (int)b.size();
  for (int i = 1; i <= n; ++i) {
    for (int j = 1; j <= m; ++j) {
      lcs[i][j] = max(lcs[i - 1][j], lcs[i][j - 1]);
      if (a[i - 1] == b[j - 1])
        lcs[i][j] = lcs[i - 1][j - 1] + 1;
    }
  }
  vector<char> v;
  while (n && m) {
    if (lcs[n][m] == lcs[n - 1][m])
      n--;
    else if (lcs[n][m] == lcs[n][m - 1])
      m--;
    else {
      v.push_back(a[n - 1]);
      n--, m--;
    }
  }
  reverse(v.begin(), v.end());
  for (auto u : v)
    cout << u;
}
Problem p03165 (C++)

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

using namespace std;

using ll = long long;
const ll INF = 4e18, N = 3001;

string s, t;
ll dp[N][N];

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);

  cin >> s >> t;

  for (int i = 0; i <= s.size(); i += 1)
    for (int j = 0; j <= t.size(); j += 1)
      dp[i][j] = 0;
  for (int i = 0; i <= s.size(); i += 1)
    dp[i][0] = 0;
  for (int i = 0; i <= t.size(); i += 1)
    dp[0][i] = 0;

  string ans;
  for (int i = 1; i <= s.size(); i += 1) {
    for (int j = 1; j <= t.size(); j += 1) {
      if (s[i - 1] == t[j - 1])
        dp[i][j] = dp[i - 1][j - 1] + 1;
      else
        dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]);
    }
  }

  int i = s.size();
  int j = t.size();

  while (i >= 0 && j >= 0) {
    if (dp[i][j] == dp[i - 1][j])
      i -= 1;
    else if (dp[i][j] == dp[i][j - 1])
      j -= 1;
    else
      ans += s[i - 1], i -= 1, j -= 1;
  }
  reverse(ans.begin(), ans.end());
  cout << ans << endl;
  return 0;
}


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

using namespace std;

using ll = long long;
const ll INF = 4e18, N = 3001;

string s, t;
ll dp[N][N];

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);

  cin >> s >> t;

  for (int i = 0; i <= s.size(); i += 1)
    for (int j = 0; j <= t.size(); j += 1)
      dp[i][j] = 0;
  for (int i = 0; i <= s.size(); i += 1)
    dp[i][0] = 0;
  for (int i = 0; i <= t.size(); i += 1)
    dp[0][i] = 0;

  string ans;
  for (int i = 1; i <= s.size(); i += 1) {
    for (int j = 1; j <= t.size(); j += 1) {
      if (s[i - 1] == t[j - 1])
        dp[i][j] = dp[i - 1][j - 1] + 1;
      else
        dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]);
    }
  }

  int i = s.size();
  int j = t.size();

  while (i >= 1 && j >= 1) {
    if (dp[i][j] == dp[i - 1][j])
      i -= 1;
    else if (dp[i][j] == dp[i][j - 1])
      j -= 1;
    else
      ans += s[i - 1], i -= 1, j -= 1;
  }
  reverse(ans.begin(), ans.end());
  cout << ans << endl;
  return 0;
}
Problem p03165 (C++)

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

#define int long long
#define pb push_back
#define pii pair<int, int>
#define vi vector<int>
#define vii vector<pii>
#define mi map<int, int>
#define mii map<pii, int>
#define all(a) (a).begin(), (a).end()
#define F first
#define S second
#define sz(x) (int)x.size()
#define endl '\n'
#define hell 1000000007
#define mem(x) memset(x, 0, sizeof(x));
#define what_is(x) cout << #x << " is " << x << flush << endl;
#define rep(i, a, b) for (int i = a; i < b; i++)

using namespace std;
const int maxn = 100010;
int n, w;
void solve() {
  string s, t;
  cin >> s >> t;
  int n = s.length(), m = t.length();

  int dp[n + 1][m + 1];
  memset(dp, 0, sizeof(dp));
  rep(i, 1, n + 1) {
    rep(j, 1, m + 1) {
      if (s[i - 1] == t[j - 1])
        dp[i][j] = 1 + dp[i - 1][j - 1];
      else
        dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]);
    }
  }
  string ans = "";
  int i = n, j = m;
  int flag = 0;
  if (n > m)
    flag = 1;
  while (i > 0 || j > 0) {
    if (dp[i - 1][j - 1] + 1 == dp[i][j]) {
      ans += s[i - 1];
      i--;
      j--;
    } else {
      if (dp[i][j] == dp[i - 1][j]) {
        i--;
      } else {
        j--;
      }
    }
  }
  reverse(ans.begin(), ans.end());

  cout << ans;
}

signed main() {

  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);

  int t = 1;
  // cin>>t;

  while (t--)
    solve();

  return 0;
}

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

#define int long long
#define pb push_back
#define pii pair<int, int>
#define vi vector<int>
#define vii vector<pii>
#define mi map<int, int>
#define mii map<pii, int>
#define all(a) (a).begin(), (a).end()
#define F first
#define S second
#define sz(x) (int)x.size()
#define endl '\n'
#define hell 1000000007
#define mem(x) memset(x, 0, sizeof(x));
#define what_is(x) cout << #x << " is " << x << flush << endl;
#define rep(i, a, b) for (int i = a; i < b; i++)

using namespace std;
const int maxn = 100010;
int n, w;
void solve() {
  string s, t;
  cin >> s >> t;
  int n = s.length(), m = t.length();

  int dp[n + 1][m + 1];
  memset(dp, 0, sizeof(dp));
  rep(i, 1, n + 1) {
    rep(j, 1, m + 1) {
      if (s[i - 1] == t[j - 1])
        dp[i][j] = 1 + dp[i - 1][j - 1];
      else
        dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]);
    }
  }
  string ans = "";
  int i = n, j = m;
  int flag = 0;
  if (n > m)
    flag = 1;
  while (i > 0 && j > 0) {
    if (dp[i - 1][j - 1] + 1 == dp[i][j] && s[i - 1] == t[j - 1]) {
      ans += s[i - 1];
      i--;
      j--;
    } else {
      if (dp[i][j] == dp[i - 1][j]) {
        i--;
      } else {
        j--;
      }
    }
  }
  reverse(ans.begin(), ans.end());

  cout << ans;
}

signed main() {

  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);

  int t = 1;
  // cin>>t;

  while (t--)
    solve();

  return 0;
}
Problem p03165 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define boost ios_base::sync_with_stdio(false), cin.tie(NULL);

using namespace std;
const int N = 1e3 + 44;
const int X[] = {1, 0, -1, 0, 1, -1, 1, -1};
const int Y[] = {0, 1, 0, -1, 1, 1, -1, -1};
const int mod = 1e9 + 7;
const long long INF = 1e18;
const int inf = 1e9;

int dp[N][N];

pair<int, int> p[N][N];

int a[N], b[N];

int main() {
  boost;
  string s, t;
  cin >> s >> t;
  int ns = s.size(), nt = t.size();
  for (int i = 0; i < ns; ++i)
    a[i + 1] = (int)(s[i] - 'a');
  for (int i = 0; i < nt; ++i)
    b[i + 1] = (int)(t[i] - 'a');
  for (int i = 1; i <= ns; ++i) {
    for (int j = 1; j <= nt; ++j) {
      if (a[i] == b[j])
        dp[i][j] = dp[i - 1][j - 1] + 1, p[i][j] = {i - 1, j - 1};
      if (dp[i][j] <= dp[i - 1][j])
        dp[i][j] = dp[i - 1][j], p[i][j] = {i - 1, j};
      if (dp[i][j] <= dp[i][j - 1])
        dp[i][j] = dp[i][j - 1], p[i][j] = {i, j - 1};
    }
  }
  string ans = "";
  while (ns && nt) {
    if (p[ns][nt].first != ns && p[ns][nt].second != nt) {
      ans += s[ns - 1];
    }
    tie(ns, nt) = p[ns][nt];
  }
  reverse(ans.begin(), ans.end());
  cout << ans;
}

Fixed submission:
#include <bits/stdc++.h>
#define boost ios_base::sync_with_stdio(false), cin.tie(NULL);

using namespace std;
const int N = 4e3 + 44;
const int X[] = {1, 0, -1, 0, 1, -1, 1, -1};
const int Y[] = {0, 1, 0, -1, 1, 1, -1, -1};
const int mod = 1e9 + 7;
const long long INF = 1e18;
const int inf = 1e9;

int dp[N][N];

pair<int, int> p[N][N];

int a[N], b[N];

int main() {
  boost;
  string s, t;
  cin >> s >> t;
  int ns = s.size(), nt = t.size();
  for (int i = 0; i < ns; ++i)
    a[i + 1] = (int)(s[i] - 'a');
  for (int i = 0; i < nt; ++i)
    b[i + 1] = (int)(t[i] - 'a');
  for (int i = 1; i <= ns; ++i) {
    for (int j = 1; j <= nt; ++j) {
      if (a[i] == b[j])
        dp[i][j] = dp[i - 1][j - 1] + 1, p[i][j] = {i - 1, j - 1};
      if (dp[i][j] <= dp[i - 1][j])
        dp[i][j] = dp[i - 1][j], p[i][j] = {i - 1, j};
      if (dp[i][j] <= dp[i][j - 1])
        dp[i][j] = dp[i][j - 1], p[i][j] = {i, j - 1};
    }
  }
  string ans = "";
  while (ns && nt) {
    if (p[ns][nt].first != ns && p[ns][nt].second != nt) {
      ans += s[ns - 1];
    }
    tie(ns, nt) = p[ns][nt];
  }
  reverse(ans.begin(), ans.end());
  cout << ans;
}
Problem p03165 (C++)

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

#define ll long long int
#define endl '\n'
const long long inf = 1e18;
ll dp[3005][3005];
string find(string a, string b) {
  ll n = a.length();
  ll m = b.length();

  for (int i = 0; i <= n; i++) {
    for (int j = 0; j <= m; j++) {
      if (i == 0 || j == 0) {
        dp[i][j] = 0;
      } else if (a[i - 1] == b[j - 1]) {
        dp[i][j] = dp[i - 1][j - 1] + 1;
      } else {
        dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]);
      }
    }
  }

  ll i = n;
  ll j = m;
  string ans = "";
  while (i > 0 and j > 0) {
    if (a[i - 1] == b[j - 1]) {
      ans = a[i - 1] + ans;
      i--;
      j--;
    } else if (dp[i - 1][j] > dp[i][j - 1]) {
      i--;
    } else {
      j--;
    }
  }
  cout << ans;
}
void testCase() {

  // ll t; cin>>t; while(t--)
  {
    string a, b;
    cin >> a >> b;
    cout << find(a, b) << endl;
  }
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  testCase();
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;

#define ll long long int
#define endl '\n'
const long long inf = 1e18;
ll dp[3005][3005];
string find(string a, string b) {
  ll n = a.length();
  ll m = b.length();

  for (int i = 0; i <= n; i++) {
    for (int j = 0; j <= m; j++) {
      if (i == 0 || j == 0) {
        dp[i][j] = 0;
      } else if (a[i - 1] == b[j - 1]) {
        dp[i][j] = dp[i - 1][j - 1] + 1;
      } else {
        dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]);
      }
    }
  }

  ll i = n;
  ll j = m;
  string ans = "";
  while (i > 0 and j > 0) {
    if (a[i - 1] == b[j - 1]) {
      ans = a[i - 1] + ans;
      i--;
      j--;
    } else if (dp[i - 1][j] > dp[i][j - 1]) {
      i--;
    } else {
      j--;
    }
  }
  return ans;
}
void testCase() {

  // ll t; cin>>t; while(t--)
  {
    string a, b;
    cin >> a >> b;
    cout << find(a, b) << endl;
  }
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  testCase();
  return 0;
}
Problem p03165 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <stack>
#include <string>
#include <vector>

using namespace std;

template <class T> istream &operator>>(istream &in, vector<T> &arr) {
  for (auto &x : arr)
    in >> x;
  return in;
}
template <class T> ostream &operator<<(ostream &out, const vector<T> &arr) {
  for (auto &x : arr)
    out << x << ' ';
  cout << "\n";
  return out;
}

enum { NW, N, W };

void printlcs(string s, vector<vector<int>> dp, int i, int j) {
  if (i == 0 || j == 0) {
    return;
  }
  if (dp[i][j] == NW) {
    printlcs(s, dp, i - 1, j - 1);
    cout << s[i - 1];
  } else if (dp[i][j] == N) {
    printlcs(s, dp, i - 1, j);
  } else if (dp[i][j] == W) {
    printlcs(s, dp, i, j - 1);
  }
}

void lcs(string s, string t) {

  int size_s = s.size();
  int size_t = t.size();

  vector<vector<int>> dp(size_s + 1, vector<int>(size_t + 1));
  vector<vector<int>> pdp(size_s + 1, vector<int>(size_t + 1));

  for (int i = 0; i <= size_s; i++) {
    for (int j = 0; j <= size_t; j++) {
      if (i == 0 || j == 0) {
        dp[i][j] = 0;
      } else if (s[i - 1] == t[j - 1]) {
        dp[i][j] = dp[i - 1][j - 1] + 1;
        pdp[i][j] = NW;
      } else if (dp[i - 1][j] > dp[i][j - 1]) {
        dp[i][j] = dp[i - 1][j];
        pdp[i][j] = N;
      } else {
        dp[i][j] = dp[i][j - 1];
        pdp[i][j] = W;
      }
    }
  }

  printlcs(s, pdp, size_s, size_t);
}

int main() {

  string s, t;

  cin >> s >> t;

  lcs(s, t);

  return 0;
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <stack>
#include <string>
#include <vector>

using namespace std;

template <class T> istream &operator>>(istream &in, vector<T> &arr) {
  for (auto &x : arr)
    in >> x;
  return in;
}
template <class T> ostream &operator<<(ostream &out, const vector<T> &arr) {
  for (auto &x : arr)
    out << x << ' ';
  cout << "\n";
  return out;
}

enum { NW, N, W };

void printlcs(const string &s, vector<vector<int>> &dp, int i, int j) {
  if (i == 0 || j == 0) {
    return;
  }
  if (dp[i][j] == NW) {
    printlcs(s, dp, i - 1, j - 1);
    cout << s[i - 1];
  } else if (dp[i][j] == N) {
    printlcs(s, dp, i - 1, j);
  } else if (dp[i][j] == W) {
    printlcs(s, dp, i, j - 1);
  }
}

void lcs(string s, string t) {

  int size_s = s.size();
  int size_t = t.size();

  vector<vector<int>> dp(size_s + 1, vector<int>(size_t + 1));
  vector<vector<int>> pdp(size_s + 1, vector<int>(size_t + 1));

  for (int i = 0; i <= size_s; i++) {
    for (int j = 0; j <= size_t; j++) {
      if (i == 0 || j == 0) {
        dp[i][j] = 0;
      } else if (s[i - 1] == t[j - 1]) {
        dp[i][j] = dp[i - 1][j - 1] + 1;
        pdp[i][j] = NW;
      } else if (dp[i - 1][j] > dp[i][j - 1]) {
        dp[i][j] = dp[i - 1][j];
        pdp[i][j] = N;
      } else {
        dp[i][j] = dp[i][j - 1];
        pdp[i][j] = W;
      }
    }
  }

  printlcs(s, pdp, size_s, size_t);
}

int main() {

  string s, t;

  cin >> s >> t;

  lcs(s, t);

  return 0;
}
Problem p03165 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define IOS                                                                    \
  ios::sync_with_stdio(0);                                                     \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define endl "\n"
#define int long long
#define watch(x) cout << (#x) << " is " << (x) << endl
#define pb push_back
const int N = 1e9 + 1;

int32_t main() {

  IOS;

  string s, t;
  cin >> s >> t;

  int n = s.length();
  int m = t.length();

  int C[n + 1][m + 1];
  int B[n + 1][m + 1];
  for (int i = 0; i <= m; i++) {
    C[0][i] = 0;
  }
  for (int i = 0; i <= n; i++) {
    C[i][0] = 0;
  }

  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      if (char(t[j - 1]) == char(s[i - 1])) {
        C[i][j] = C[i - 1][j - 1] + 1;
        B[i][j] = 'D';
      } else {
        C[i][j] = max(C[i - 1][j], C[i][j - 1]);
        if (C[i - 1][j] < C[i][j - 1]) {
          B[i][j] = 'L';
        } else {
          B[i][j] = 'U';
        }
      }
    }
  }

  // cout<<C[n][m]<<endl;

  int i = n, j = m;
  string str = "";

  while (1) {
    if (i == 0 && j == 0)
      break;
    if (B[i][j] == 'D') {
      str = t[j - 1] + str;
      i--, j--;
    } else if (B[i][j] == 'L') {
      j--;
    } else
      i--;
  }

  cout << str << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define IOS                                                                    \
  ios::sync_with_stdio(0);                                                     \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define endl "\n"
#define int long long
#define watch(x) cout << (#x) << " is " << (x) << endl
#define pb push_back
const int N = 1e9 + 1;

int32_t main() {

  IOS;

  string s, t;
  cin >> s >> t;

  int n = s.length();
  int m = t.length();

  int C[n + 1][m + 1];
  int B[n + 1][m + 1];
  for (int i = 0; i <= m; i++) {
    C[0][i] = 0;
  }
  for (int i = 0; i <= n; i++) {
    C[i][0] = 0;
  }

  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      if (char(t[j - 1]) == char(s[i - 1])) {
        C[i][j] = C[i - 1][j - 1] + 1;
        B[i][j] = 'D';
      } else {
        C[i][j] = max(C[i - 1][j], C[i][j - 1]);
        if (C[i - 1][j] < C[i][j - 1]) {
          B[i][j] = 'L';
        } else {
          B[i][j] = 'U';
        }
      }
    }
  }

  // cout<<C[n][m]<<endl;

  int i = n, j = m;
  string str = "";

  while (1) {
    if (i == 0 || j == 0)
      break;
    if (B[i][j] == 'D') {
      str = t[j - 1] + str;
      i--, j--;
    } else if (B[i][j] == 'L') {
      j--;
    } else
      i--;
  }

  cout << str << endl;
  return 0;
}
Problem p03165 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define fast                                                                   \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(NULL);                                                               \
  cout.tie(NULL)
#define f(i, a, b) for (i = a; i < b; i++)
#define fr(i, a, b) for (i = a; i >= b; i--)
#define endl '\n'
#define ll long long int
#define ff first
#define ss second
#define pb push_back
using namespace std;

int dp[3005][3005];

int main() {
  string s1, s2, s3 = "";
  cin >> s1 >> s2;
  int n1 = s1.size(), n2 = s2.size(), i, j;
  f(i, 1, n1 + 1) f(j, 1, n2 + 1) {
    if (s1[i - 1] == s2[j - 1])
      dp[i][j] = 1 + dp[i - 1][j - 1];
    else
      dp[i][j] = max(dp[i][j - 1], dp[i - 1][j]);
  }
  for (i = n1, j = n2; i > 0, j > 0;) {
    if (dp[i][j] != dp[i - 1][j] && dp[i][j] != dp[i][j - 1]) {
      s3 += s1[i - 1];
      i--;
      j--;
    } else if (dp[i][j] != dp[i - 1][j] && j > 0)
      j--;
    else
      i--;
  }
  reverse(s3.begin(), s3.end());
  cout << s3;
}

Fixed submission:
#include <bits/stdc++.h>
#define fast                                                                   \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(NULL);                                                               \
  cout.tie(NULL)
#define f(i, a, b) for (i = a; i < b; i++)
#define fr(i, a, b) for (i = a; i >= b; i--)
#define endl '\n'
#define ll long long int
#define ff first
#define ss second
#define pb push_back
using namespace std;

int dp[3005][3005];

int main() {
  string s1, s2, s3 = "";
  cin >> s1 >> s2;
  int n1 = s1.size(), n2 = s2.size(), i, j;
  f(i, 1, n1 + 1) f(j, 1, n2 + 1) {
    if (s1[i - 1] == s2[j - 1])
      dp[i][j] = 1 + dp[i - 1][j - 1];
    else
      dp[i][j] = max(dp[i][j - 1], dp[i - 1][j]);
  }
  for (i = n1, j = n2; i > 0 && j > 0;) {
    if (dp[i][j] != dp[i - 1][j] && dp[i][j] != dp[i][j - 1]) {
      s3 += s1[i - 1];
      i--;
      j--;
    } else if (dp[i][j] != dp[i - 1][j] && j > 0)
      j--;
    else
      i--;
  }
  reverse(s3.begin(), s3.end());
  cout << s3;
}
Problem p03165 (C++)

Buggy submission (status: Runtime Error):
// In the name of God

#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef pair<int, int> pii;

const int N = 2e3 + 10, mod = 1e9 + 7;

int dp[N][N];
pii par[N][N], lst;

int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  string s, t;
  cin >> s >> t;
  s = "!" + s;
  t = "@" + t;
  int ans = 0;
  for (int i = 1; i < s.length(); i++)
    for (int j = 1; j < t.length(); j++) {
      if (s[i] == t[j])
        dp[i][j] = max(dp[i][j], dp[i - 1][j - 1] + 1),
        par[i][j] = {i - 1, j - 1};
      else {
        if (dp[i - 1][j] > dp[i][j]) {
          dp[i][j] = dp[i - 1][j];
          par[i][j] = {i - 1, j};
        }
        if (dp[i][j - 1] > dp[i][j]) {
          dp[i][j] = dp[i][j - 1];
          par[i][j] = {i, j - 1};
        }
      }
      if (dp[i][j] > ans) {
        ans = dp[i][j];
        lst = {i, j};
      }
    }
  string ret = "";
  while (lst.first != 0 && lst.second != 0) {
    int i = lst.first, j = lst.second;
    if (s[i] == t[j])
      ret = ret + s[i];
    lst = par[i][j];
  }
  reverse(ret.begin(), ret.end());
  cout << ret;
  return 0;
}


Fixed submission:
// In the name of God

#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef pair<int, int> pii;

const int N = 3e3 + 10, mod = 1e9 + 7;

int dp[N][N];
pii par[N][N], lst;

int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  string s, t;
  cin >> s >> t;
  s = "!" + s;
  t = "@" + t;
  int ans = 0;
  for (int i = 1; i < s.length(); i++)
    for (int j = 1; j < t.length(); j++) {
      if (s[i] == t[j])
        dp[i][j] = max(dp[i][j], dp[i - 1][j - 1] + 1),
        par[i][j] = {i - 1, j - 1};
      else {
        if (dp[i - 1][j] > dp[i][j]) {
          dp[i][j] = dp[i - 1][j];
          par[i][j] = {i - 1, j};
        }
        if (dp[i][j - 1] > dp[i][j]) {
          dp[i][j] = dp[i][j - 1];
          par[i][j] = {i, j - 1};
        }
      }
      if (dp[i][j] > ans) {
        ans = dp[i][j];
        lst = {i, j};
      }
    }
  string ret = "";
  while (lst.first != 0 && lst.second != 0) {
    int i = lst.first, j = lst.second;
    if (s[i] == t[j])
      ret = ret + s[i];
    lst = par[i][j];
  }
  reverse(ret.begin(), ret.end());
  cout << ret;
  return 0;
}