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

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <vector>
using namespace std;
template <typename A, size_t N, typename T>
void Fill(A (&array)[N], const T &val) {
  std::fill((T *)array, (T *)(array + N), val);
}
#define rep(i, n) for (int i = 0; i < n; i++)
#define req(i, n) for (int i = 1; i <= n; i++)
#define rrep(i, n) for (int i = n - 1; i >= 0; i--)
#define ALL(a) a.begin(), a.end()
typedef long long ll;
typedef long double ld;
const ll INF = 1LL << 60;
const int inf = 1 << 30;
int n, m, x, y, q;
ll z, w = 0, sum = 0, ans = 0;
const int MAX = 510000;
const int MOD = 1000000007;
int main(void) {
  string s, t, ans;
  cin >> s >> t;
  int dp[301][301];
  Fill(dp, 0);
  rep(i, s.size() + 1) {
    rep(j, t.size() + 1) {
      if (s[i] == t[j])
        dp[i + 1][j + 1] = max(dp[i + 1][j + 1], dp[i][j] + 1);
      dp[i + 1][j + 1] = max(dp[i + 1][j + 1], dp[i + 1][j]);
      dp[i + 1][j + 1] = max(dp[i + 1][j + 1], dp[i][j + 1]);
    }
  }
  int i = s.size(), j = t.size();
  while (i > 0 && j > 0) {
    if (dp[i][j] == dp[i - 1][j])
      i--;
    else if (dp[i][j] == dp[i][j - 1])
      j--;
    else {
      ans += s[i - 1];
      i--;
      j--;
    }
  }
  reverse(ALL(ans));
  cout << ans << endl;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <vector>
using namespace std;
template <typename A, size_t N, typename T>
void Fill(A (&array)[N], const T &val) {
  std::fill((T *)array, (T *)(array + N), val);
}
#define rep(i, n) for (int i = 0; i < n; i++)
#define req(i, n) for (int i = 1; i <= n; i++)
#define rrep(i, n) for (int i = n - 1; i >= 0; i--)
#define ALL(a) a.begin(), a.end()
typedef long long ll;
typedef long double ld;
const ll INF = 1LL << 60;
const int inf = 1 << 30;
int n, m, x, y, q;
ll z, w = 0, sum = 0, ans = 0;
const int MAX = 510000;
const int MOD = 1000000007;
int main(void) {
  string s, t, ans;
  cin >> s >> t;
  int dp[3010][3010];
  Fill(dp, 0);
  rep(i, s.size() + 1) {
    rep(j, t.size() + 1) {
      if (s[i] == t[j])
        dp[i + 1][j + 1] = max(dp[i + 1][j + 1], dp[i][j] + 1);
      dp[i + 1][j + 1] = max(dp[i + 1][j + 1], dp[i + 1][j]);
      dp[i + 1][j + 1] = max(dp[i + 1][j + 1], dp[i][j + 1]);
    }
  }
  int i = s.size(), j = t.size();
  while (i > 0 && j > 0) {
    if (dp[i][j] == dp[i - 1][j])
      i--;
    else if (dp[i][j] == dp[i][j - 1])
      j--;
    else {
      ans += s[i - 1];
      i--;
      j--;
    }
  }
  reverse(ALL(ans));
  cout << ans << endl;
}
Problem p03165 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
int main() {
  string s, t;
  cin >> s >> t;
  int dp[s.size() + 1][t.size() + 1];
  for (int i = 0; i <= s.size(); i++)
    dp[i][0] = 0;
  for (int i = 0; i <= t.size(); i++)
    dp[0][i] = 0;
  for (int i = 1; i <= s.size(); i++) {
    for (int j = 1; j <= t.size(); j++) {
      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]);
    }
  }
  if (dp[s.size()][t.size()] == 0) {
    cout << ' ' << "\n";
    return 0;
  }
  string com;
  int i = s.size();
  int j = t.size();
  while (i != 0 || j != 0) {
    if (s[i - 1] == t[j - 1]) {
      com.push_back(s[i - 1]);
      i--;
      j--;
    } else {
      if (dp[i - 1][j] >= dp[i][j - 1])
        i--;
      else
        j--;
    }
  }
  reverse(com.begin(), com.end());
  cout << com << "\n";
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int main() {
  string s, t;
  cin >> s >> t;
  int dp[s.size() + 1][t.size() + 1];
  for (int i = 0; i <= s.size(); i++)
    dp[i][0] = 0;
  for (int i = 0; i <= t.size(); i++)
    dp[0][i] = 0;
  for (int i = 1; i <= s.size(); i++) {
    for (int j = 1; j <= t.size(); j++) {
      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]);
    }
  }
  if (dp[s.size()][t.size()] == 0) {
    cout << ' ' << "\n";
    return 0;
  }
  string com;
  int i = s.size();
  int j = t.size();
  while (i != 0 && j != 0) {
    if (s[i - 1] == t[j - 1]) {
      com.push_back(s[i - 1]);
      i--;
      j--;
    } else {
      if (dp[i - 1][j] >= dp[i][j - 1])
        i--;
      else
        j--;
    }
  }
  reverse(com.begin(), com.end());
  cout << com << "\n";
  return 0;
}
Problem p03165 (C++)

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

int LCS[MAXN][MAXN];
char L[MAXN];

int main() {
  string s1, s2;
  cin >> s1 >> s2;
  int x = s1.size();
  int y = s2.size();
  int i, j;
  for (i = 0; i <= x; i++)
    for (j = 0; j <= y; j++) {
      if (i == 0 || j == 0)
        LCS[i][j] = 0;
      else if (s1[i - 1] == s2[j - 1]) {
        LCS[i][j] = 1 + LCS[i - 1][j - 1];
      } else {
        LCS[i][j] = max(LCS[i - 1][j], LCS[i][j - 1]);
      }
    }

  int index = LCS[x][y];
  L[index + 1] = '\0';

  while (i > 0 && j > 0) {
    if (s1[i - 1] == s2[j - 1]) {
      L[index] = s1[i - 1];
      i--;
      j--;
      index--;
    } else {
      if (LCS[i][j - 1] > LCS[i - 1][j]) {
        j--;
      } else
        i--;
    }
  }
  cout << L << endl;
}

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

int LCS[MAXN][MAXN];
char L[MAXN];

int main() {
  string s1, s2;
  cin >> s1 >> s2;
  int x = s1.size();
  int y = s2.size();
  int i, j;
  for (i = 0; i <= x; i++)
    for (j = 0; j <= y; j++) {
      if (i == 0 || j == 0)
        LCS[i][j] = 0;
      else if (s1[i - 1] == s2[j - 1]) {
        LCS[i][j] = 1 + LCS[i - 1][j - 1];
      } else {
        LCS[i][j] = max(LCS[i - 1][j], LCS[i][j - 1]);
      }
    }

  int index = LCS[x][y];
  L[index + 1] = '\0';

  while (i > 0 && j > 0) {
    if (s1[i - 1] == s2[j - 1]) {
      L[index] = s1[i - 1];
      i--;
      j--;
      index--;
    } else {
      if (LCS[i][j - 1] > LCS[i - 1][j]) {
        j--;
      } else
        i--;
    }
  }
  cout << L << endl;
}
Problem p03165 (C++)

Buggy submission (status: Runtime Error):
using namespace std;
#include <bits/stdc++.h>
#define Debug(...) fprintf(stderr, __VA_ARGS__)
#define st first
#define nd second
#define FORE(i, a, b) for (int i = (a), b_ = (b); i <= b_; ++i)
#define FORD(i, a, b) for (int i = (a), b_ = (b); i >= b_; --i)
#define TR(c, it)                                                              \
  for (__typeof((c).begin()) it = (c).begin(); it != (c).end(); it++)
#define removeDuplicate(a)                                                     \
  sort(a.begin(), a.end());                                                    \
  a.resize(distance(a.begin(), unique(a.begin(), a.end())))

const int N = 1e3 + 100;
const int MOD = 1e9 + 7;
int dp[N][N];

int main() {
#define TASK "TASK"
  // freopen(TASK".inp","r",stdin);
  // freopen(TASK".out","w",stdout);
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  string s, t;
  cin >> s >> t;
  int n = s.size(), m = t.size();
  s = " " + s;
  t = " " + t;
  for (int i = 1; i <= n; ++i) {
    for (int j = 1; j <= m; ++j) {
      if (s[i] == t[j]) {
        dp[i][j] = dp[i - 1][j - 1] + 1;
      } else {
        dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]);
      }
    }
  }
  string ans = "";
  int x = n, y = m;
  while (x > 0 && y > 0) {
    if (s[x] == t[y] and dp[x][y] > dp[x - 1][y] and dp[x][y] > dp[x][y - 1]) {
      ans = ans + s[x];
      x--;
      y--;
    } else {
      if (dp[x - 1][y] > dp[x][y - 1]) {
        x--;
      } else {
        y--;
      }
    }
  }
  reverse(ans.begin(), ans.end());
  cout << ans;

  return 0;
}

Fixed submission:
using namespace std;
#include <bits/stdc++.h>
#define Debug(...) fprintf(stderr, __VA_ARGS__)
#define st first
#define nd second
#define FORE(i, a, b) for (int i = (a), b_ = (b); i <= b_; ++i)
#define FORD(i, a, b) for (int i = (a), b_ = (b); i >= b_; --i)
#define TR(c, it)                                                              \
  for (__typeof((c).begin()) it = (c).begin(); it != (c).end(); it++)
#define removeDuplicate(a)                                                     \
  sort(a.begin(), a.end());                                                    \
  a.resize(distance(a.begin(), unique(a.begin(), a.end())))

const int N = 5e3 + 100;
const int MOD = 1e9 + 7;
int dp[N][N];

int main() {
#define TASK "TASK"
  // freopen(TASK".inp","r",stdin);
  // freopen(TASK".out","w",stdout);
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  string s, t;
  cin >> s >> t;
  int n = s.size(), m = t.size();
  s = " " + s;
  t = " " + t;
  for (int i = 1; i <= n; ++i) {
    for (int j = 1; j <= m; ++j) {
      if (s[i] == t[j]) {
        dp[i][j] = dp[i - 1][j - 1] + 1;
      } else {
        dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]);
      }
    }
  }
  string ans = "";
  int x = n, y = m;
  while (x > 0 && y > 0) {
    if (s[x] == t[y] and dp[x][y] > dp[x - 1][y] and dp[x][y] > dp[x][y - 1]) {
      ans = ans + s[x];
      x--;
      y--;
    } else {
      if (dp[x - 1][y] > dp[x][y - 1]) {
        x--;
      } else {
        y--;
      }
    }
  }
  reverse(ans.begin(), ans.end());
  cout << ans;

  return 0;
}
Problem p03165 (C++)

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

int main() {
  string s, t;
  cin >> s >> t;

  vector<vector<string>> st(s.size() + 1, vector<string>(t.size() + 1, ""));

  for (int i = 1; i <= s.size(); i++) {
    for (int j = 1; j <= t.size(); j++) {
      if (st[i - 1][j].size() > st[i][j - 1].size()) {
        st[i][j] = st[i - 1][j];
      } else {
        st[i][j] = st[i][j - 1];
      }

      if (s[i - 1] == t[j - 1] &&
          (st[i - 1][j - 1].size() + 1) > st[i][j].size()) {
        st[i][j] = st[i - 1][j - 1] + s[i - 1];
      }
      st[i - 1][j - 1].clear();
    }
  }

  cout << st.back().back() << endl;
}


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

int main() {
  string s, t;
  cin >> s >> t;

  vector<vector<string>> st(s.size() + 1, vector<string>(t.size() + 1, ""));

  for (int i = 1; i <= s.size(); i++) {
    for (int j = 1; j <= t.size(); j++) {
      if (st[i - 1][j].size() > st[i][j - 1].size()) {
        st[i][j] = st[i - 1][j];
      } else {
        st[i][j] = st[i][j - 1];
      }

      if (s[i - 1] == t[j - 1] &&
          (st[i - 1][j - 1].size() + 1) > st[i][j].size()) {
        st[i][j] = st[i - 1][j - 1] + s[i - 1];
      }
      string().swap(st[i - 1][j - 1]);
    }
  }

  cout << st.back().back() << endl;
}
Problem p03165 (C++)

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

using namespace std;

int dp[1010][1010];
int main() {
  string a, b;
  cin >> a >> b;
  int n = a.size(), m = b.size();
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      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 i = n, j = m;
  string out = "";
  int maxi = dp[n][m];
  while (j > 0 && i > 0) {
    if (a[i - 1] == b[j - 1]) {
      if (dp[i - 1][j - 1] + 1 == dp[i][j]) {
        out += b[j - 1];
        i--;
        j--;
        maxi--;
      }
    } else if (dp[i - 1][j] == maxi)
      i--;
    else
      j--;
  }
  for (int i = 0; i < out.size(); i++)
    cout << out[out.size() - i - 1];
  cout << endl;
}

Fixed submission:
#include <algorithm>
#include <iostream>

using namespace std;

int dp[3010][3010];
int main() {
  string a, b;
  cin >> a >> b;
  int n = a.size(), m = b.size();
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      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 i = n, j = m;
  string out = "";
  int maxi = dp[n][m];
  while (j > 0 && i > 0) {
    if (a[i - 1] == b[j - 1]) {
      if (dp[i - 1][j - 1] + 1 == dp[i][j]) {
        out += b[j - 1];
        i--;
        j--;
        maxi--;
      }
    } else if (dp[i - 1][j] == maxi)
      i--;
    else
      j--;
  }
  for (int i = 0; i < out.size(); i++)
    cout << out[out.size() - i - 1];
  cout << endl;
}
Problem p03165 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>

using namespace std;

int dp[3001][3001];
int idx[3001][3001];
bool already[3001][3001];
string a, b;

int Solve(int x = 0, int y = 0) {
  if (already[x][y])
    return dp[x][y];
  if (x == a.size() || y == b.size())
    dp[x][y] = 0;
  else {
    dp[x][y] = Solve(x + 1, y);

    int i = y;
    for (; i < b.size(); ++i)
      if (b[i] == a[x])
        break;
    idx[x][y] = i;
    if (i != b.size() && Solve(x + 1, i + 1) + 1 > dp[x][y])
      dp[x][y] = 1 + dp[x + 1][i + 1];
  }
  return dp[x][y];
}

void Print(int x = 0, int y = 0) {
  if (dp[x][y] == 0)
    return;
  int i = idx[x][y];
  if (i != b.size() && dp[x + 1][i + 1] + 1 > dp[x + 1][y]) {
    cout << a[x];
    Print(x + 1, i + 1);
  } else
    Print(x + 1, y);
}

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

  cin >> a >> b;
  Solve();
  Print();
  cout << "\n";

  return 0;
} // RUL0

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

using namespace std;

int dp[3001][3001];
int idx[3001][3001];
bool already[3001][3001];
string a, b;

int Solve(int x = 0, int y = 0) {
  if (already[x][y])
    return dp[x][y];
  already[x][y] = true;
  if (x == a.size() || y == b.size())
    dp[x][y] = 0;
  else {
    dp[x][y] = Solve(x + 1, y);

    int i = y;
    for (; i < b.size(); ++i)
      if (b[i] == a[x])
        break;
    idx[x][y] = i;
    if (i != b.size() && Solve(x + 1, i + 1) + 1 > dp[x][y])
      dp[x][y] = 1 + dp[x + 1][i + 1];
  }
  return dp[x][y];
}

void Print(int x = 0, int y = 0) {
  if (dp[x][y] == 0)
    return;
  int i = idx[x][y];
  if (i != b.size() && dp[x + 1][i + 1] + 1 > dp[x + 1][y]) {
    cout << a[x];
    Print(x + 1, i + 1);
  } else
    Print(x + 1, y);
}

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

  cin >> a >> b;
  Solve();
  Print();
  cout << "\n";

  return 0;
} // RUL0
Problem p03165 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
int dp[1001][1001];
string s, t, ans;
int main() {
  cin >> s >> t;
  for (int i = 1; i <= s.size(); i++) {
    for (int j = 1; j <= t.size(); j++) {
      if (s[i - 1] == t[j - 1])
        dp[i][j] = max({dp[i - 1][j - 1] + 1, dp[i - 1][j], dp[i][j - 1]});
      else
        dp[i][j] = max({dp[i][j - 1], dp[i - 1][j], dp[i - 1][j - 1]});
    }
  }
  int x = s.size();
  int y = t.size();
  while (x && y) {
    int Maxx = max({dp[x - 1][y - 1], dp[x][y - 1], dp[x - 1][y]});
    if (Maxx == dp[x][y] - 1) {
      ans += s[x - 1];
      x--;
      y--;
    } else if (Maxx == dp[x - 1][y])
      x--;
    else
      y--;
  }
  reverse(ans.begin(), ans.end());
  cout << ans << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int dp[3001][3001];
string s, t, ans;
int main() {
  cin >> s >> t;
  for (int i = 1; i <= s.size(); i++) {
    for (int j = 1; j <= t.size(); j++) {
      if (s[i - 1] == t[j - 1])
        dp[i][j] = max({dp[i - 1][j - 1] + 1, dp[i - 1][j], dp[i][j - 1]});
      else
        dp[i][j] = max({dp[i][j - 1], dp[i - 1][j], dp[i - 1][j - 1]});
    }
  }
  int x = s.size();
  int y = t.size();
  while (x && y) {
    int Maxx = max({dp[x - 1][y - 1], dp[x][y - 1], dp[x - 1][y]});
    if (Maxx == dp[x][y] - 1) {
      ans += s[x - 1];
      x--;
      y--;
    } else if (Maxx == dp[x - 1][y])
      x--;
    else
      y--;
  }
  reverse(ans.begin(), ans.end());
  cout << ans << endl;
}
Problem p03165 (C++)

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

using namespace std;

int main() {
  string s, t;
  cin >> s >> t;
  int m = s.length();
  int n = t.length();
  int dp[m + 1][n + 1];
  for (int i = 0; i <= m; i++) {
    for (int j = 0; j <= n; j++) {
      dp[i][j] = 0;
    }
  }
  for (int i = 1; i <= m; i++) {
    for (int j = 1; j <= n; j++) {
      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]);
      }
    }
  }
  int k = dp[m][n];
  string lcs('.', k);
  int i = m;
  int j = n;
  int p = k - 1;
  while (i > 0 && j > 0) {
    if (s[i - 1] == t[j - 1]) {
      lcs[p] = s[i - 1];
      i--;
      j--;
      p--;
    } else if (dp[i - 1][j] > dp[i][j - 1]) {
      i--;
    } else
      j--;
  }
  cout << lcs;
}


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

using namespace std;

int main() {
  string s, t;
  cin >> s >> t;
  int m = s.length();
  int n = t.length();
  int dp[m + 1][n + 1];
  for (int i = 0; i <= m; i++) {
    for (int j = 0; j <= n; j++) {
      dp[i][j] = 0;
    }
  }
  for (int i = 1; i <= m; i++) {
    for (int j = 1; j <= n; j++) {
      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]);
      }
    }
  }
  int k = dp[m][n];
  string lcs(k, '.');
  int i = m;
  int j = n;
  int p = k - 1;
  while (i > 0 && j > 0) {
    if (s[i - 1] == t[j - 1]) {
      lcs[p] = s[i - 1];
      i--;
      j--;
      p--;
    } else if (dp[i - 1][j] > dp[i][j - 1]) {
      i--;
    } else
      j--;
  }
  cout << lcs;
}
Problem p03165 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define FOR(i, a, b) for (int i = (a), _b = (b); i <= _b; i++)
#define FORN(i, b, a) for (int i = (b); _a = (a); i >= _a; i--)
#define REP(i, n) for (int i = 0, _n = n; i < n; i++)
#define ll long long
#define pii pair<int, int>
#define re return
#define vi vector<int>
#define pb push_back
#define si set<int>
#define in insert
#define fl float
#define db double
#define ld long double
#define X first
#define Y second
#define st string
#define ull unsigned long long
#define mod 1000000007
#define INF 1000000007
using namespace std;
inline void read(int &x) {
  short negative = 1;
  x = 0;
  char c = getchar();
  while (c < '0' || c > '9') {
    if (c == '-')
      negative = -1;
    c = getchar();
  }
  while (c >= '0' && c <= '9')
    x = (x << 3) + (x << 1) + (c ^ 48), c = getchar();
  x *= negative;
}
int main() {
  string s, t;
  string ans = "";
  ll dp[330][330];
  memset(dp, 0, sizeof(dp));
  cin >> s >> t;
  REP(i, s.size())
  REP(j, t.size()) {
    if (s[i] == t[j])
      dp[i + 1][j + 1] = max(dp[i + 1][j + 1], dp[i][j] + 1);
    dp[i + 1][j + 1] = max(dp[i + 1][j + 1], dp[i + 1][j]);
    dp[i + 1][j + 1] = max(dp[i + 1][j + 1], 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--;
    else if (dp[i][j] == dp[i][j - 1])
      j--;
    else {
      ans += s[i - 1];
      i--;
      j--;
    }
  }
  for (int i = ans.size() - 1; i >= 0; i--)
    cout << ans[i];
  cout << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define FOR(i, a, b) for (int i = (a), _b = (b); i <= _b; i++)
#define FORN(i, b, a) for (int i = (b); _a = (a); i >= _a; i--)
#define REP(i, n) for (int i = 0, _n = n; i < n; i++)
#define ll long long
#define pii pair<int, int>
#define re return
#define vi vector<int>
#define pb push_back
#define si set<int>
#define in insert
#define fl float
#define db double
#define ld long double
#define X first
#define Y second
#define st string
#define ull unsigned long long
#define mod 1000000007
#define INF 1000000007
using namespace std;
inline void read(int &x) {
  short negative = 1;
  x = 0;
  char c = getchar();
  while (c < '0' || c > '9') {
    if (c == '-')
      negative = -1;
    c = getchar();
  }
  while (c >= '0' && c <= '9')
    x = (x << 3) + (x << 1) + (c ^ 48), c = getchar();
  x *= negative;
}
int main() {
  string s, t;
  string ans = "";
  ll dp[3030][3030];
  memset(dp, 0, sizeof(dp));
  cin >> s >> t;
  REP(i, s.size())
  REP(j, t.size()) {
    if (s[i] == t[j])
      dp[i + 1][j + 1] = max(dp[i + 1][j + 1], dp[i][j] + 1);
    dp[i + 1][j + 1] = max(dp[i + 1][j + 1], dp[i + 1][j]);
    dp[i + 1][j + 1] = max(dp[i + 1][j + 1], 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--;
    else if (dp[i][j] == dp[i][j - 1])
      j--;
    else {
      ans += s[i - 1];
      i--;
      j--;
    }
  }
  for (int i = ans.size() - 1; i >= 0; i--)
    cout << ans[i];
  cout << endl;
  return 0;
}
Problem p03165 (C++)

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

using namespace std;

const int MAX = 3005;

int dp[MAX][MAX];

int main() {

  string s, t;

  cin >> s;
  cin >> t;

  for (int i = 1; i <= s.size(); i++) {
    for (int j = 1; j <= t.size(); j++) {
      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]);
      }
    }
  }

  int x = s.size(), y = t.size();
  string ans;

  while (true) {
    if (dp[x - 1][y] == dp[x][y]) {
      x--;
    } else if (dp[x][y - 1] == dp[x][y]) {
      y--;
    } else if (dp[x - 1][y - 1] + 1 == dp[x][y]) {
      ans += s[x - 1];
      x--;
      y--;
    } else {
      break;
    }
  }

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

  cout << ans << endl;
}

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

using namespace std;

const int MAX = 3005;

int dp[MAX][MAX];

int main() {

  string s, t;

  cin >> s;
  cin >> t;

  for (int i = 1; i <= s.size(); i++) {
    for (int j = 1; j <= t.size(); j++) {
      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]);
      }
    }
  }

  int x = s.size(), y = t.size();
  string ans;

  while (x > 0 && y > 0) {
    if (dp[x - 1][y] == dp[x][y]) {
      x--;
    } else if (dp[x][y - 1] == dp[x][y]) {
      y--;
    } else if (dp[x - 1][y - 1] + 1 == dp[x][y]) {
      ans += s[x - 1];
      x--;
      y--;
    } else {
      break;
    }
  }

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

  cout << ans << endl;
}
Problem p03165 (C++)

Buggy submission (status: Memory Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define fs first
#define sc second
#define mp make_pair
#define pb push_back
#define eb emplace_back
#define ALL(A) A.begin(), A.end()
#define RALL(A) A.rbegin(), A.rend()
typedef long LL;
typedef pair<LL, LL> P;
const LL mod = 1e9 + 7;
const LL LINF = 1LL << 62;
const int INF = 1000000000;

int main() {
  string s, t;
  cin >> s >> t;
  int n = s.length(), m = t.length();
  vector<vector<string>> dp(n + 1, vector<string>(m + 1, ""));
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      string p = dp[i][j - 1], q = dp[i - 1][j], r = dp[i - 1][j - 1];
      if (p.length() > q.length())
        dp[i][j] = p;
      else
        dp[i][j] = q;
      if (s[i - 1] == t[j - 1]) {
        if (r.length() + 1 > dp[i][j].length())
          dp[i][j] = r + s[i - 1];
      }
    }
  }
  cout << dp[n][m] << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define fs first
#define sc second
#define mp make_pair
#define pb push_back
#define eb emplace_back
#define ALL(A) A.begin(), A.end()
#define RALL(A) A.rbegin(), A.rend()
typedef long LL;
typedef pair<LL, LL> P;
const LL mod = 1e9 + 7;
const LL LINF = 1LL << 62;
const int INF = 1000000000;

int main() {
  string s, t;
  cin >> s >> t;
  int n = s.length(), m = t.length();
  vector<vector<string>> dp(n + 1, vector<string>(m + 1, ""));
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      string p = dp[i][j - 1], q = dp[i - 1][j], r = dp[i - 1][j - 1];
      if (p.length() > q.length())
        dp[i][j] = p;
      else
        dp[i][j] = q;
      if (s[i - 1] == t[j - 1]) {
        if (r.length() + 1 > dp[i][j].length())
          dp[i][j] = r + s[i - 1];
      }
      dp[i - 1][j - 1] = "";
    }
  }
  cout << dp[n][m] << endl;
  return 0;
}
Problem p03165 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define pb emplace_back
#define fi first
#define se second
#define int long long
using namespace std;
mt19937_64
    rang(chrono::high_resolution_clock::now().time_since_epoch().count());
int rng(int l) {
  uniform_int_distribution<int> uid(0, l - 1);
  return uid(rang);
}

int a[3001][3001];

int32_t main() {
#ifndef ONLINE_JUDGE
  freopen("/tmp/input", "r", stdin);
  freopen("/tmp/output", "w", stdout);
#endif
  ios::sync_with_stdio(false);
  cin.tie(NULL);
  cout.precision(10);
  srand(chrono::high_resolution_clock::now().time_since_epoch().count());
  string s;
  cin >> s;
  string t;
  cin >> t;
  int n = s.size();
  int m = t.size();
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < m; ++j) {
      if (s[i] == t[j]) {
        a[i + 1][j + 1] = a[i][j] + 1;
      } else {
        a[i + 1][j + 1] = max(a[i][j + 1], a[i + 1][j]);
      }
    }
  }
  string s1 = "";
  int i = n, j = m;
  while (i && j) {
    if (s[i - 1] == t[j - 1]) {
      s1 += string(1, s[i - 1]);
      i--;
      j--;
    } else {
      if (a[i - 1][j] > a[i][j - 1]) {
        i--;
      } else {
        j--;
      }
    }
  }
  reverse(s1.begin(), s1.end());
  cout << s1;
}

Fixed submission:
#include <bits/stdc++.h>
#define pb emplace_back
#define fi first
#define se second
#define int long long
using namespace std;
mt19937_64
    rang(chrono::high_resolution_clock::now().time_since_epoch().count());
int rng(int l) {
  uniform_int_distribution<int> uid(0, l - 1);
  return uid(rang);
}

int a[3001][3001];

int32_t main() {
  // #ifndef ONLINE_JUDGE
  // 	freopen("/tmp/input", "r", stdin);
  // 	freopen("/tmp/output", "w", stdout);
  // #endif
  ios::sync_with_stdio(false);
  cin.tie(NULL);
  cout.precision(10);
  srand(chrono::high_resolution_clock::now().time_since_epoch().count());
  string s;
  cin >> s;
  string t;
  cin >> t;
  int n = s.size();
  int m = t.size();
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < m; ++j) {
      if (s[i] == t[j]) {
        a[i + 1][j + 1] = a[i][j] + 1;
      } else {
        a[i + 1][j + 1] = max(a[i][j + 1], a[i + 1][j]);
      }
    }
  }
  string s1 = "";
  int i = n, j = m;
  while (i && j) {
    if (s[i - 1] == t[j - 1]) {
      s1 += string(1, s[i - 1]);
      i--;
      j--;
    } else {
      if (a[i - 1][j] > a[i][j - 1]) {
        i--;
      } else {
        j--;
      }
    }
  }
  reverse(s1.begin(), s1.end());
  cout << s1;
}
Problem p03165 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
#include <cstring>
using std::max;

int LCS(char *s1, char *s2, char *res) {
  int i, j, M, N, k = 0, upbd;
  M = strlen(s1);
  N = strlen(s2);
  int **dp;
  dp = new int *[M + 2];
  for (i = 0; i < M + 2; i++)
    dp[i] = new int[N + 2];
  for (i = 0; i < N + 2; i++)
    dp[0][i] = 0;

  for (i = 1; i < M + 1; i++) {
    dp[i][0] = 0;
    for (j = 1; j < N + 1; j++) {
      if (s1[i - 1] == s2[j - 1])
        dp[i][j] = dp[i - 1][j - 1] + 1;
      else
        dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]);
    }
  }
  k = dp[M][N];

  i = M;
  j = N;
  upbd = k;
  res[k] = '\0';
  while (i > 0 && j > 0) {
    if (dp[i - 1][j] == dp[i - 1][j - 1] && dp[i - 1][j - 1] == dp[i][j - 1]) {
      if (dp[i][j] == dp[i - 1][j - 1] + 1)
        res[--upbd] = s1[i - 1];
      i--;
      j--;
    } else {
      if (dp[i - 1][j] == dp[i][j])
        i--;
      else
        j--;
    }
  }
  for (i = 0; i < M + 2; i++)
    delete[] dp[i];
  delete[] dp;

  return k;
}

void solve(char *s) {
  char *s1, *s2, *res, *target;
  int N = strlen(s), i, d, maxVal = -1;
  s1 = new char[N + 1];
  s2 = new char[N + 1];
  res = new char[N + 1];
  target = new char[N + 1];
  for (i = 2; i < N - 1; i++) {
    strcpy(s1, s);
    s1[i] = 0;
    strcpy(s2, s + i);

    d = LCS(s1, s2, res);
    if (d > maxVal) {
      maxVal = d;
      strcpy(target, res);
    }
  }
  printf("%s\n", target);
  delete[] s1;
  delete[] s2;
  delete[] res;
  delete[] target;
}

#define MAXLENGTH 303
int main(void) {
  char s[MAXLENGTH], t[MAXLENGTH], r[MAXLENGTH];
  scanf("%s%s", s, t);
  LCS(s, t, r);
  printf("%s\n", r);
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cstdio>
#include <cstring>
using std::max;

int LCS(char *s1, char *s2, char *res) {
  int i, j, M, N, k = 0, upbd;
  M = strlen(s1);
  N = strlen(s2);
  int **dp;
  dp = new int *[M + 2];
  for (i = 0; i < M + 2; i++)
    dp[i] = new int[N + 2];
  for (i = 0; i < N + 2; i++)
    dp[0][i] = 0;

  for (i = 1; i < M + 1; i++) {
    dp[i][0] = 0;
    for (j = 1; j < N + 1; j++) {
      if (s1[i - 1] == s2[j - 1])
        dp[i][j] = dp[i - 1][j - 1] + 1;
      else
        dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]);
    }
  }
  k = dp[M][N];

  i = M;
  j = N;
  upbd = k;
  res[k] = '\0';
  while (i > 0 && j > 0) {
    if (dp[i - 1][j] == dp[i - 1][j - 1] && dp[i - 1][j - 1] == dp[i][j - 1]) {
      if (dp[i][j] == dp[i - 1][j - 1] + 1)
        res[--upbd] = s1[i - 1];
      i--;
      j--;
    } else {
      if (dp[i - 1][j] == dp[i][j])
        i--;
      else
        j--;
    }
  }
  for (i = 0; i < M + 2; i++)
    delete[] dp[i];
  delete[] dp;

  return k;
}

#define MAXLENGTH 3003
int main(void) {
  char s[MAXLENGTH], t[MAXLENGTH], r[MAXLENGTH];
  scanf("%s%s", s, t);
  LCS(s, t, r);
  printf("%s\n", r);
  return 0;
}
Problem p03165 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
const int MAX_N = 1005;
string s, t;
int n, m;
int dp[MAX_N][MAX_N];
pair<int, int> pre[MAX_N][MAX_N];
int main() {
  cin >> s >> t;
  n = s.size(), m = t.size();
  s = '0' + s;
  t = '0' + t;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      //	if(s[i]==t[j])dp[i][j]=max(dp[i][j],dp[i-1][j-1]+1);
      //	dp[i][j]=max(dp[i][j],max(dp[i-1][j],dp[i][j-1]));
      if (s[i] == t[j] && dp[i][j] < dp[i - 1][j - 1] + 1) {
        dp[i][j] = dp[i - 1][j - 1] + 1;
        pre[i][j] = make_pair(i - 1, j - 1);
      }
      if (dp[i][j] < dp[i - 1][j]) {
        dp[i][j] = dp[i - 1][j];
        pre[i][j] = make_pair(i - 1, j);
      }
      if (dp[i][j] < dp[i][j - 1]) {
        dp[i][j] = dp[i][j - 1];
        pre[i][j] = make_pair(i, j - 1);
      }
    }
  }
  int nx = n, ny = m;
  string ans;
  while (nx && ny) {
    if (s[nx] == t[ny])
      ans = s[nx] + ans;
    tie(nx, ny) = pre[nx][ny];
  }
  cout << ans << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
const int MAX_N = 3005;
string s, t;
int n, m;
int dp[MAX_N][MAX_N];
pair<int, int> pre[MAX_N][MAX_N];
int main() {
  cin >> s >> t;
  n = s.size(), m = t.size();
  s = '0' + s;
  t = '0' + t;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      //	if(s[i]==t[j])dp[i][j]=max(dp[i][j],dp[i-1][j-1]+1);
      //	dp[i][j]=max(dp[i][j],max(dp[i-1][j],dp[i][j-1]));
      if (s[i] == t[j] && dp[i][j] < dp[i - 1][j - 1] + 1) {
        dp[i][j] = dp[i - 1][j - 1] + 1;
        pre[i][j] = make_pair(i - 1, j - 1);
      }
      if (dp[i][j] < dp[i - 1][j]) {
        dp[i][j] = dp[i - 1][j];
        pre[i][j] = make_pair(i - 1, j);
      }
      if (dp[i][j] < dp[i][j - 1]) {
        dp[i][j] = dp[i][j - 1];
        pre[i][j] = make_pair(i, j - 1);
      }
    }
  }
  int nx = n, ny = m;
  string ans;
  while (nx && ny) {
    if (s[nx] == t[ny])
      ans = s[nx] + ans;
    tie(nx, ny) = pre[nx][ny];
  }
  cout << ans << endl;
  return 0;
}
Problem p03165 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
int dp[3000][3000];
void helper(string &s, string &t, int len) {
  string LCS;
  int i = 0, j = 0;
  while (len) {
    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++;
  }
  cout << LCS;
}

int maxlensubseqence(int i, int j, string s, string t) {
  if (i >= s.length() or j >= t.length())
    return 0;
  if (dp[i][j] != -1)
    return dp[i][j];
  if (s[i] == t[j])
    return dp[i][j] = 1 + maxlensubseqence(i + 1, j + 1, s, t);
  else
    return dp[i][j] = max(maxlensubseqence(i, j + 1, s, t),
                          maxlensubseqence(i + 1, j, s, t));
}

void printmaxlensubsequence(string s, string t) {
  memset(dp, -1, sizeof dp);
  int len = maxlensubseqence(0, 0, s, t);

  helper(s, t, len);
  // for(int i=0;i<s.length();i++)
  // {
  //     for(int j=0;j<t.length();j++)
  //         cout<<dp[i][j]<<"\t";
  //     cout<<endl;
  // }
}

int main() {
  string s, t;
  cin >> s >> t;
  printmaxlensubsequence(s, t);
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int dp[3000][3000];
void helper(string &s, string &t, int len) {
  string LCS;
  int i = 0, j = 0;
  while (len) {
    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++;
  }
  cout << LCS;
}

int maxlensubseqence(int i, int j, string &s, string &t) {
  if (i >= s.length() or j >= t.length())
    return 0;
  if (dp[i][j] != -1)
    return dp[i][j];
  if (s[i] == t[j])
    return dp[i][j] = 1 + maxlensubseqence(i + 1, j + 1, s, t);
  else
    return dp[i][j] = max(maxlensubseqence(i, j + 1, s, t),
                          maxlensubseqence(i + 1, j, s, t));
}

void printmaxlensubsequence(string s, string t) {
  memset(dp, -1, sizeof dp);
  int len = maxlensubseqence(0, 0, s, t);

  helper(s, t, len);
  // for(int i=0;i<s.length();i++)
  // {
  //     for(int j=0;j<t.length();j++)
  //         cout<<dp[i][j]<<"\t";
  //     cout<<endl;
  // }
}

int main() {
  string s, t;
  cin >> s >> t;
  printmaxlensubsequence(s, t);
  return 0;
}
Problem p03165 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
int dp[1001][1001];
int main() {
  string s, t;
  cin >> s >> t;
  string ans = "";
  for (int i = 0; i <= s.length(); i++) {
    for (int j = 0; j <= t.length(); j++) {
      if (i == 0 or j == 0)
        dp[i][j] = 0;
      else 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]);
      }
    }
  }

  int i = s.size(), j = t.size();
  while (i > 0 and j > 0) {
    if (s[i - 1] == t[j - 1]) {
      ans = s[i - 1] + ans;
      i--;
      j--;
    } else if (dp[i - 1][j] > dp[i][j - 1])
      i--;
    else {
      j--;
    }
  }
  cout << ans;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int dp[3001][3001];
int main() {
  string s, t;
  cin >> s >> t;
  string ans = "";
  for (int i = 0; i <= s.length(); i++) {
    for (int j = 0; j <= t.length(); j++) {
      if (i == 0 or j == 0)
        dp[i][j] = 0;
      else 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]);
      }
    }
  }

  int i = s.size(), j = t.size();
  while (i > 0 and j > 0) {
    if (s[i - 1] == t[j - 1]) {
      ans = s[i - 1] + ans;
      i--;
      j--;
    } else if (dp[i - 1][j] > dp[i][j - 1])
      i--;
    else {
      j--;
    }
  }
  cout << ans;
  return 0;
}
Problem p03165 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
int main() {
  string s, t;
  cin >> s >> t;
  int n = s.length(), m = t.length();
  int dp[n + 1][m + 1];
  for (int i = 0; i <= n; i++) {
    for (int j = 0; j <= m; j++) {
      if (i == 0 || j == 0)
        dp[i][j] = 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] = 1 + dp[i - 1][j - 1];
      } else

        dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]);
    }
  }
  string ans = "";
  while (n > 0 || m > 0) {
    if (dp[n - 1][m] == dp[n][m]) {

      n--;
    } else if (dp[n][m - 1] == dp[n][m]) {

      m--;
    } else {
      ans.push_back(s[n - 1]);
      n--;
      m--;
    }
  }
  reverse(ans.begin(), ans.end());
  cout << ans;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int main() {
  string s, t;
  cin >> s >> t;
  int n = s.length(), m = t.length();
  int dp[n + 1][m + 1];
  for (int i = 0; i <= n; i++) {
    for (int j = 0; j <= m; j++) {
      if (i == 0 || j == 0)
        dp[i][j] = 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] = 1 + dp[i - 1][j - 1];
      } else

        dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]);
    }
  }
  string ans = "";
  while (n > 0 && m > 0) {
    if (dp[n - 1][m] == dp[n][m]) {

      n--;
    } else if (dp[n][m - 1] == dp[n][m]) {

      m--;
    } else {
      ans.push_back(s[n - 1]);
      n--;
      m--;
    }
  }
  reverse(ans.begin(), ans.end());
  cout << ans;
  return 0;
}
Problem p03165 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define MAX 3000

int dp[MAX + 1][MAX + 1];

string getLCS(string &s, string &t, int len) {
  string LCS;
  int i = 0, j = 0;
  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) {
  if (i >= s.length() || j >= t.length())
    return 0;

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

  if (s[i] == t[j])
    return dp[i][j] = 1 + lenLCS(s, t, i + 1, j + 1);
  else {
    return dp[i][j] = max(lenLCS(s, t, i, j + 1), lenLCS(s, t, i + 1, 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() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  string s, t;
  cin >> s >> t;
  cout << solve(s, t);
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define MAX 3000

int dp[MAX + 1][MAX + 1];

string getLCS(string &s, string &t, int len) {
  string LCS;
  int i = 0, j = 0;
  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) {
  if (i >= s.length() || j >= t.length())
    return 0;

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

  if (s[i] == t[j])
    return dp[i][j] = 1 + lenLCS(s, t, i + 1, j + 1);
  else {
    return dp[i][j] = max(lenLCS(s, t, i, j + 1), lenLCS(s, t, i + 1, 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() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  string s, t;
  cin >> s >> t;
  cout << solve(s, t);
}
Problem p03165 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
int main() {
  string s, t;
  cin >> s >> t;
  vector<vector<int>> dp(s.size() + 1, vector<int>(t.size() + 1));
  for (int i = 1; i <= s.size(); i++) {
    for (int j = 1; j <= t.size(); j++) {
      if (s[i - 1] == t[j - 1])
        dp[i][j] = max(dp[i][j], 1 + dp[i - 1][j - 1]);
      else {
        dp[i][j] = max(dp[i][j], max(dp[i - 1][j], dp[i][j - 1]));
      }
    }
  }
  int i = s.size(), j = t.size();
  string ans = "";
  while (j > 0) {
    // cout<<i<<" "<<j<<" "<<dp[i][j]<<endl;
    if (dp[i][j] == dp[i - 1][j]) {
      i--;
    } else if (dp[i][j] == dp[i][j - 1]) {
      j--;
    } else {
      ans = s[i - 1] + ans;
      i--;
      j--;
    }
  }
  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int main() {
  string s, t;
  cin >> s >> t;
  vector<vector<int>> dp(s.size() + 1, vector<int>(t.size() + 1));
  for (int i = 1; i <= s.size(); i++) {
    for (int j = 1; j <= t.size(); j++) {
      if (s[i - 1] == t[j - 1])
        dp[i][j] = max(dp[i][j], 1 + dp[i - 1][j - 1]);
      else {
        dp[i][j] = max(dp[i][j], max(dp[i - 1][j], dp[i][j - 1]));
      }
    }
  }
  int i = s.size(), j = t.size();
  string ans = "";
  while (j > 0 && i > 0) {
    // cout<<i<<" "<<j<<" "<<dp[i][j]<<endl;
    if (dp[i][j] == dp[i - 1][j]) {
      i--;
    } else if (dp[i][j] == dp[i][j - 1]) {
      j--;
    } else {
      ans = s[i - 1] + ans;
      i--;
      j--;
    }
  }
  cout << ans << endl;
  return 0;
}
Problem p03165 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
int arr[100005];
int dp[3005][3005];
int anss = 0;
char rep[100005];
int c = 0;
string str1, str2;
int lcs(int first, int last) {
  if (first <= 0 | last <= 0) {
    return 0;
  }
  if (dp[first][last] != -1)
    return dp[first][last];
  if (str1[first] == str2[last] &&
      (first != str1.size() && last != str2.size())) {
    dp[first][last] = 1 + lcs(first - 1, last - 1);
    // anss = max(anss,dp[first][last]);

  } else {
    dp[first][last] =
        max(dp[first][last], max(lcs(first - 1, last), lcs(first, last - 1)));
  }
  return dp[first][last];
}
void printstring() {
  int i = str1.size();
  int j = str2.size();
  // int idx = anss;
  int idx = lcs(str1.size() - 1, str2.size() - 1);
  char str[anss + 1];
  str[idx + 1] = '\0';
  while (i > 0 && j > 0) {
    if (str1[i] == str2[j]) {
      str[idx] = str1[i];
      idx--;
      i--;
      j--;
    } else if (dp[i - 1][j] > dp[i][j - 1]) {
      i--;
    } else
      j--;
  }
  cout << str << endl;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  for (int i = 0; i < 3006; i++)
    for (int j = 0; j < 3006; j++)
      dp[i][j] = -1;
  cin >> str1 >> str2;
  str1 = "X" + str1;
  str2 = "Y" + str2;
  string ans = "";
  int n = min(str2.size(), str1.size());
  int n1 = max(str2.size(), str1.size());
  // lcs(str1.size()-1,str2.size()-1);
  printstring();
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
int arr[100005];
int dp[3005][3005];
int anss = 0;
char rep[100005];
int c = 0;
string str1, str2;
int lcs(int first, int last) {
  if (first <= 0 | last <= 0) {
    return 0;
  }
  if (dp[first][last] != -1)
    return dp[first][last];
  if (str1[first] == str2[last] &&
      (first != str1.size() && last != str2.size())) {
    dp[first][last] = 1 + lcs(first - 1, last - 1);
    anss = max(anss, dp[first][last]);

  } else {
    dp[first][last] =
        max(dp[first][last], max(lcs(first - 1, last), lcs(first, last - 1)));
  }
  return dp[first][last];
}
void printstring() {
  int i = str1.size();
  int j = str2.size();
  // int idx = anss;
  int idx = lcs(str1.size() - 1, str2.size() - 1);
  char str[anss + 1];
  str[idx + 1] = '\0';
  while (i > 0 && j > 0) {
    if (str1[i] == str2[j]) {
      str[idx] = str1[i];
      idx--;
      i--;
      j--;
    } else if (dp[i - 1][j] > dp[i][j - 1]) {
      i--;
    } else
      j--;
  }
  cout << str << endl;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  for (int i = 0; i < 3006; i++)
    for (int j = 0; j < 3006; j++)
      dp[i][j] = -1;
  cin >> str1 >> str2;
  str1 = "X" + str1;
  str2 = "Y" + str2;
  string ans = "";
  int n = min(str2.size(), str1.size());
  int n1 = max(str2.size(), str1.size());
  // lcs(str1.size()-1,str2.size()-1);
  printstring();
}
Problem p03165 (C++)

Buggy submission (status: Runtime Error):
/*
 It's better to sleep happy and tired rather than disappointed and frustrated
 There's only 1 thing that's impossible and that is defeating a man who doesn't
 give up
*/
#include <bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define pb push_back
#define mx2 102
#define mx3 1003
#define mx4 10004
#define mx5 100005
#define mx6 1000006
#define mod 1000000007
#define PI 3.14159265
#define deb(x) cout << #x << " " << x << endl;
#define fo(i, n) for (int i = 0; i < n; i++)
#define Fo(i, k, n) for (int i = k; i < n; i++)

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

const int inf = 0x3f3f3f3f;
int n, m, ans, sum, a, b, dp[3 * mx3][3 * mx3];
string s, t;

string solve(int i, int j) {
  string res;
  if (i < 0 || j < 0) {
    return res;
  }

  if (s[i - 1] == t[j - 1]) {
    res = solve(i - 1, j - 1);
    res += s[i - 1];
    return res;
  } else if (dp[i - 1][j] > dp[i][j - 1]) {
    res = solve(i - 1, j);
    return res;
  } else {
    res = solve(i, j - 1);
    return res;
  }
}

int main() {
  ios::sync_with_stdio(false);
  // int t;cin>>t;while(t--)
  {
    cin >> s >> t;
    n = s.length();
    m = t.length();
    Fo(i, 1, n + 1) {
      Fo(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]);
        }
      }
    }
    cout << solve(n, m);
  }
  return 0;
}


Fixed submission:
/*
 It's better to sleep happy and tired rather than disappointed and frustrated
 There's only 1 thing that's impossible and that is defeating a man who doesn't
 give up
*/
#include <bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define pb push_back
#define mx2 102
#define mx3 1003
#define mx4 10004
#define mx5 100005
#define mx6 1000006
#define mod 1000000007
#define PI 3.14159265
#define deb(x) cout << #x << " " << x << endl;
#define fo(i, n) for (int i = 0; i < n; i++)
#define Fo(i, k, n) for (int i = k; i < n; i++)

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

const int inf = 0x3f3f3f3f;
int n, m, ans, sum, a, b, dp[3 * mx3][3 * mx3];
string s, t;

string solve(int i, int j) {
  string res;
  if (i <= 0 || j <= 0) {
    return res;
  }

  if (s[i - 1] == t[j - 1]) {
    res = solve(i - 1, j - 1);
    res += s[i - 1];
    return res;
  } else if (dp[i - 1][j] > dp[i][j - 1]) {
    res = solve(i - 1, j);
    return res;
  } else {
    res = solve(i, j - 1);
    return res;
  }
}

int main() {
  ios::sync_with_stdio(false);
  // int t;cin>>t;while(t--)
  {
    cin >> s >> t;
    n = s.length();
    m = t.length();
    Fo(i, 1, n + 1) {
      Fo(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]);
        }
      }
    }
    cout << solve(n, m);
  }
  return 0;
}
Problem p03165 (C++)

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

using namespace std;
typedef long long ll;

const int maxn = 1e3 + 10;
int dp[maxn][maxn], par[maxn][maxn];

int main() {
  ios_base::sync_with_stdio(false);
  string s, t;
  cin >> s >> t;
  for (int i = 1; i <= s.size(); i++) {
    for (int j = 1; j <= t.size(); j++) {
      dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]);
      if (dp[i - 1][j] >= dp[i][j - 1])
        par[i][j] = 1;
      else
        par[i][j] = 2;
      if (s[i - 1] == t[j - 1]) {
        dp[i][j] = dp[i - 1][j - 1] + 1;
        par[i][j] = 3;
      }
    }
  }
  int x = s.size(), y = t.size();
  string ans;
  while (x > 0 and y > 0) {
    if (par[x][y] == 1) {
      x--;
    } else if (par[x][y] == 2) {
      y--;
    } else {
      ans += s[x - 1];
      x--, y--;
    }
  }
  reverse(ans.begin(), ans.end());
  cout << ans << endl;
}


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

using namespace std;
typedef long long ll;

const int maxn = 3e3 + 10;
int dp[maxn][maxn], par[maxn][maxn];

int main() {
  ios_base::sync_with_stdio(false);
  string s, t;
  cin >> s >> t;
  for (int i = 1; i <= s.size(); i++) {
    for (int j = 1; j <= t.size(); j++) {
      dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]);
      if (dp[i - 1][j] >= dp[i][j - 1])
        par[i][j] = 1;
      else
        par[i][j] = 2;
      if (s[i - 1] == t[j - 1]) {
        dp[i][j] = dp[i - 1][j - 1] + 1;
        par[i][j] = 3;
      }
    }
  }
  int x = s.size(), y = t.size();
  string ans;
  while (x > 0 and y > 0) {
    if (par[x][y] == 1) {
      x--;
    } else if (par[x][y] == 2) {
      y--;
    } else {
      ans += s[x - 1];
      x--, y--;
    }
  }
  reverse(ans.begin(), ans.end());
  cout << ans << endl;
}