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

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
int dp[3001][3001];
int fun(string s1, string s2, int n, int m) {
  if (n == 0 || m == 0)
    return dp[n][m] = 0;
  else if (dp[n][m] != -1)
    return dp[n][m];
  else if (s1[n - 1] == s2[m - 1])
    return dp[n][m] = 1 + fun(s1, s2, n - 1, m - 1);
  else
    return dp[n][m] = max(fun(s1, s2, n - 1, m), fun(s1, s2, n, m - 1));
}
int main() {
  string s1, s2;
  cin >> s1 >> s2;
  int i, j, n = s1.length(), m = s2.length();
  for (i = 0; i <= n; i++) {
    for (j = 0; j <= m; j++)
      dp[i][j] = -1;
  }
  fun(s1, s2, n, m);
  i = n, j = m;
  string ans;
  while (i > 0 && j > 0) {
    if (s1[i - 1] == s2[j - 1]) {
      ans.push_back(s1[i - 1]);
      i--;
      j--;
    } else {
      if (dp[i - 1][j] > dp[i][j - 1])
        i--;
      else
        j--;
    }
  }
  reverse(ans.begin(), ans.end());
  cout << ans << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int dp[3001][3001];
int fun(string s1, string s2, int n, int m) {
  if (n == 0 || m == 0)
    return dp[n][m] = 0;
  else if (dp[n][m] != -1)
    return dp[n][m];
  else if (s1[n - 1] == s2[m - 1])
    return dp[n][m] = 1 + fun(s1, s2, n - 1, m - 1);
  else
    return dp[n][m] = max(fun(s1, s2, n - 1, m), fun(s1, s2, n, m - 1));
}
int main() {
  string s1, s2;
  cin >> s1 >> s2;
  int i, j, n = s1.length(), m = s2.length();
  for (i = 0; i <= n; i++) {
    for (j = 0; j <= m; j++)
      dp[i][j] = -1;
  }
  //	fun(s1,s2,n,m);
  for (i = 1; i <= n; i++) {
    for (j = 1; j <= m; j++) {
      if (s1[i - 1] == s2[j - 1])
        dp[i][j] = 1 + dp[i - 1][j - 1];
      else
        dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]);
    }
  }
  i = n, j = m;
  string ans;
  while (i > 0 && j > 0) {
    if (s1[i - 1] == s2[j - 1]) {
      ans.push_back(s1[i - 1]);
      i--;
      j--;
    } else {
      if (dp[i - 1][j] > dp[i][j - 1])
        i--;
      else
        j--;
    }
  }
  reverse(ans.begin(), ans.end());
  cout << ans << endl;
}
Problem p03165 (C++)

Buggy submission (status: Runtime Error):
#pragma GCC optimize("O3")
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<int, int>;
template <class T> using V = vector<T>;
#define fi first
#define se second
#define all(v) (v).begin(), (v).end()
const ll inf = (1e18);
const ll mod = 1000000007;
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
ll lcm(ll c, ll d) { return c / gcd(c, d) * d; }
struct __INIT {
  __INIT() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    cout << fixed << setprecision(15);
  }
} __init;
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
int dp[1005][1005];
int main() {
  string s, t;
  cin >> s >> t;
  int n = s.size(), m = t.size();
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      if (s[i] == t[j])
        chmax(dp[i + 1][j + 1], dp[i][j] + 1);
      else
        dp[i + 1][j + 1] = max(dp[i + 1][j], dp[i][j + 1]);
    }
  }
  int d = dp[n][m];
  string ans = {};
  while (1) {
    if (d == 0)
      break;
    if (n > 0 && dp[n - 1][m] == d)
      n--;
    else if (m > 0 && dp[n][m - 1] == d)
      m--;
    else if (n > 0 && m > 0) {
      n--;
      m--;
      d--;
      ans.push_back(s[n]);
    }
  }
  reverse(all(ans));
  cout << ans << endl;
}


Fixed submission:
#pragma GCC optimize("O3")
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<int, int>;
template <class T> using V = vector<T>;
#define fi first
#define se second
#define all(v) (v).begin(), (v).end()
const ll inf = (1e18);
const ll mod = 1000000007;
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
ll lcm(ll c, ll d) { return c / gcd(c, d) * d; }
struct __INIT {
  __INIT() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    cout << fixed << setprecision(15);
  }
} __init;
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
int dp[3005][3005];
int main() {
  string s, t;
  cin >> s >> t;
  int n = s.size(), m = t.size();
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      if (s[i] == t[j])
        chmax(dp[i + 1][j + 1], dp[i][j] + 1);
      else
        dp[i + 1][j + 1] = max(dp[i + 1][j], dp[i][j + 1]);
    }
  }
  int d = dp[n][m];
  string ans = {};
  while (1) {
    if (d == 0)
      break;
    if (n > 0 && dp[n - 1][m] == d)
      n--;
    else if (m > 0 && dp[n][m - 1] == d)
      m--;
    else if (n > 0 && m > 0) {
      n--;
      m--;
      d--;
      ans.push_back(s[n]);
    }
  }
  reverse(all(ans));
  cout << ans << endl;
}
Problem p03165 (C++)

Buggy submission (status: Runtime Error):
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
string s, t;
int f[3001][3001];
int main() {
  cin >> s >> t;
  for (int i = s.size(); i >= 0; i--) {
    for (int j = t.size(); j >= 0; j--) {
      if (s[i] == t[j]) {
        f[i][j] = f[i + 1][j + 1] + 1;
        continue;
      }
      f[i][j] = max(f[i][j + 1], f[i + 1][j]);
    }
  }
  int h = 0, k = 0;
  while (h < s.size() && k < t.size()) {
    if (s[h] == t[k]) {
      printf("%c", s[h]);
      h++;
      k++;
      continue;
    }
    if (f[h][k] == f[h + 1][k]) {
      h++;
      continue;
    }
    k++;
  }
}

Fixed submission:
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
string s, t;
int f[3100][3100];
int main() {
  cin >> s >> t;
  for (int i = s.size(); i >= 0; i--) {
    for (int j = t.size(); j >= 0; j--) {
      if (s[i] == t[j]) {
        f[i][j] = f[i + 1][j + 1] + 1;
        continue;
      }
      f[i][j] = max(f[i][j + 1], f[i + 1][j]);
    }
  }
  int h = 0, k = 0;
  while (h < s.size() && k < t.size()) {
    if (s[h] == t[k]) {
      printf("%c", s[h]);
      h++;
      k++;
      continue;
    }
    if (f[h][k] == f[h + 1][k]) {
      h++;
      continue;
    }
    k++;
  }
}
Problem p03165 (C++)

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

using namespace std;
#define ll unsigned long long

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  string A;
  string B;
  cin >> A;
  cin >> B;
  int n = A.length();
  int m = B.length();
  int dp[n][m];
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      dp[i][j] = 0;
    }
  }
  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]);
      }
    }
  }
  int ans = dp[n][m];
  map<int, int> check;
  vector<char> res;
  for (int i = n; i >= 1; i--) {
    for (int j = m; j >= 1; j--) {
      if (A[i - 1] == B[j - 1] && dp[i][j] == ans && check[i - 1] == 0) {
        res.push_back(A[i - 1]);
        check[i - 1]++;
        ans--;
      }
      if (ans == 0)
        break;
    }
  }
  for (int i = res.size() - 1; i >= 0; i--) {
    cout << res[i];
  }
}

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

using namespace std;
#define ll unsigned long long

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  string A;
  string B;
  cin >> A;
  cin >> B;
  int n = A.length();
  int m = B.length();
  int dp[n + 1][m + 1];
  for (int i = 0; i <= n; i++) {
    for (int j = 0; j <= m; j++) {
      dp[i][j] = 0;
    }
  }
  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]);
      }
    }
  }
  int ans = dp[n][m];
  map<int, int> check;
  vector<char> res;
  for (int i = n; i >= 1; i--) {
    for (int j = m; j >= 1; j--) {
      if (A[i - 1] == B[j - 1] && dp[i][j] == ans && check[i - 1] == 0) {
        res.push_back(A[i - 1]);
        check[i - 1]++;
        ans--;
      }
      if (ans == 0)
        break;
    }
  }
  for (int i = res.size() - 1; i >= 0; i--) {
    cout << res[i];
  }
}
Problem p03165 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <climits>
#include <cmath>
#include <complex>
#include <ctime>
#include <deque>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <vector>

#define ff first
#define ss second

using namespace std;
using pii = pair<int, int>;
using ll = long long;

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

  string s, t;
  cin >> s >> t;
  vector<vector<pair<int, pii>>> dp(
      s.length(), vector<pair<int, pii>>(t.length(), {0, {-1, -1}}));
  if (s[0] == t[0])
    dp[0][0] = {1, {0, 0}};
  for (int i = 0; i < s.length(); i++) {
    for (int j = 0; j < t.length(); j++) {
      if (i + j == 0)
        continue;
      if (i > 0 && dp[i][j].ff < dp[i - 1][j].ff)
        dp[i][j] = dp[i - 1][j];
      if (j > 0 && dp[i][j].ff < dp[i][j - 1].ff)
        dp[i][j] = dp[i][j - 1];
      if (s[i] == t[j]) {
        if (i > 0 && j > 0)
          dp[i][j].ff = dp[i - 1][j - 1].ff + 1;
        else
          dp[i][j].ff = 1;
        dp[i][j].ss = {i, j};
      }
    }
  }
  pii now = {s.length() - 1, t.length() - 1};
  string answer;
  while (now.ff != -1 && now.ss != -1) {
    now = dp[now.ff][now.ss].ss;
    answer += s[now.ff];
    now.ff -= 1;
    now.ss -= 1;
  }
  reverse(answer.begin(), answer.end());
  cout << answer;

  return 0;
}


Fixed submission:
#include <algorithm>
#include <bitset>
#include <climits>
#include <cmath>
#include <complex>
#include <ctime>
#include <deque>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <vector>

#define ff first
#define ss second

using namespace std;
using pii = pair<int, int>;
using ll = long long;

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

  string s, t;
  cin >> s >> t;
  vector<vector<pair<int, pii>>> dp(
      s.length(), vector<pair<int, pii>>(t.length(), {0, {-1, -1}}));
  if (s[0] == t[0])
    dp[0][0] = {1, {0, 0}};
  for (int i = 0; i < s.length(); i++) {
    for (int j = 0; j < t.length(); j++) {
      if (i + j == 0)
        continue;
      if (i > 0 && dp[i][j].ff < dp[i - 1][j].ff)
        dp[i][j] = dp[i - 1][j];
      if (j > 0 && dp[i][j].ff < dp[i][j - 1].ff)
        dp[i][j] = dp[i][j - 1];
      if (s[i] == t[j]) {
        if (i > 0 && j > 0)
          dp[i][j].ff = dp[i - 1][j - 1].ff + 1;
        else
          dp[i][j].ff = 1;
        dp[i][j].ss = {i, j};
      }
    }
  }
  pii now = {s.length() - 1, t.length() - 1};
  string answer;
  while (now.ff != -1 && now.ss != -1) {
    now = dp[now.ff][now.ss].ss;
    if (now.ff == -1 || now.ss == -1)
      break;
    answer += s[now.ff];
    now.ff -= 1;
    now.ss -= 1;
  }
  reverse(answer.begin(), answer.end());
  cout << answer;

  return 0;
}
Problem p03165 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;

const int N = (1e3) + 3;
int memo[N][N];
string s1, s2;
int n, m;

void lcs(int n, int m) {
  for (int i = 0; i <= n; ++i) {
    memo[i][0] = 0;
  }
  for (int i = 0; i <= m; ++i) {
    memo[0][i] = 0;
  }
  for (int i = 1; i <= n; ++i) {
    for (int j = 1; j <= m; ++j) {
      if (s1[i - 1] == s2[j - 1]) {
        memo[i][j] = 1 + memo[i - 1][j - 1];
      } else {
        memo[i][j] = max(memo[i - 1][j], memo[i][j - 1]);
      }
    }
  }
}

void printLcs(int n, int m) {
  if (n == 0 or m == 0)
    return;
  if (s1[n - 1] == s2[m - 1]) {
    printLcs(n - 1, m - 1);
    printf("%c", s1[n - 1]);
  } else {
    if (memo[n][m - 1] > memo[n - 1][m])
      printLcs(n, m - 1);
    else
      printLcs(n - 1, m);
  }
}

int main() {
  getline(cin, s1);
  getline(cin, s2);
  int n = s1.size();
  int m = s2.size();
  lcs(n, m);
  printLcs(n, m);
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;

const int N = (3e3) + 3;
int memo[N][N];
string s1, s2;
int n, m;

void lcs(int n, int m) {
  for (int i = 0; i <= n; ++i) {
    memo[i][0] = 0;
  }
  for (int i = 0; i <= m; ++i) {
    memo[0][i] = 0;
  }
  for (int i = 1; i <= n; ++i) {
    for (int j = 1; j <= m; ++j) {
      if (s1[i - 1] == s2[j - 1]) {
        memo[i][j] = 1 + memo[i - 1][j - 1];
      } else {
        memo[i][j] = max(memo[i - 1][j], memo[i][j - 1]);
      }
    }
  }
}

void printLcs(int n, int m) {
  if (n == 0 or m == 0)
    return;
  if (s1[n - 1] == s2[m - 1]) {
    printLcs(n - 1, m - 1);
    printf("%c", s1[n - 1]);
  } else {
    if (memo[n][m - 1] > memo[n - 1][m])
      printLcs(n, m - 1);
    else
      printLcs(n - 1, m);
  }
}

int main() {
  getline(cin, s1);
  getline(cin, s2);
  int n = s1.size();
  int m = s2.size();
  lcs(n, m);
  printLcs(n, m);
  return 0;
}
Problem p03165 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define re reverse
using namespace std;
signed main() {
  string b, c, a, d;
  cin >> b >> c;
  int lcs[1005][1005] = {};
  for (int j = 1; j <= b.size(); j++) {
    for (int k = 1; k <= c.size(); k++) {
      if (b[j - 1] == c[k - 1])
        lcs[j][k] = lcs[j - 1][k - 1] + 1;
      else
        lcs[j][k] = max(lcs[j - 1][k], lcs[j][k - 1]);
    }
  }
  int x = c.size(), y = b.size();
  while (true) {
    if (x == 0 || y == 0)
      break;
    if (lcs[y - 1][x] == lcs[y][x - 1] && lcs[y - 1][x - 1] == lcs[y - 1][x] &&
        lcs[y][x] > lcs[y][x - 1]) {
      a += c[x - 1];
      x--, y--;
    } else {
      if (lcs[y - 1][x] > lcs[y][x - 1])
        y--;
      else
        x--;
    }
  }
  reverse(a.begin(), a.end());
  cout << a << "\n";
}


Fixed submission:
#include <bits/stdc++.h>
#define re reverse
using namespace std;
signed main() {
  string b, c, a, d;
  cin >> b >> c;
  int lcs[3005][3005] = {};
  for (int j = 1; j <= b.size(); j++) {
    for (int k = 1; k <= c.size(); k++) {
      if (b[j - 1] == c[k - 1])
        lcs[j][k] = lcs[j - 1][k - 1] + 1;
      else
        lcs[j][k] = max(lcs[j - 1][k], lcs[j][k - 1]);
    }
  }
  int x = c.size(), y = b.size();
  while (true) {
    if (x == 0 || y == 0)
      break;
    if (lcs[y - 1][x] == lcs[y][x - 1] && lcs[y - 1][x - 1] == lcs[y - 1][x] &&
        lcs[y][x] > lcs[y][x - 1]) {
      a += c[x - 1];
      x--, y--;
    } else {
      if (lcs[y - 1][x] > lcs[y][x - 1])
        y--;
      else
        x--;
    }
  }
  reverse(a.begin(), a.end());
  cout << a << "\n";
}
Problem p03165 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define endl '\n'
#define vi vector<int>
#define vb vector<bool>
#define pii pair<int, int>
#define mod 1000000007
#define ss second
#define ff first
#define vpii vector<pii>
#define vvi vector<vi>
#define pb push_back
#define vs vector<string>
int power(int a, int n) {
  int res = 1;
  while (n) {
    if (n % 2)
      res *= a;
    a *= a;
    n /= 2;
  }
  return res;
}
int func(int i, int j, string &s, string &t, vvi &dp) {
  if (i == -1 || j == -1)
    return 0;
  if (!dp[i][j])
    return dp[i][j];
  else if (s[i] == t[j]) {
    dp[i][j] = func(i - 1, j - 1, s, t, dp) + 1;
    return dp[i][j];
  } else {
    dp[i][j] = max(func(i, j - 1, s, t, dp), func(i - 1, j, s, t, dp));
    return dp[i][j];
  }
}
void reverse(string str) {
  for (int i = str.length() - 1; i >= 0; i--)
    cout << str[i];
}
int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  string s, t;
  cin >> s >> t;
  vvi dp(s.size(), vi(t.size(), -1));
  int n = s.size(), m = t.size();
  func(n - 1, m - 1, s, t, dp);
  int i = n - 1, j = m - 1;
  string s1 = "";
  while (i >= 0 && j >= 0) {
    if (s[i] == t[j]) {
      s1 += s[i];
      if (i == 0 && j == 0)
        break;
      i--;
      j--;
    }

    else {
      if (i == 0 && j == 0)
        break;
      if (i > 0 && dp[i][j] == dp[i - 1][j]) {
        i--;
      } else if (j > 0 && dp[i][j] == dp[i][j - 1]) {
        j--;
      }
    }
  }
  reverse(s1);
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define endl '\n'
#define vi vector<int>
#define vb vector<bool>
#define pii pair<int, int>
#define mod 1000000007
#define ss second
#define ff first
#define vpii vector<pii>
#define vvi vector<vi>
#define pb push_back
#define vs vector<string>
int power(int a, int n) {
  int res = 1;
  while (n) {
    if (n % 2)
      res *= a;
    a *= a;
    n /= 2;
  }
  return res;
}
int func(int i, int j, string &s, string &t, vvi &dp) {
  if (i == -1 || j == -1)
    return 0;
  if (dp[i][j] != -1)
    return dp[i][j];
  else if (s[i] == t[j]) {
    dp[i][j] = func(i - 1, j - 1, s, t, dp) + 1;
    return dp[i][j];
  } else {
    dp[i][j] = max(func(i, j - 1, s, t, dp), func(i - 1, j, s, t, dp));
    return dp[i][j];
  }
}
void reverse(string str) {
  for (int i = str.length() - 1; i >= 0; i--)
    cout << str[i];
}
int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  string s, t;
  cin >> s >> t;
  vvi dp(s.size(), vi(t.size(), -1));
  int n = s.size(), m = t.size();
  func(n - 1, m - 1, s, t, dp);
  int i = n - 1, j = m - 1;
  string s1 = "";
  while (i >= 0 && j >= 0) {
    if (s[i] == t[j]) {
      s1 += s[i];
      if (i == 0 && j == 0)
        break;
      i--;
      j--;
    }

    else {
      if (i == 0 && j == 0)
        break;
      if (i > 0 && dp[i][j] == dp[i - 1][j]) {
        i--;
      } else if (j > 0 && dp[i][j] == dp[i][j - 1]) {
        j--;
      }
    }
  }
  reverse(s1);
  return 0;
}
Problem p03165 (C++)

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

int dp[100][100];
string getLCS(string &s, string &t, int len) {
  int i = 0, j = 0;
  string LCS;
  while (len > 0) {
    if (s[i] == t[j]) {
      LCS.push_back(s[i]);
      i++;
      j++;
      len--;
    } else {
      if (dp[i][j + 1] > dp[i + 1][j])
        j++;

      else
        i++;
    }
  }
  return LCS;
}
int lenLCS(string &s, string &t, int i, int j) // topdown lcs
{
  if (i >= s.length() || j >= t.length())
    return 0;

  if (dp[i][j] != -1)
    return dp[i][j];

  if (s[i] == t[j]) {
    dp[i][j] = 1 + lenLCS(s, t, i + 1, j + 1);

    return dp[i][j];

  }

  else {

    dp[i][j] = max(lenLCS(s, t, i + 1, j), lenLCS(s, t, i, j + 1));

    return dp[i][j];
  }
}
string solve(string s, string t) {
  memset(dp, -1, sizeof dp);
  int len = lenLCS(s, t, 0, 0);
  return getLCS(s, t, len);
}

int main() {

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

  cout << solve(s, t) << "\n";
  return 0;
}


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

int dp[3005][3005];
string getLCS(string &s, string &t, int len) {
  int i = 0, j = 0;
  string LCS;
  while (len > 0) {
    if (s[i] == t[j]) {
      LCS.push_back(s[i]);
      i++;
      j++;
      len--;
    } else {
      if (dp[i][j + 1] > dp[i + 1][j])
        j++;

      else
        i++;
    }
  }
  return LCS;
}
int lenLCS(string &s, string &t, int i, int j) // topdown lcs
{
  if (i >= s.length() || j >= t.length())
    return 0;

  if (dp[i][j] != -1)
    return dp[i][j];

  if (s[i] == t[j]) {
    dp[i][j] = 1 + lenLCS(s, t, i + 1, j + 1);

    return dp[i][j];

  }

  else {

    dp[i][j] = max(lenLCS(s, t, i + 1, j), lenLCS(s, t, i, j + 1));

    return dp[i][j];
  }
}
string solve(string s, string t) {
  memset(dp, -1, sizeof dp);
  int len = lenLCS(s, t, 0, 0);
  return getLCS(s, t, len);
}

int main() {

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

  cout << solve(s, t) << "\n";
  return 0;
}
Problem p03165 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define INF (1LL << 60)
#define int long long
#define magic 100000
using namespace std;

int dp[3001][3001]; // i, j
// dp[i][j] : lcs of s[1:i] and t[1:j]

string ans;
int32_t main() {
  string s, t;
  cin >> s >> t;
  for (int i = 0; i < (int)s.size(); i++) {
    for (int j = 0; j < (int)t.size(); j++) {
      dp[i + 1][j + 1] = max(dp[i + 1][j], dp[i][j + 1]);
      if (s[i] == t[j]) {
        dp[i + 1][j + 1] = max(dp[i + 1][j + 1], dp[i][j] + 1);
      }
    }
  }
  int i = s.size() - 1, j = t.size() - 1;
  while (i != -1 || j != -1) {
    if (s[i] == t[j] && dp[i + 1][j + 1] == dp[i][j] + 1) {
      ans.push_back(s[i]);
      i--;
      j--;
    } else if (dp[i + 1][j + 1] == dp[i + 1][j]) {
      j--;
    } else {
      i--;
    }
  }
  for (int i = (int)ans.size() - 1; i >= 0; i--) {
    cout << ans[i];
  }
}

Fixed submission:
#include <bits/stdc++.h>
#define INF (1LL << 60)
#define int long long
#define magic 100000
using namespace std;

int dp[3001][3001]; // i, j
// dp[i][j] : lcs of s[1:i] and t[1:j]

string ans;
int32_t main() {
  string s, t;
  cin >> s >> t;
  for (int i = 0; i < (int)s.size(); i++) {
    for (int j = 0; j < (int)t.size(); j++) {
      dp[i + 1][j + 1] = max(dp[i + 1][j], dp[i][j + 1]);
      if (s[i] == t[j]) {
        dp[i + 1][j + 1] = max(dp[i + 1][j + 1], dp[i][j] + 1);
      }
    }
  }
  int i = s.size() - 1, j = t.size() - 1;
  while (i != -1 && j != -1) {
    if (s[i] == t[j] && dp[i + 1][j + 1] == dp[i][j] + 1) {
      ans.push_back(s[i]);
      i--;
      j--;
    } else if (dp[i + 1][j + 1] == dp[i + 1][j]) {
      j--;
    } else {
      i--;
    }
  }
  for (int i = (int)ans.size() - 1; i >= 0; i--) {
    cout << ans[i];
  }
}
Problem p03165 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define mmax(a, b, c) max(a, max(b, c))
#define mmin(a, b, c) min(a, min(b, c))
#define M 1000000007
#define V vector
#define F(a, b, c) for (a = b; a <= c; ++a)
#define I cin
#define O cout
#define pb push_back
#define all(x) x.begin(), x.end()
#define ff first
#define ss second

int main() {
  I.sync_with_stdio(false);
  I.tie(0);
  ll ls, lt, i, j;
  string s, t;
  I >> s >> t;
  ls = s.size();
  lt = t.size();
  vector<vector<ll>> dp(lt + 1, vector<ll>(ls + 1, 0));
  string a;
  F(i, 1, lt) {
    F(j, 1, ls) {
      if (s[j - 1] == t[i - 1])
        dp[i][j] = dp[i - 1][j - 1] + 1;
      else
        dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]);
    }
  }
  j = ls;
  while (1) {
    while (1) {
      if (s[j - 1] == t[i - 1]) {
        a = s[j - 1] + a;
        --i;
        --j;
      } else if (dp[i][j - 1] > dp[i - 1][j]) {
        --j;
      } else if (dp[i][j - 1] <= dp[i - 1][j]) {
        --i;
      }
      if (j == 0 || i == 0)
        break;
    }
    if (j == 0 || i == 0)
      break;
  }
  O << a << '\n';
  // O<<dp[lt][ls]<<'\n';
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define mmax(a, b, c) max(a, max(b, c))
#define mmin(a, b, c) min(a, min(b, c))
#define M 1000000007
#define V vector
#define F(a, b, c) for (a = b; a <= c; ++a)
#define I cin
#define O cout
#define pb push_back
#define all(x) x.begin(), x.end()
#define ff first
#define ss second

int main() {
  I.sync_with_stdio(false);
  I.tie(0);
  ll ls, lt, i, j;
  string s, t;
  I >> s >> t;
  ls = s.size();
  lt = t.size();
  vector<vector<ll>> dp(lt + 1, vector<ll>(ls + 1, 0));
  string a;
  F(i, 1, lt) {
    F(j, 1, ls) {
      if (s[j - 1] == t[i - 1])
        dp[i][j] = dp[i - 1][j - 1] + 1;
      else
        dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]);
    }
  }
  i = lt;
  j = ls;
  while (1) {
    while (1) {
      if (s[j - 1] == t[i - 1]) {
        a = s[j - 1] + a;
        --i;
        --j;
      } else if (dp[i][j - 1] > dp[i - 1][j]) {
        --j;
      } else if (dp[i][j - 1] <= dp[i - 1][j]) {
        --i;
      }
      if (j == 0 || i == 0)
        break;
    }
    if (j == 0 || i == 0)
      break;
  }
  O << a << '\n';
  // O<<dp[lt][ls]<<'\n';
  return 0;
}
Problem p03165 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define mod 1000000007
#define pb push_back
typedef long long ll;
typedef long double ld;

int main() {
  // ios_base::sync_with_stdio(false);
  // cin.tie(NULL);

  ll i, j, N = 3e2 + 10;
  ll dp[N][N];
  for (i = 0; i < N; i++) {
    for (j = 0; j < N; j++) {
      dp[i][j] = -1;
    }
  }
  for (i = 0; i < N; i++) {
    dp[i][0] = dp[0][i] = 0;
  }

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

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

  if (dp[s.size()][t.size()] == 0) {
    cout << "\n";
    return 0;
  }

  for (i = s.size(), j = t.size(); i > 0 && j > 0 && dp[i][j] != 0;) {

    if (dp[i - 1][j] == dp[i][j])
      i--;
    else if (dp[i][j - 1] == dp[i][j])
      j--;
    else if (dp[i - 1][j] != dp[i][j] && dp[i][j - 1] != dp[i][j]) {
      f.pb(t[j - 1]);
      i--, j--;
    }
  }

  reverse(f.begin(), f.end());
  cout << f << "\n";

  /*for (i = 0; i <= s.size(); i++)
  {
      for (j = 0; j <= t.size(); j++)
          cout << dp[i][j] << " ";
      cout << "\n";
  }
  cout << dp[s.size()][t.size()] << "\n";*/
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define mod 1000000007
#define pb push_back
typedef long long ll;
typedef long double ld;

int main() {
  // ios_base::sync_with_stdio(false);
  // cin.tie(NULL);

  ll i, j, N = 3e3 + 10;
  ll dp[N][N];
  for (i = 0; i < N; i++) {
    for (j = 0; j < N; j++) {
      dp[i][j] = -1;
    }
  }
  for (i = 0; i < N; i++) {
    dp[i][0] = dp[0][i] = 0;
  }

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

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

  if (dp[s.size()][t.size()] == 0) {
    cout << "\n";
    return 0;
  }

  for (i = s.size(), j = t.size(); i > 0 && j > 0 && dp[i][j] != 0;) {

    if (dp[i - 1][j] == dp[i][j])
      i--;
    else if (dp[i][j - 1] == dp[i][j])
      j--;
    else if (dp[i - 1][j] != dp[i][j] && dp[i][j - 1] != dp[i][j]) {
      f.pb(t[j - 1]);
      i--, j--;
    }
  }

  reverse(f.begin(), f.end());
  cout << f << "\n";

  /*for (i = 0; i <= s.size(); i++)
  {
      for (j = 0; j <= t.size(); j++)
          cout << dp[i][j] << " ";
      cout << "\n";
  }
  cout << dp[s.size()][t.size()] << "\n";*/
  return 0;
}
Problem p03165 (C++)

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

int arr[1001][1001];

int main() {
  string s1, s2;
  cin >> s1 >> s2;
  int n1 = s1.length(), n2 = s2.length();
  for (int i = 0; i < n1 + 1; i++) {
    for (int j = 0; j < n2 + 1; j++) {
      if (i == 0 || j == 0)
        arr[i][j] = 0;
      else {
        if (s1[i - 1] == s2[j - 1])
          arr[i][j] = arr[i - 1][j - 1] + 1;
        else {
          if (arr[i][j - 1] < arr[i - 1][j])
            arr[i][j] = arr[i - 1][j];
          else
            arr[i][j] = arr[i][j - 1];
        }
      }
      //	cout<<arr[i][j]<<" ";
    }
    // cout<<endl;
  }
  int i = n1, j = n2;
  int index = arr[n1][n2] - 1;
  char ans[arr[n1][n2] + 1];
  ans[arr[n1][n2]] = '\0';
  while (i > 0 && j > 0) {
    if (arr[i][j] == arr[i][j - 1])
      j--;
    else if (arr[i][j] == arr[i - 1][j])
      i--;

    else {
      // cout<<s1[i-1]<<endl;
      ans[index--] = s1[i - 1];
      i--;
      j--;
    }
  }
  cout << ans << endl;
}

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

int arr[3001][3001];

int main() {
  string s1, s2;
  cin >> s1 >> s2;
  int n1 = s1.length(), n2 = s2.length();
  for (int i = 0; i < n1 + 1; i++) {
    for (int j = 0; j < n2 + 1; j++) {
      if (i == 0 || j == 0)
        arr[i][j] = 0;
      else {
        if (s1[i - 1] == s2[j - 1])
          arr[i][j] = arr[i - 1][j - 1] + 1;
        else {
          if (arr[i][j - 1] < arr[i - 1][j])
            arr[i][j] = arr[i - 1][j];
          else
            arr[i][j] = arr[i][j - 1];
        }
      }
      //	cout<<arr[i][j]<<" ";
    }
    // cout<<endl;
  }
  int i = n1, j = n2;
  int index = arr[n1][n2] - 1;
  char ans[arr[n1][n2] + 1];
  ans[arr[n1][n2]] = '\0';
  while (i > 0 && j > 0) {
    if (arr[i][j] == arr[i][j - 1])
      j--;
    else if (arr[i][j] == arr[i - 1][j])
      i--;

    else {
      // cout<<s1[i-1]<<endl;
      ans[index--] = s1[i - 1];
      i--;
      j--;
    }
  }
  cout << ans << endl;
}
Problem p03165 (C++)

Buggy submission (status: Runtime Error):
// Abhigyan's Code:

// YOU DON'T DARE TO COPY :)

#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef unsigned long long int ull;
#define checker cout << "CHECKED"
#define modulo 1000000007
ll dp[1000][1000];

void lcs(string x, string y) {
  ll s1 = x.size();
  ll s2 = y.size();

  for (ll i = 0; i <= s1; i++) {
    for (ll j = 0; j <= s2; j++) {
      if (i == 0 || j == 0)
        dp[i][j] = 0;
      else if (x[i - 1] == y[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 main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

  string x, y;

  cin >> x >> y;

  ll i = x.size();
  ll j = y.size();

  lcs(x, y);

  vector<char> st(dp[x.size()][y.size()]);

  ll curr;

  curr = dp[x.size()][y.size()] - 1;

  // cout<<curr<<endl;
  while (i > 0 && j > 0 && curr >= 0) {
    if (x[i - 1] == y[j - 1]) {
      st[curr] = x[i - 1];
      i--;
      j--;
      curr--;
    } else if (dp[i - 1][j] > dp[i][j - 1]) {
      i--;
    } else {
      j--;
    }
  }

  for (auto z : st)
    cout << z;

  return 0;
}

Fixed submission:
// Abhigyan's Code:

// YOU DON'T DARE TO COPY :)

#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef unsigned long long int ull;
#define checker cout << "CHECKED"
#define modulo 1000000007
ll dp[10000][10000];

void lcs(string x, string y) {
  ll s1 = x.size();
  ll s2 = y.size();

  for (ll i = 0; i <= s1; i++) {
    for (ll j = 0; j <= s2; j++) {
      if (i == 0 || j == 0)
        dp[i][j] = 0;
      else if (x[i - 1] == y[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 main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

  string x, y;

  cin >> x >> y;

  ll i = x.size();
  ll j = y.size();

  lcs(x, y);

  vector<char> st(dp[x.size()][y.size()]);

  ll curr;

  curr = dp[x.size()][y.size()] - 1;

  // cout<<curr<<endl;
  while (i > 0 && j > 0 && curr >= 0) {
    if (x[i - 1] == y[j - 1]) {
      st[curr] = x[i - 1];
      i--;
      j--;
      curr--;
    } else if (dp[i - 1][j] > dp[i][j - 1]) {
      i--;
    } else {
      j--;
    }
  }

  for (auto z : st)
    cout << z;

  return 0;
}
Problem p03165 (C++)

Buggy submission (status: Runtime Error):
/*BINH :))*/
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef unsigned long long llu;
#define IO                                                                     \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define f first
#define s second
#define mp make_pair
#define pb push_back
#define ep emplace_back
#define sz(a) (int)a.size()
#define FOR(x, a, b) for (int x = a; x <= b; x++)
#define FOD(x, a, b) for (int x = a; x >= b; x--)
#define REP(x, a, b) for (int x = a; x < b; x++)
#define RED(x, a, b) for (int x = a; x > b; x--)
#define EL cout << endl
#define maxn 1005

void init() {}
string a, b;
int f[1000][1000];
int main() {
  IO init();
  cin >> a >> b;
  int m = a.size(), n = b.size();
  a = '0' + a;
  b = '0' + b;
  FOR(i, 1, m) {
    FOR(j, 1, n) {
      if (a[i] == b[j])
        f[i][j] = f[i - 1][j - 1] + 1;
      else {
        f[i][j] = max(f[i - 1][j], f[i][j - 1]);
      }
    }
  }
  int i = m, j = n;
  string res;
  while (i > 0 && j > 0) {
    if (a[i] == b[j]) {
      res = a[i] + res;
      i--;
      j--;

    } else {

      if (f[i - 1][j] > f[i][j - 1]) {
        i--;
      } else
        j--;
    }
  }
  cout << res;
}

Fixed submission:
/*BINH :))*/
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef unsigned long long llu;
#define IO                                                                     \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define f first
#define s second
#define mp make_pair
#define pb push_back
#define ep emplace_back
#define sz(a) (int)a.size()
#define FOR(x, a, b) for (int x = a; x <= b; x++)
#define FOD(x, a, b) for (int x = a; x >= b; x--)
#define REP(x, a, b) for (int x = a; x < b; x++)
#define RED(x, a, b) for (int x = a; x > b; x--)
#define EL cout << endl
#define maxn 1005

void init() {}
string a, b;
int f[3001][3001];
int main() {
  IO init();
  cin >> a >> b;
  int m = a.size(), n = b.size();
  a = '0' + a;
  b = '0' + b;
  FOR(i, 1, m) {
    FOR(j, 1, n) {
      if (a[i] == b[j])
        f[i][j] = f[i - 1][j - 1] + 1;
      else {
        f[i][j] = max(f[i - 1][j], f[i][j - 1]);
      }
    }
  }
  int i = m, j = n;
  string res;
  while (i > 0 && j > 0) {
    if (a[i] == b[j]) {
      res = a[i] + res;
      i--;
      j--;

    } else {

      if (f[i - 1][j] > f[i][j - 1]) {
        i--;
      } else
        j--;
    }
  }
  cout << res;
}
Problem p03165 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define pii pair<int, int>
#define f first
#define s second
#define p 1000000007
using namespace std;
string s, t;
int a[3000][3000];
int b[3000][3000];
int lcs(int i, int j) {
  if (i == -1 || j == -1)
    return 0;
  if (s[i] == t[j]) {
    b[i][j] = 1;
    return a[i][j] = lcs(i - 1, j - 1) + 1;
  }
  if (lcs(i - 1, j) > lcs(i, j - 1)) {
    b[i][j] = 2;
    return a[i][j] = lcs(i - 1, j);
  }
  b[i][j] = 3;
  return a[i][j] = lcs(i, j - 1);
}
int main() {

  cin >> s >> t;
  memset(a, -1, sizeof(a));
  lcs(s.size() - 1, t.size() - 1);
  string s1;
  int i = s.size() - 1, j = t.size() - 1;
  while (i >= 0 && j >= 0) {
    if (b[i][j] == 1) {
      s1.push_back(s[i]);
      i--;
      j--;
    } else if (b[i][j] == 2) {
      i--;
    } else {
      j--;
    }
  }
  reverse(s1.begin(), s1.end());
  cout << s1;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define pii pair<int, int>
#define f first
#define s second
#define p 1000000007
using namespace std;
string s, t;
int a[3000][3000];
int b[3000][3000];
int lcs(int i, int j) {
  if (i == -1 || j == -1)
    return 0;
  if (a[i][j] != -1)
    return a[i][j];
  if (s[i] == t[j]) {
    b[i][j] = 1;
    return a[i][j] = lcs(i - 1, j - 1) + 1;
  }
  if (lcs(i - 1, j) > lcs(i, j - 1)) {
    b[i][j] = 2;
    return a[i][j] = lcs(i - 1, j);
  }
  b[i][j] = 3;
  return a[i][j] = lcs(i, j - 1);
}
int main() {

  cin >> s >> t;
  memset(a, -1, sizeof(a));
  lcs(s.size() - 1, t.size() - 1);
  string s1;
  int i = s.size() - 1, j = t.size() - 1;
  while (i >= 0 && j >= 0) {
    if (b[i][j] == 1) {
      s1.push_back(s[i]);
      i--;
      j--;
    } else if (b[i][j] == 2) {
      i--;
    } else {
      j--;
    }
  }
  reverse(s1.begin(), s1.end());
  cout << s1;
  return 0;
}
Problem p03165 (C++)

Buggy submission (status: Runtime Error):
#define _CRT_SECURE_NO_WARNINGS
#include <bits/stdc++.h>
#include <unordered_map>
#include <unordered_set>
using namespace std;
#define all(v) ((v).begin()), ((v).end())
#define sz(v) ((int)((v).size()))
#define clr(v, d) memset(v, d, sizeof(v))
#define rep(i, v) for (int i = 0; i < sz(v); ++i)
#define lp(i, n) for (int i = 0; i < (int)(n); ++i)
typedef long long ll;
const double EPS = (1e-7);
int dcmp(double x, double y) { return fabs(x - y) <= EPS ? 0 : x < y ? -1 : 1; }
#define pb push_back
#define P(x) cout << #x << " = { " << x << " }\n"
clock_t T;
#define ctime cerr << "Time : " << double(clock() - T) / CLOCKS_PER_SEC << endl
void GOAT() {
  std::ios_base::sync_with_stdio(0);
  cin.tie(NULL);
  cout.tie(NULL);
#ifndef ONLINE_JUDGE
  freopen("in.txt", "r", stdin);
  // freopen("output.txt", "w	", stdout);
#else
#endif
}
string a, b, ans = "";
int mem[3001][3001];
int lcs(int i, int j) {
  if (i >= sz(a) || j >= sz(b))
    return 0;
  int &ret = mem[i][j];
  if (~ret)
    return ret;
  ret = -1;
  if (a[i] == b[j]) {
    // += a[i];
    return ret = lcs(i + 1, j + 1) + 1;
  }
  int ch1 = lcs(i, j + 1);
  int ch2 = lcs(i + 1, j);
  return ret = max(ch1, ch2);
}
void solve(int i, int j) {
  if (i >= sz(a) || j >= sz(b))
    return;
  if (a[i] == b[j]) {
    ans += a[i];
    solve(i + 1, j + 1);
  } else {
    if (mem[i + 1][j] >= mem[i][j + 1])
      solve(i + 1, j);
    else
      solve(i, j + 1);
  }
}
int main() {
  GOAT();
  clr(mem, -1);
  cin >> a >> b;
  lcs(0, 0);
  solve(0, 0);
  cout << ans << endl;
}


Fixed submission:
#define _CRT_SECURE_NO_WARNINGS
#include <bits/stdc++.h>
#include <unordered_map>
#include <unordered_set>
using namespace std;
#define all(v) ((v).begin()), ((v).end())
#define sz(v) ((int)((v).size()))
#define clr(v, d) memset(v, d, sizeof(v))
#define rep(i, v) for (int i = 0; i < sz(v); ++i)
#define lp(i, n) for (int i = 0; i < (int)(n); ++i)
typedef long long ll;
const double EPS = (1e-7);
int dcmp(double x, double y) { return fabs(x - y) <= EPS ? 0 : x < y ? -1 : 1; }
#define pb push_back
#define P(x) cout << #x << " = { " << x << " }\n"
clock_t T;
#define ctime cerr << "Time : " << double(clock() - T) / CLOCKS_PER_SEC << endl
void GOAT() {
  std::ios_base::sync_with_stdio(0);
  cin.tie(NULL);
  cout.tie(NULL);
#ifndef ONLINE_JUDGE
  freopen("in.txt", "r", stdin);
  // freopen("output.txt", "w	", stdout);
#else
#endif
}
string a, b, ans = "";
int mem[3001][3001];
int lcs(int i, int j) {
  if (i >= sz(a) || j >= sz(b))
    return 0;
  int &ret = mem[i][j];
  if (~ret)
    return ret;
  ret = -1;
  if (a[i] == b[j]) {
    // += a[i];
    return ret = lcs(i + 1, j + 1) + 1;
  }
  int ch1 = lcs(i, j + 1);
  int ch2 = lcs(i + 1, j);
  return ret = max(ch1, ch2);
}
void solve(int i, int j) {
  if (i >= sz(a) || j >= sz(b))
    return;
  if (a[i] == b[j]) {
    ans += a[i];
    solve(i + 1, j + 1);
  } else {
    if (mem[i + 1][j] >= mem[i][j + 1])
      solve(i + 1, j);
    else
      solve(i, j + 1);
  }
}
int main() {
  // GOAT();
  clr(mem, -1);
  cin >> a >> b;
  lcs(0, 0);
  solve(0, 0);
  cout << ans << endl;
}
Problem p03165 (C++)

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

using namespace std;
typedef long long ll;
typedef long double ld;
typedef vector<ll> vll;

const ll inf = 1e18 + 7;
const int maxn = 1e3 + 10;
const int maxq = 5e2 + 10;
const int alf = 26;
const ll dlm = 1e9 + 7;
const int del = 998244353;
const int eps = 1e-7;

int dp[maxn][maxn];
pair<int, int> path[maxn][maxn];

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  string z1, z2;
  cin >> z1 >> z2;
  string s = "", t = "";
  s += " ";
  s += z1;
  t += " ";
  t += z2;
  for (int i = 1; i < s.size(); i++) {
    for (int j = 1; j < t.size(); j++) {
      if (s[i] == t[j]) {
        dp[i][j] = dp[i - 1][j - 1] + 1;
        path[i][j] = {i - 1, j - 1};
      } else {
        if (dp[i - 1][j] > dp[i][j - 1]) {
          dp[i][j] = dp[i - 1][j];
          path[i][j] = {i - 1, j};
        } else {
          dp[i][j] = dp[i][j - 1];
          path[i][j] = {i, j - 1};
        }
      }
    }
  }
  string rre = "";
  string re = "";
  int i = s.size() - 1, j = t.size() - 1;
  while (i > 0 && j > 0) {
    int x = path[i][j].first;
    int y = path[i][j].second;
    if (x == (i - 1) && y == (j - 1)) {
      rre += s[i];
    }
    i = x;
    j = y;
  }
  for (int z = rre.size() - 1; z >= 0; z--) {
    re += (rre[z]);
  }
  cout << re << endl;
  return 0;
}


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

using namespace std;
typedef long long ll;
typedef long double ld;
typedef vector<ll> vll;

const ll inf = 1e18 + 7;
const int maxn = 3e3 + 10;
const int maxq = 5e2 + 10;
const int alf = 26;
const ll dlm = 1e9 + 7;
const int del = 998244353;
const int eps = 1e-7;

int dp[maxn][maxn];
pair<int, int> path[maxn][maxn];

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  string z1, z2;
  cin >> z1 >> z2;
  string s = "", t = "";
  s += " ";
  s += z1;
  t += " ";
  t += z2;
  for (int i = 1; i < s.size(); i++) {
    for (int j = 1; j < t.size(); j++) {
      if (s[i] == t[j]) {
        dp[i][j] = dp[i - 1][j - 1] + 1;
        path[i][j] = {i - 1, j - 1};
      } else {
        if (dp[i - 1][j] > dp[i][j - 1]) {
          dp[i][j] = dp[i - 1][j];
          path[i][j] = {i - 1, j};
        } else {
          dp[i][j] = dp[i][j - 1];
          path[i][j] = {i, j - 1};
        }
      }
    }
  }
  string rre = "";
  string re = "";
  int i = s.size() - 1, j = t.size() - 1;
  while (i > 0 && j > 0) {
    int x = path[i][j].first;
    int y = path[i][j].second;
    if (x == (i - 1) && y == (j - 1)) {
      rre += s[i];
    }
    i = x;
    j = y;
  }
  for (int z = rre.size() - 1; z >= 0; z--) {
    re += (rre[z]);
  }
  cout << re << endl;
  return 0;
}
Problem p03165 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define ll long long
#define ld long double
#define all(x) (x.begin()), (x.end())
#define sz(x) ((ll)x.size())
#define FAST_IO                                                                \
  ios_base ::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
using namespace std;
#ifdef LOCAL
#include "uj.h"
#endif

const ll oo = 1e16;
const int maxn = 1e3 + 10;
string s, t;

int N, M;
ll dp[maxn][maxn];

ll Brute(int i, int j) {
  if (i == N || j == M)
    return 0;
  ll &ans = dp[i][j];
  if (ans != -1)
    return ans;

  if (s[i] == t[j])
    return ans = Brute(i + 1, j + 1) + 1;
  ll a = Brute(i + 1, j);
  ll b = Brute(i, j + 1);
  return ans = max(a, b);
}

string res;
void build(int i, int j) {
  if (i == N || j == M)
    return;

  if (s[i] == t[j]) {
    res += s[i];
    build(i + 1, j + 1);
    return;
  }

  ll optimal = Brute(i, j);
  ll a = Brute(i + 1, j);

  if (optimal == a)
    build(i + 1, j);
  else
    build(i, j + 1);
}

void test_case() {
  cin >> s >> t;
  N = sz(s), M = sz(t);
  memset(dp, -1, sizeof(dp));
  ll check = Brute(0, 0);
  // debug() << owo(check);
  build(0, 0);

  cout << res << endl;
}

int main() {
  FAST_IO;
  int tt;
  // cin>>tt;
  tt = 1;
  while (tt--) {
    test_case();
  }
#ifndef ONLINE_JUDGE
  cerr << "Running Time: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.\n";
#endif
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define ll long long
#define ld long double
#define all(x) (x.begin()), (x.end())
#define sz(x) ((ll)x.size())
#define FAST_IO                                                                \
  ios_base ::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
using namespace std;
#ifdef LOCAL
#include "uj.h"
#endif

const ll oo = 1e16;
const int maxn = 1e4;
string s, t;

int N, M;
ll dp[maxn][maxn];

ll Brute(int i, int j) {
  if (i == N || j == M)
    return 0;
  ll &ans = dp[i][j];
  if (ans != -1)
    return ans;

  if (s[i] == t[j])
    return ans = Brute(i + 1, j + 1) + 1;
  ll a = Brute(i + 1, j);
  ll b = Brute(i, j + 1);
  return ans = max(a, b);
}

string res;
void build(int i, int j) {
  if (i == N || j == M)
    return;

  if (s[i] == t[j]) {
    res += s[i];
    build(i + 1, j + 1);
    return;
  }

  ll optimal = Brute(i, j);
  ll a = Brute(i + 1, j);

  if (optimal == a)
    build(i + 1, j);
  else
    build(i, j + 1);
}

void test_case() {
  cin >> s >> t;
  N = sz(s), M = sz(t);
  memset(dp, -1, sizeof(dp));
  ll check = Brute(0, 0);
  // debug() << owo(check);
  build(0, 0);

  cout << res << endl;
}

int main() {
  FAST_IO;
  int tt;
  // cin>>tt;
  tt = 1;
  while (tt--) {
    test_case();
  }
#ifndef ONLINE_JUDGE
  cerr << "Running Time: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.\n";
#endif
  return 0;
}
Problem p03165 (C++)

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

using namespace std;

typedef long long ll;

// freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
#define M 100

int m, n;
int MAT[M][M];
char str1[M], str2[M];

int LCS() {
  for (int i = 0; i <= m; i++) {
    for (int j = 0; j <= n; j++) {
      if (i == 0 || j == 0) {
        MAT[i][j] = 0;
      } else if (str1[i - 1] == str2[j - 1]) {
        MAT[i][j] = 1 + MAT[i - 1][j - 1];
      } else {
        MAT[i][j] = max(MAT[i - 1][j], MAT[i][j - 1]);
      }
    }
  }
  return MAT[m][n];
}

int main() {
  scanf("%s %s", &str1, &str2);

  m = strlen(str1);
  n = strlen(str2);

  //    memset(dp,-1,sizeof(dp));
  //    int indx=lcs(m,n);
  int indx = LCS();

  if (indx == 0) {
    printf(" \n");
    return 0;
  }

  int i = m, j = n;
  char ans[indx + 1];
  ans[indx] = '\0';

  while (i > 0 && j > 0) {
    if (str1[i - 1] == str2[j - 1]) {
      ans[indx - 1] = str1[i - 1];
      i--, j--, indx--;
    } else if (MAT[i - 1][j] > MAT[i][j - 1]) {
      i--;
    } else {
      j--;
    }
  }
  printf("%s\n", ans);

  return 0;
}


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

using namespace std;

typedef long long ll;

// freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
#define M 3001

int m, n;
int MAT[M][M];
char str1[M], str2[M];

int LCS() {
  for (int i = 0; i <= m; i++) {
    for (int j = 0; j <= n; j++) {
      if (i == 0 || j == 0) {
        MAT[i][j] = 0;
      } else if (str1[i - 1] == str2[j - 1]) {
        MAT[i][j] = 1 + MAT[i - 1][j - 1];
      } else {
        MAT[i][j] = max(MAT[i - 1][j], MAT[i][j - 1]);
      }
    }
  }
  return MAT[m][n];
}

int main() {
  scanf("%s %s", &str1, &str2);

  m = strlen(str1);
  n = strlen(str2);

  //    memset(dp,-1,sizeof(dp));
  //    int indx=lcs(m,n);
  int indx = LCS();

  if (indx == 0) {
    printf(" \n");
    return 0;
  }

  int i = m, j = n;
  char ans[indx + 1];
  ans[indx] = '\0';

  while (i > 0 && j > 0) {
    if (str1[i - 1] == str2[j - 1]) {
      ans[indx - 1] = str1[i - 1];
      i--, j--, indx--;
    } else if (MAT[i - 1][j] > MAT[i][j - 1]) {
      i--;
    } else {
      j--;
    }
  }
  printf("%s\n", ans);

  return 0;
}