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

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
const int M = 3e3 + 4;
string s, t, ans = "";
int dp[M][M], a, b, cnt, vis[M][M];
void fun(int x, int y) {
  if (x <= 0 || y <= 0 || cnt < 0)
    return;
  if (s[x] == t[y] && dp[x][y] == cnt) {
    ans += s[x];
    cnt--;
    fun(x - 1, y - 1);
  } else {
    fun(x, y - 1);
    fun(x - 1, y);
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cin >> s >> t;
  a = s.length();
  b = t.length();
  s = " " + s;
  t = " " + t;
  for (int i = 1; i <= a; i++) {
    for (int j = 1; j <= b; j++) {
      if (s[i] == t[j])
        dp[i][j] = max(dp[i][j], 1 + dp[i - 1][j - 1]);
      else
        dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]);
    }
  }
  cnt = dp[a][b];
  fun(a, b);
  reverse(ans.begin(), ans.end());
  cout << ans;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
const int M = 3e3 + 4;
string s, t, ans = "";
int dp[M][M], a, b, cnt, vis[M][M];
void fun(int x, int y) {
  if (x <= 0 || y <= 0 || cnt < 0)
    return;
  if (s[x] == t[y] && dp[x][y] == cnt) {
    ans += s[x];
    cnt--;
    fun(x - 1, y - 1);
  } else {
    if (dp[x][y - 1] > dp[x - 1][y])
      fun(x, y - 1);
    else
      fun(x - 1, y);
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cin >> s >> t;
  a = s.length();
  b = t.length();
  s = " " + s;
  t = " " + t;
  for (int i = 1; i <= a; i++) {
    for (int j = 1; j <= b; j++) {
      if (s[i] == t[j])
        dp[i][j] = max(dp[i][j], 1 + dp[i - 1][j - 1]);
      else
        dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]);
    }
  }
  cnt = dp[a][b];
  fun(a, b);
  reverse(ans.begin(), ans.end());
  cout << ans;
  return 0;
}
Problem p03165 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 1e3 + 2;
char res[N];
ll dp[N][N];

int main() {
  string s, t;
  cin >> s >> t;
  ll ns = s.length();
  ll nt = t.length();
  for (int i = 1; i <= ns; i++) {
    for (int j = 1; j <= nt; j++) {
      if (s[i - 1] == t[j - 1])
        dp[i][j] = dp[i - 1][j - 1] + 1;
      else
        dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]);
    }
  }

  ll i = ns;
  ll j = nt;
  ll rc = 0;

  while (i != 0 && j != 0) {
    if (s[i - 1] == t[j - 1]) {
      rc++;
      res[rc] = s[i - 1];
      i--;
      j--;
    } else {
      if (dp[i - 1][j] >= dp[i][j - 1])
        i--;
      else
        j--;
    }
  }

  for (int i = rc; i >= 1; i--)
    cout << res[i];
  cout << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 3e3 + 2;
char res[N];
ll dp[N][N];

int main() {
  string s, t;
  cin >> s >> t;
  ll ns = s.length();
  ll nt = t.length();
  for (int i = 1; i <= ns; i++) {
    for (int j = 1; j <= nt; j++) {
      if (s[i - 1] == t[j - 1])
        dp[i][j] = dp[i - 1][j - 1] + 1;
      else
        dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]);
    }
  }

  ll i = ns;
  ll j = nt;
  ll rc = 0;

  while (i != 0 && j != 0) {
    if (s[i - 1] == t[j - 1]) {
      rc++;
      res[rc] = s[i - 1];
      i--;
      j--;
    } else {
      if (dp[i - 1][j] >= dp[i][j - 1])
        i--;
      else
        j--;
    }
  }

  for (int i = rc; i >= 1; i--)
    cout << res[i];
  cout << endl;
  return 0;
}
Problem p03165 (C++)

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

int solution() {
  string s, t;
  getline(cin, s);
  getline(cin, t);
  ll n = s.length();
  ll m = t.length();

  vector<vector<ll>> dp(n + 1, vector<ll>(m + 1, 0));
  for (ll i = 1; i <= n; i++) {
    for (ll j = 1; j <= m; j++) {
      if (s[i - 1] == t[j - 1])
        dp[i][j] = dp[i - 1][j - 1] + 1;
      else
        dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]);
    }
  }
  string ans = "";
  ll i = n, j = m;
  while (i >= 0 && j >= 0) {
    if (s[i - 1] == t[j - 1]) {
      ans.insert(ans.begin(), s[i - 1]);
      i--;
      j--;
    } else if (dp[i - 1][j] > dp[i][j - 1])
      i--;
    else
      j--;
  }
  cout << ans << "\n";
  return 0;
}

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

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

int solution() {
  string s, t;
  getline(cin, s);
  getline(cin, t);
  ll n = s.length();
  ll m = t.length();

  vector<vector<ll>> dp(n + 1, vector<ll>(m + 1, 0));
  for (ll i = 1; i <= n; i++) {
    for (ll j = 1; j <= m; j++) {
      if (s[i - 1] == t[j - 1])
        dp[i][j] = dp[i - 1][j - 1] + 1;
      else
        dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]);
    }
  }
  string ans = "";
  ll i = n, j = m;
  while (i > 0 && j > 0) {
    if (s[i - 1] == t[j - 1]) {
      ans.insert(ans.begin(), s[i - 1]);
      i--;
      j--;
    } else if (dp[i - 1][j] > dp[i][j - 1])
      i--;
    else
      j--;
  }
  cout << ans << "\n";
  return 0;
}

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

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

using namespace std;

// int dp[3100][3100];
int dp[50][50];

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

  for (int i = 0; i < s.size(); ++i) {
    dp[i][0] = 0;
  }

  for (int j = 0; j < t.size(); ++j) {
    dp[0][j] = 0;
  }

  // memset(dp, 0, sizeof(dp));    // kottinoga raku

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

      } else {
        dp[i + 1][j + 1] = max(dp[i][j + 1], dp[i + 1][j]);
      }
    }
  }

  // fukugen
  string res = "";
  int i = (int)s.size();
  int j = (int)t.size();

  while (i > 0 && j > 0) {
    // (i-1, j) -> (i, j) と更新されていた場合
    if (dp[i][j] == dp[i - 1][j]) {
      --i; // DP の遷移を遡る
    }

    // (i, j-1) -> (i, j) と更新されていた場合
    else if (dp[i][j] == dp[i][j - 1]) {
      --j; // DP の遷移を遡る
    }

    // (i-1, j-1) -> (i, j) と更新されていた場合
    else {
      res = s[i - 1] +
            res; // このとき s[i-1] == t[j-1] なので、t[j-1] + res でも OK
      --i, --j; // DP の遷移を遡る
    }
  }

  // cout << dp[s.size()][t.size()] << endl;
  cout << res << endl;
}

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

using namespace std;

int dp[3100][3100];

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

  for (int i = 0; i < s.size(); ++i) {
    dp[i][0] = 0;
  }

  for (int j = 0; j < t.size(); ++j) {
    dp[0][j] = 0;
  }

  // memset(dp, 0, sizeof(dp));    // kottinoga raku

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

      } else {
        dp[i + 1][j + 1] = max(dp[i][j + 1], dp[i + 1][j]);
      }
    }
  }

  // fukugen
  string res = "";
  int i = (int)s.size();
  int j = (int)t.size();

  while (i > 0 && j > 0) {
    // (i-1, j) -> (i, j) と更新されていた場合
    if (dp[i][j] == dp[i - 1][j]) {
      --i; // DP の遷移を遡る
    }

    // (i, j-1) -> (i, j) と更新されていた場合
    else if (dp[i][j] == dp[i][j - 1]) {
      --j; // DP の遷移を遡る
    }

    // (i-1, j-1) -> (i, j) と更新されていた場合
    else {
      res = s[i - 1] +
            res; // このとき s[i-1] == t[j-1] なので、t[j-1] + res でも OK
      --i, --j; // DP の遷移を遡る
    }
  }

  // cout << dp[s.size()][t.size()] << endl;
  cout << res << 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 n = s.size(), m = t.size();
  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;
      else if (s[i - 1] == t[j - 1])
        dp[i][j] = 1 + dp[i - 1][j - 1];
      else
        dp[i][j] = max(dp[i][j - 1], dp[i - 1][j]);
    }
  }
  char ch[dp[n][m] + 1];
  int i = n, j = m;
  while (i > 0 || j > 0) {
    if (s[i - 1] == t[j - 1]) {
      ch[dp[i][j]] = s[i - 1];
      i--, j--;
    } else if (dp[i - 1][j] < dp[i][j - 1]) {
      j--;
    } else
      i--;
  }
  for (int i = 1; i <= dp[n][m]; i++)
    cout << ch[i];
  cout << endl;
}

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

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
const long long maxn = 2000;
long long f[maxn + 5][maxn + 5];
int main() {
  string s, s1 = "", ans = "";
  long long i, j, m, n;
  cin >> s1 >> s;
  if (s1.length() > s.length())
    swap(s1, s);
  m = s.length();
  n = s1.length();
  s = 'l' + s;
  s1 = 'l' + s1;
  for (i = 1; i <= m; i++) {
    for (j = 1; j <= n; j++) {
      if (s[i] == s1[j])
        f[i][j] = f[i - 1][j - 1] + 1;
      else
        f[i][j] = max(f[i - 1][j], f[i][j - 1]);
    }
  }
  while (f[m][n] > 0) {
    if (s[m] == s1[n])
      ans += s[m], m--, n--;
    else if (f[m][n] == f[m][n - 1])
      n--;
    else
      m--;
  }
  reverse(ans.begin(), ans.end());
  cout << ans << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
const long long maxn = 3000;
long long f[maxn + 5][maxn + 5];
int main() {
  string s, s1 = "", ans = "";
  long long i, j, m, n;
  cin >> s1 >> s;
  if (s1.length() > s.length())
    swap(s1, s);
  m = s.length();
  n = s1.length();
  s = 'l' + s;
  s1 = 'l' + s1;
  for (i = 1; i <= m; i++) {
    for (j = 1; j <= n; j++) {
      if (s[i] == s1[j])
        f[i][j] = f[i - 1][j - 1] + 1;
      else
        f[i][j] = max(f[i - 1][j], f[i][j - 1]);
    }
  }
  while (f[m][n] > 0) {
    if (s[m] == s1[n])
      ans += s[m], m--, n--;
    else if (f[m][n] == f[m][n - 1])
      n--;
    else
      m--;
  }
  reverse(ans.begin(), ans.end());
  cout << ans << endl;
}
Problem p03165 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
int dp[3001][3001];
int lcs(string s, string t, int n, int m) {
  if (n >= s.size() || m >= t.size()) {
    dp[n][m] = 0;
    return 0;
  }
  if (dp[n][m] != -1)
    return dp[n][m];
  int ans;
  if (s[n] == t[m]) {
    if (dp[n + 1][m + 1] == -1)
      dp[n + 1][m + 1] = lcs(s, t, n + 1, m + 1);
    ans = 1 + dp[n + 1][m + 1];
  } else {
    if (dp[n][m + 1] == -1)
      dp[n][m + 1] = lcs(s, t, n, m + 1);
    if (dp[n + 1][m] == -1)
      dp[n + 1][m] = lcs(s, t, n + 1, m);
    if (dp[n + 1][m] > dp[n][m + 1])
      ans = dp[n + 1][m];
    else
      ans = dp[n][m + 1];
  }
  dp[n][m] = ans;
  return ans;
}
int main() {
  for (int i = 0; i < 3000; i++)
    for (int j = 0; j <= 3000; j++)
      dp[i][j] = -1;
  string s, t;
  cin >> s >> t;
  lcs(s, t, 0, 0);
  string str = "";
  int i = 0, j = 0;
  while (i < s.size() && j < t.size()) {
    if (s[i] == t[j]) {
      str += s[i];
      i++;
      j++;
    } else if (dp[i + 1][j] > dp[i][j + 1])
      i++;
    else
      j++;
  }
  cout << str << endl;
  return 0;
}

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

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
typedef long long ll;
#define w first
#define v second
#define ii pair<ll, ll>
using namespace std;

string s, t;
const int N = 1010;
int m, n;
int mem[N][N];

int dp(int i, int j) {
  if (i == n || j == m)
    return 0;
  int &ret = mem[i][j];
  if (ret != -1)
    return ret;
  ret = 0;
  if (s[i] == t[j])
    ret = 1 + dp(i + 1, j + 1);
  return ret = max({ret, dp(i + 1, j), dp(i, j + 1)});
}

void getpath(int i, int j) {
  if (i == n || j == m)
    return;
  int &ret = mem[i][j];
  if (s[i] == t[j]) {
    printf("%c", s[i]);
    getpath(i + 1, j + 1);
    return;
  }
  if (mem[i + 1][j] > mem[i][j + 1])
    getpath(i + 1, j);
  else
    getpath(i, j + 1);
}
int main() {
  memset(mem, -1, sizeof mem);
  cin >> s >> t;
  n = s.length();
  m = t.length();
  // printf("%d\n" , dp(0,0));
  dp(0, 0);
  getpath(0, 0);
}


Fixed submission:
#include <bits/stdc++.h>
typedef long long ll;
#define w first
#define v second
#define ii pair<ll, ll>
using namespace std;

string s, t;
const int N = 3010;
int m, n;
int mem[N][N];

int dp(int i, int j) {
  if (i == n || j == m)
    return 0;
  int &ret = mem[i][j];
  if (ret != -1)
    return ret;
  ret = 0;
  if (s[i] == t[j])
    ret = 1 + dp(i + 1, j + 1);
  return ret = max({ret, dp(i + 1, j), dp(i, j + 1)});
}

void getpath(int i, int j) {
  if (i == n || j == m)
    return;
  int &ret = mem[i][j];
  if (s[i] == t[j]) {
    printf("%c", s[i]);
    getpath(i + 1, j + 1);
    return;
  }
  if (mem[i + 1][j] > mem[i][j + 1])
    getpath(i + 1, j);
  else
    getpath(i, j + 1);
}
int main() {
  memset(mem, -1, sizeof mem);
  cin >> s >> t;
  n = s.length();
  m = t.length();
  // printf("%d\n" , dp(0,0));
  dp(0, 0);
  getpath(0, 0);
}
Problem p03165 (C++)

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

typedef long long ll;
#define all(x) (x).begin(), (x).end()
const int mod = 1000000007;
const ll INF = 1LL << 60;

int main() {

  string S, T;
  cin >> S >> T;
  vector<vector<int>> dp(S.size() + 2, vector<int>(T.size() + 2, 0));
  vector<vector<pair<int, int>>> par(
      S.size() + 2, vector<pair<int, int>>(T.size() + 2, make_pair(-1, -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] = dp[i - 1][j - 1] + 1;
        par[i][j] = make_pair(i - 1, j - 1);
      } else {
        if (dp[i - 1][j] >= dp[i][j - 1]) {
          dp[i][j] = dp[i - 1][j];
          par[i][j] = make_pair(i - 1, j);
        } else {
          dp[i][j] = dp[i][j - 1];
          par[i][j] = make_pair(i, j - 1);
        }
      }
    }
  }
  int i = S.size();
  int j = T.size();
  string ans;
  while (i > 0 || j > 0) {
    if (dp[i][j] != dp[par[i][j].first][par[i][j].second])
      ans += S[par[i][j].first];
    int m = i, n = j;
    i = par[m][n].first;
    j = par[m][n].second;
  }
  reverse(all(ans));
  cout << ans << endl;
}


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

typedef long long ll;
#define all(x) (x).begin(), (x).end()
const int mod = 1000000007;
const ll INF = 1LL << 60;

int main() {

  string S, T;
  cin >> S >> T;
  vector<vector<int>> dp(S.size() + 2, vector<int>(T.size() + 2, 0));
  vector<vector<pair<int, int>>> par(
      S.size() + 2, vector<pair<int, int>>(T.size() + 2, make_pair(-1, -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] = dp[i - 1][j - 1] + 1;
        par[i][j] = make_pair(i - 1, j - 1);
      } else {
        if (dp[i - 1][j] >= dp[i][j - 1]) {
          dp[i][j] = dp[i - 1][j];
          par[i][j] = make_pair(i - 1, j);
        } else {
          dp[i][j] = dp[i][j - 1];
          par[i][j] = make_pair(i, j - 1);
        }
      }
    }
  }
  int i = S.size();
  int j = T.size();
  string ans;
  while (i && j) {
    if (dp[i][j] != dp[par[i][j].first][par[i][j].second])
      ans += S[par[i][j].first];
    int m = i, n = j;
    i = par[m][n].first;
    j = par[m][n].second;
  }
  reverse(all(ans));
  cout << ans << endl;
}
Problem p03165 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
int dp[2001][2001];
int main() {
  string s, t;
  cin >> s;
  cin >> t;
  int lens = s.length();
  int lent = t.length();

  // dp[i][j] is len lcs of 0-i-1 and 0-j-1
  // dp[i][j] = if(s[i-1]==t[j-1]) 1 + dp[i-1][j-1]
  // dp[i][j] = max(dp[i-1][j],dp[i][j-1])
  // int dp[lens+1][lent+1] = {0};
  dp[0][0] = 0;
  for (int i = 1; i <= lens; i++) {
    for (int j = 1; j <= lent; 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]);
      }
      // printf("dp[%d][%d] = %d\n",i,j,dp[i][j]);
    }
  }
  int i = lens;
  int j = lent;
  string ans = "";
  while (i >= 1 && j >= 1) {
    if (s[i - 1] == t[j - 1]) {
      i--;
      j--;
      ans += s[i];
    } else {
      if (dp[i - 1][j] > dp[i][j - 1]) {
        i--;
      } else {
        j--;
      }
    }
  }
  reverse(ans.begin(), ans.end());
  // cout<<dp[lens][lent]<<endl;
  cout << ans << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int dp[3001][3001];
int main() {
  string s, t;
  cin >> s;
  cin >> t;
  int lens = s.length();
  int lent = t.length();

  // dp[i][j] is len lcs of 0-i-1 and 0-j-1
  // dp[i][j] = if(s[i-1]==t[j-1]) 1 + dp[i-1][j-1]
  // dp[i][j] = max(dp[i-1][j],dp[i][j-1])
  // int dp[lens+1][lent+1] = {0};
  dp[0][0] = 0;
  for (int i = 1; i <= lens; i++) {
    for (int j = 1; j <= lent; 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]);
      }
      // printf("dp[%d][%d] = %d\n",i,j,dp[i][j]);
    }
  }
  int i = lens;
  int j = lent;
  string ans = "";
  while (i >= 1 && j >= 1) {
    if (s[i - 1] == t[j - 1]) {
      i--;
      j--;
      ans += s[i];
    } else {
      if (dp[i - 1][j] > dp[i][j - 1]) {
        i--;
      } else {
        j--;
      }
    }
  }
  reverse(ans.begin(), ans.end());
  // cout<<dp[lens][lent]<<endl;
  cout << ans << endl;
}
Problem p03165 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
const int N = 1e3;
string s, t;
int dp[N + 1][N + 1];
int f(unsigned int i, unsigned int j) {
  if (i == s.size() || j == t.size())
    return 0;
  if (dp[i][j] != -1)
    return dp[i][j];
  if (s[i] == t[j])
    return dp[i][j] = f(i + 1, j + 1) + 1;
  return dp[i][j] = max(f(i + 1, j), f(i, j + 1));
}
void p(unsigned int i, unsigned int j) {
  if (i == s.size() || j == t.size())
    return;
  if (s[i] == t[j]) {
    cout << s[i];
    p(i + 1, j + 1);
    return;
  }
  if (i == s.size() - 1)
    return p(i, j + 1);
  if (j == t.size() - 1)
    return p(i + 1, j);
  if (dp[i + 1][j] > dp[i][j + 1])
    return p(i + 1, j);
  return p(i, j + 1);
}
int main() {
  cin >> s >> t;
  memset(dp, -1, sizeof dp);
  f(0, 0);
  p(0, 0);
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
const int N = 3 * 1e3;
string s, t;
int dp[N + 1][N + 1];
int f(unsigned int i, unsigned int j) {
  if (i == s.size() || j == t.size())
    return 0;
  if (dp[i][j] != -1)
    return dp[i][j];
  if (s[i] == t[j])
    return dp[i][j] = f(i + 1, j + 1) + 1;
  return dp[i][j] = max(f(i + 1, j), f(i, j + 1));
}
void p(unsigned int i, unsigned int j) {
  if (i == s.size() || j == t.size())
    return;
  if (s[i] == t[j]) {
    cout << s[i];
    p(i + 1, j + 1);
    return;
  }
  if (i == s.size() - 1)
    return p(i, j + 1);
  if (j == t.size() - 1)
    return p(i + 1, j);
  if (dp[i + 1][j] > dp[i][j + 1])
    return p(i + 1, j);
  return p(i, j + 1);
}
int main() {
  cin >> s >> t;
  memset(dp, -1, sizeof dp);
  f(0, 0);
  p(0, 0);
  return 0;
}
Problem p03165 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define MAXN 3010
#define pii pair<int, int>
#define pb push_back
typedef long long ll;

using namespace std;

int n, m, dp[MAXN][MAXN];
int b[MAXN][MAXN];
string s, t;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cin >> s >> t;
  n = s.size();
  m = t.size();
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      if (s[i - 1] == t[j - 1]) {
        dp[i][j] = dp[i - 1][j - 1] + 1;
        b[i][j] = 2;
        continue;
      }
      if (dp[i - 1][j] < dp[i][j - 1]) {
        dp[i][j] = dp[i][j - 1];
        b[i][j] = 1;
      } else {
        dp[i][j] = dp[i - 1][j];
        b[i][j] = 0;
      }
    }
  }
  vector<char> v;
  // cout << dp[n][m];
  int i = n, j = m;
  while (i > 0 or j > 0) {
    if (b[i][j] == 2) {
      v.pb(s[i - 1]);
      i--;
      j--;
    }
    if (b[i][j] == 1)
      j--;
    if (b[i][j] == 0)
      i--;
  }
  reverse(v.begin(), v.end());
  for (auto i : v)
    cout << i;
}

Fixed submission:
#include <bits/stdc++.h>
#define MAXN 3010
#define pii pair<int, int>
#define pb push_back
typedef long long ll;

using namespace std;

int n, m, dp[MAXN][MAXN];
int b[MAXN][MAXN];
string s, t;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cin >> s >> t;
  n = s.size();
  m = t.size();
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      if (s[i - 1] == t[j - 1]) {
        dp[i][j] = dp[i - 1][j - 1] + 1;
        b[i][j] = 2;
        continue;
      }
      if (dp[i - 1][j] < dp[i][j - 1]) {
        dp[i][j] = dp[i][j - 1];
        b[i][j] = 1;
      } else {
        dp[i][j] = dp[i - 1][j];
        b[i][j] = 0;
      }
    }
  }
  vector<char> v;
  // cout << dp[n][m];
  int i = n, j = m;
  while (i > 0 and j > 0) {
    if (b[i][j] == 2) {
      v.pb(s[i - 1]);
      i--;
      j--;
    }
    if (b[i][j] == 1)
      j--;
    if (b[i][j] == 0)
      i--;
  }
  reverse(v.begin(), v.end());
  for (auto i : v)
    cout << i;
}
Problem p03165 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
using ll = long long int;
using namespace std;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
ll mod = 1e9 + 7;

int main() {
  int dp[100][100] = {};

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

  rep(a, s.size()) {
    rep(b, t.size()) {
      if (s[a] == t[b]) {
        dp[a + 1][b + 1] = dp[a][b] + 1;
      }
      chmax(dp[a + 1][b + 1], dp[a + 1][b]);
      chmax(dp[a + 1][b + 1], dp[a][b + 1]);
    }
  }

  string ans = "";
  int a = (int)s.size(), b = (int)t.size();

  while (a > 0 && b > 0) {
    if (dp[a][b] == dp[a - 1][b]) {
      a--;
    } else if (dp[a][b] == dp[a][b - 1]) {
      b--;
    } else {
      ans = s[a - 1] + ans;
      a--, b--;
    }
  }

  cout << ans << endl;
}

Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
using ll = long long int;
using namespace std;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
ll mod = 1e9 + 7;

int main() {
  int dp[3010][3010] = {};

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

  rep(a, s.size()) {
    rep(b, t.size()) {
      if (s[a] == t[b]) {
        dp[a + 1][b + 1] = dp[a][b] + 1;
      }
      chmax(dp[a + 1][b + 1], dp[a + 1][b]);
      chmax(dp[a + 1][b + 1], dp[a][b + 1]);
    }
  }

  string ans = "";
  int a = (int)s.size(), b = (int)t.size();

  while (a > 0 && b > 0) {
    if (dp[a][b] == dp[a - 1][b]) {
      a--;
    } else if (dp[a][b] == dp[a][b - 1]) {
      b--;
    } else {
      ans = s[a - 1] + ans;
      a--, b--;
    }
  }

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

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define ll long long
#define PB push_back
#define MP make_pair

#define REP(i, n) for (i = 0; i < (n); ++i)
#define FOR(i, l, h) for (i = (l); i <= (h); ++i)
#define FORD(i, h, l) for (i = (h); i >= (l); --i)

#define max(a, b) a > b ? a : b
#define min(a, b) a > b ? b : a
using namespace std;

//*********************************
//******problem F DP contest LCS ***

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  string s1, s2;
  cin >> s1 >> s2;
  ll int m, n;
  n = s1.length();
  m = s2.length();
  int dp[m + 2][n + 2];
  //  memset(dp,0,sizeof(dp));
  for (ll int i = 0; i <= n; i++) {
    for (ll int j = 0; j <= m; j++) {
      if (i == 0 || j == 0)
        dp[i][j] = 0;
      else if (s1[i - 1] == s2[j - 1]) {
        dp[i][j] = dp[i - 1][j - 1] + 1;
        // ans += s1[i];
      } else
        dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]);
    }
  }
  int lclen = dp[n][m];
  string ans(lclen, '.');
  ll int i = n, j = m;
  while (i > 0 && j > 0) {
    if (s1[i - 1] == s2[j - 1]) {
      ans[lclen - 1] = s1[i - 1];
      i--;
      j--;
      lclen--;
    } else if (dp[i - 1][j] < dp[i][j - 1])
      j--;
    else
      i--;
  }
  // cout<<dp[n][m]<<"\n";
  // reverse(ans.begin(),ans.end());
  cout << ans;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define ll long long
#define PB push_back
#define MP make_pair

#define REP(i, n) for (i = 0; i < (n); ++i)
#define FOR(i, l, h) for (i = (l); i <= (h); ++i)
#define FORD(i, h, l) for (i = (h); i >= (l); --i)

#define max(a, b) a > b ? a : b
#define min(a, b) a > b ? b : a
using namespace std;

//*********************************
//******problem F DP contest LCS ***

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  string s1, s2;
  cin >> s1 >> s2;
  ll int m, n;
  n = s1.length();
  m = s2.length();
  vector<vector<int>> dp(n + 2, vector<int>(m + 2, 0));
  //  memset(dp,0,sizeof(dp));
  for (ll int i = 0; i <= n; i++) {
    for (ll int j = 0; j <= m; j++) {
      if (i == 0 || j == 0)
        dp[i][j] = 0;
      else if (s1[i - 1] == s2[j - 1]) {
        dp[i][j] = dp[i - 1][j - 1] + 1;
        // ans += s1[i];
      } else
        dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]);
    }
  }
  int lclen = dp[n][m];
  string ans(lclen, '.');
  ll int i = n, j = m;
  while (i > 0 && j > 0) {
    if (s1[i - 1] == s2[j - 1]) {
      ans[lclen - 1] = s1[i - 1];
      i--;
      j--;
      lclen--;
    } else if (dp[i - 1][j] < dp[i][j - 1])
      j--;
    else
      i--;
  }
  // cout<<dp[n][m]<<"\n";
  // reverse(ans.begin(),ans.end());
  cout << ans;
  return 0;
}
Problem p03165 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
int main() {
  string str = "";
  string a, b;
  cin >> a >> b;
  int sa = a.length();
  int sb = b.length();
  int dp[sb + 1][sa + 1];
  for (int i = 0; i < sa + 1; i++)
    dp[0][i] = 0;
  for (int i = 0; i < sb + 1; i++)
    dp[i][0] = 0;
  for (int i = 1; i < sb + 1; i++)
    for (int j = 1; j < sa + 1; j++) {
      if (b[i - 1] == a[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 = sb;
  int j = sa;
  while (i || j) {
    if (a[j - 1] == b[i - 1]) {
      str += a[j - 1];
      i--;
      j--;
      continue;
    }
    if (dp[i - 1][j] > dp[i][j - 1])
      i--;
    else
      j--;
  }
  reverse(str.begin(), str.end());
  cout << str;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int main() {
  string str = "";
  string a, b;
  cin >> a >> b;
  int sa = a.length();
  int sb = b.length();
  int dp[sb + 1][sa + 1];
  for (int i = 0; i < sa + 1; i++)
    dp[0][i] = 0;
  for (int i = 0; i < sb + 1; i++)
    dp[i][0] = 0;
  for (int i = 1; i < sb + 1; i++)
    for (int j = 1; j < sa + 1; j++) {
      if (b[i - 1] == a[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 = sb;
  int j = sa;
  while (i > 0 && j > 0) {
    if (a[j - 1] == b[i - 1]) {
      str += a[j - 1];
      i--;
      j--;
      continue;
    }
    if (dp[i - 1][j] > dp[i][j - 1])
      i--;
    else
      j--;
  }
  reverse(str.begin(), str.end());
  cout << str;
}
Problem p03165 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <string.h>
#include <string>
#include <vector>
using namespace std;
int dp[3001][3001];
int main() {
  char s[3001], s1[3001];
  cin >> s >> s1;
  int len = strlen(s), len1 = strlen(s1);
  for (int i = 1; i <= len; i++) {
    for (int j = 1; j <= len1; j++) {
      if (s[i - 1] == s1[j - 1]) {
        dp[i][j] = dp[i - 1][j - 1] + 1;
      } else
        dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]);
    }
  }
  vector<char> s2;
  int i = len, j = len1;
  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 {
      s2.push_back(s[i - 1]);
      i--, j--;
    }
  }
  reverse(s2.begin(), s2.end());
  for (char c : s2)
    cout << c;
}


Fixed submission:
#include <algorithm>
#include <iostream>
#include <string.h>
#include <string>
#include <vector>
using namespace std;
int dp[3001][3001];
int main() {
  char s[3001], s1[3001];
  cin >> s >> s1;
  int len = strlen(s), len1 = strlen(s1);
  for (int i = 1; i <= len; i++) {
    for (int j = 1; j <= len1; j++) {
      if (s[i - 1] == s1[j - 1]) {
        dp[i][j] = dp[i - 1][j - 1] + 1;
      } else
        dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]);
    }
  }
  vector<char> s2;
  int i = len, j = len1;
  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 {
      s2.push_back(s[i - 1]);
      i--, j--;
    }
  }
  reverse(s2.begin(), s2.end());
  for (char c : s2)
    cout << c;
}
Problem p03165 (C++)

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

using namespace std;
string a, b, s = "";
int arr[3001][3001], alen, blen;

void dp(string a, string b) {
  alen = a.length(), blen = b.length();
  for (int i = 0; i <= blen; i++) {
    for (int j = 0; j <= alen; j++) {
      int k = i - 1, l = j - 1;
      if (i == 0 || j == 0)
        arr[i][j] = 0;
      else {
        if (a[l] == b[k]) {
          arr[i][j] = 1 + arr[i - 1][j - 1];
        } else
          arr[i][j] += max(arr[i - 1][j], arr[i][j - 1]);
      }
    }
  }
  int curmax = arr[blen][alen];

  for (int i = blen; i > 0; i--) {
    for (int j = alen; j > 0; j--) {
      if (arr[i][j] == curmax && arr[i][j] > arr[i - 1][j] &&
          arr[i][j] > arr[i][j - 1]) {
        s = a[j - 1] + s;
        curmax -= 1;
        if (j != 1)
          i -= 1;
      }
    }
  }
}
int main() {
  cin >> a >> b;
  dp(a, b);
  cout << s;
}

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

using namespace std;
string a, b, s = "";
int arr[3001][3001], alen, blen;

void dp(string a, string b) {
  alen = a.length(), blen = b.length();
  for (int i = 0; i <= blen; i++) {
    for (int j = 0; j <= alen; j++) {
      int k = i - 1, l = j - 1;
      if (i == 0 || j == 0)
        arr[i][j] = 0;
      else {
        if (a[l] == b[k]) {
          arr[i][j] = 1 + arr[i - 1][j - 1];
        } else
          arr[i][j] += max(arr[i - 1][j], arr[i][j - 1]);
      }
    }
  }
  int curmax = arr[blen][alen];
  int i = blen, j = alen;
  while (i > 0 && j > 0) {
    if (arr[i][j] == curmax && arr[i][j] > arr[i - 1][j] &&
        arr[i][j] > arr[i][j - 1]) {
      s = a[j - 1] + s;
      curmax -= 1;
      i -= 1;
      j -= 1;
    } else if (arr[i - 1][j] > arr[i][j - 1])
      i -= 1;
    else
      j -= 1;
  }
}
int main() {
  cin >> a >> b;
  dp(a, b);
  cout << s;
}
Problem p03165 (C++)

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

int main() {
  string a, b;
  cin >> a;
  cin >> b;
  a = ' ' + a;
  b = ' ' + b;
  int A = a.length();
  int B = b.length();
  int dp[A][B];
  for (int i = 0; i < A; i++) {
    for (int j = 0; j < B; j++) {
      if (i == 0 || j == 0) {
        dp[i][j] = 0;
        continue;
      } else if (a[i] == b[j]) {
        dp[i][j] = 1 + dp[i - 1][j - 1];
      } else {
        dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]);
      }
    }
  }
  int i = A - 1, j = B - 1;
  string ans = "";
  while (i > 0 || j > 0) {
    if (a[i] == b[j]) {
      ans = a[i] + ans;
      i--;
      j--;
    } else if (dp[i - 1][j] > dp[i][j - 1]) {
      i--;
    } else {
      j--;
    }
  }
  cout << ans;
}


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

int main() {
  string a, b;
  cin >> a;
  cin >> b;
  a = ' ' + a;
  b = ' ' + b;
  int A = a.length();
  int B = b.length();
  int dp[A][B];
  for (int i = 0; i < A; i++) {
    for (int j = 0; j < B; j++) {
      if (i == 0 || j == 0) {
        dp[i][j] = 0;
        continue;
      } else if (a[i] == b[j]) {
        dp[i][j] = 1 + dp[i - 1][j - 1];
      } else {
        dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]);
      }
    }
  }
  int i = A - 1, j = B - 1;
  string ans = "";
  while (i > 0 && j > 0) {
    if (a[i] == b[j]) {
      ans = a[i] + ans;
      i--;
      j--;
    } else if (dp[i - 1][j] > dp[i][j - 1]) {
      i--;
    } else {
      j--;
    }
  }
  cout << ans;
}
Problem p03165 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
string s1, s2;
static int t[2001][2001];
void nhap() { cin >> s1 >> s2; }
void qhd() {
  for (int i = 1; i <= (int)s1.length(); i++)
    for (int j = 1; j <= (int)s2.length(); j++)
      if (s1[i - 1] == s2[j - 1])
        t[i][j] = t[i - 1][j - 1] + 1;
      else
        t[i][j] = max(t[i][j - 1], t[i - 1][j]);

  int i = (int)s1.length();
  int j = (int)s2.length();
  string res = "";
  while (i != 0 && j != 0) {
    if (s1[i - 1] == s2[j - 1]) {
      res = s1[i - 1] + res;
      i--;
      j--;
    } else if (t[i][j] == t[i - 1][j])
      i--;
    else
      j--;
  }
  cout << res;
}
int main() {
  nhap();
  qhd();
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
string s1, s2;
static int t[3005][3005];
void nhap() { cin >> s1 >> s2; }
void qhd() {
  for (int i = 1; i <= (int)s1.length(); i++)
    for (int j = 1; j <= (int)s2.length(); j++)
      if (s1[i - 1] == s2[j - 1])
        t[i][j] = t[i - 1][j - 1] + 1;
      else
        t[i][j] = max(t[i][j - 1], t[i - 1][j]);

  int i = (int)s1.length();
  int j = (int)s2.length();
  string res = "";
  while (i != 0 && j != 0) {
    if (s1[i - 1] == s2[j - 1]) {
      res = s1[i - 1] + res;
      i--;
      j--;
    } else if (t[i][j] == t[i - 1][j])
      i--;
    else
      j--;
  }
  cout << res;
}
int main() {
  nhap();
  qhd();
  return 0;
}
Problem p03165 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; ++i)
#define ALL(v) v.begin(), v.end()
typedef long long ll;
typedef pair<int, int> P;
const int INF = 1000000007;

int dp[3000 + 1][3000 + 1];

int main() {
  string s, t, ans;
  cin >> s >> t;
  fill(dp[0], dp[3000 + 1], 0);
  for (int i = 0; i < s.size(); ++i) {
    int foo = 0;
    for (int j = 0; j < t.size(); ++j) {
      if (s[i] == t[j]) {
        dp[i + 1][j + 1] = dp[i][j] + 1;

      } else {
        if (dp[i][j + 1] > dp[i + 1][j]) {
          dp[i + 1][j + 1] = dp[i][j + 1];
        } else {
          dp[i + 1][j + 1] = dp[i + 1][j];
        }
      }
    }
  }

  int sl = s.size();
  int tl = t.size();
  int l = dp[sl][tl];

  while (l > -1) {

    if (s[sl - 1] == t[tl - 1]) {
      ans.push_back(s[sl - 1]);
      sl--;
      tl--;
      l--;

    } else if (dp[sl][tl] == dp[sl - 1][tl]) {
      sl--;
    } else {
      tl--;
    }
  }
  reverse(ALL(ans));
  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; ++i)
#define ALL(v) v.begin(), v.end()
typedef long long ll;
typedef pair<int, int> P;
const int INF = 1000000007;

int dp[3000 + 1][3000 + 1];

int main() {
  string s, t, ans;
  cin >> s >> t;
  fill(dp[0], dp[3000 + 1], 0);
  for (int i = 0; i < s.size(); ++i) {
    int foo = 0;
    for (int j = 0; j < t.size(); ++j) {
      if (s[i] == t[j]) {
        dp[i + 1][j + 1] = dp[i][j] + 1;

      } else {
        if (dp[i][j + 1] > dp[i + 1][j]) {
          dp[i + 1][j + 1] = dp[i][j + 1];
        } else {
          dp[i + 1][j + 1] = dp[i + 1][j];
        }
      }
    }
  }

  int sl = s.size();
  int tl = t.size();
  int l = dp[sl][tl];

  while (l > 0) {

    if (s[sl - 1] == t[tl - 1]) {
      ans.push_back(s[sl - 1]);
      sl--;
      tl--;
      l--;

    } else if (dp[sl][tl] == dp[sl - 1][tl]) {
      sl--;
    } else {
      tl--;
    }
  }
  reverse(ALL(ans));
  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;
#define fi first
#define sec second
#define MOD 1000000007
#define pb push_back
#define test cout << "debuged\n";
#define INF 10000000000
#define si 1000000
#define debug(x) cout << x << "\n";

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output", "w", stdout);
#endif
  string s, d;
  cin >> s >> d;
  ll n = s.size(), m = d.size();
  ll dp[n + 10][m + 10];
  vector<char> ans;
  for (ll i = 0; i <= n; i++)
    for (ll j = 0; j <= m; j++)
      dp[i][j] = 0;

  for (ll i = 1; i <= n; i++) {
    for (ll j = 1; j <= m; j++) {
      if (s[i - 1] == d[j - 1]) {
        dp[i][j] = dp[i - 1][j - 1] + 1;
      } else {
        dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]);
      }
    }
  }
  ll i = n, j = m;
  while (j > 0 and i > 0) {
    if (dp[i][j] == dp[i - 1][j]) {
      i--;
    } else if (dp[i][j] == dp[i][j - 1]) {
      j--;
    } else {
      ans.pb(s[i - 1]);
      i--;
      j--;
    }
  }
  reverse(ans.begin(), ans.end());
  for (auto x : ans)
    cout << x;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
#define fi first
#define sec second
#define MOD 1000000007
#define pb push_back
#define test cout << "debuged\n";
#define INF 10000000000
#define si 1000000
#define debug(x) cout << x << "\n";

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

  string s, d;
  cin >> s >> d;
  ll n = s.size(), m = d.size();
  ll dp[n + 10][m + 10];
  vector<char> ans;
  for (ll i = 0; i <= n; i++)
    for (ll j = 0; j <= m; j++)
      dp[i][j] = 0;

  for (ll i = 1; i <= n; i++) {
    for (ll j = 1; j <= m; j++) {
      if (s[i - 1] == d[j - 1]) {
        dp[i][j] = dp[i - 1][j - 1] + 1;
      } else {
        dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]);
      }
    }
  }
  ll i = n, j = m;
  while (j > 0 and i > 0) {
    if (dp[i][j] == dp[i - 1][j]) {
      i--;
    } else if (dp[i][j] == dp[i][j - 1]) {
      j--;
    } else {
      ans.pb(s[i - 1]);
      i--;
      j--;
    }
  }
  reverse(ans.begin(), ans.end());
  for (auto x : ans)
    cout << x;
}
Problem p03165 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <vector>
#define ll long long
#define pii pair<int, int>
#define pll pair<ll, ll>
#define vii vector<int>
#define vll vector<ll>
#define lb lower_bound
#define pb push_back
#define mp make_pair
#define rep(i, n) for (ll i = 0; i < n; i++)
#define rep2(i, a, b) for (ll i = a; i < b; i++)
#define repr(i, n) for (ll i = n - 1; i >= 0; i--)
#define all(x) x.begin(), x.end()
#define INF (ll)1e9 + 7
#define LLINF (ll)1e30
// #define int ll
using namespace std;
const int MOD = 1e9 + 7;

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  string s, t, res = "";
  cin >> s >> t;
  int T = t.size(), S = s.size();
  int dp[301][301] = {};
  rep(i, S) {
    rep(j, T) {
      if (s[i] == t[j]) {
        dp[i + 1][j + 1] = max(dp[i][j] + 1, dp[i + 1][j + 1]);
      }
      dp[i + 1][j + 1] = max(dp[i][j + 1], dp[i + 1][j + 1]);
      dp[i + 1][j + 1] = max(dp[i + 1][j], dp[i + 1][j + 1]);
    }
  }
  while (S > 0 && T > 0) {
    if (dp[S][T] == dp[S - 1][T])
      S--;
    else if (dp[S][T] == dp[S][T - 1])
      T--;
    else {
      res = s[S - 1] + res;
      T--;
      S--;
    }
  }
  cout << res << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <vector>
#define ll long long
#define pii pair<int, int>
#define pll pair<ll, ll>
#define vii vector<int>
#define vll vector<ll>
#define lb lower_bound
#define pb push_back
#define mp make_pair
#define rep(i, n) for (ll i = 0; i < n; i++)
#define rep2(i, a, b) for (ll i = a; i < b; i++)
#define repr(i, n) for (ll i = n - 1; i >= 0; i--)
#define all(x) x.begin(), x.end()
#define INF (ll)1e9 + 7
#define LLINF (ll)1e30
// #define int ll
using namespace std;
const int MOD = 1e9 + 7;

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  string s, t, res = "";
  cin >> s >> t;
  int T = t.size(), S = s.size();
  int dp[3010][3010] = {};
  rep(i, S) {
    rep(j, T) {
      if (s[i] == t[j]) {
        dp[i + 1][j + 1] = max(dp[i][j] + 1, dp[i + 1][j + 1]);
      }
      dp[i + 1][j + 1] = max(dp[i][j + 1], dp[i + 1][j + 1]);
      dp[i + 1][j + 1] = max(dp[i + 1][j], dp[i + 1][j + 1]);
    }
  }
  while (S > 0 && T > 0) {
    if (dp[S][T] == dp[S - 1][T])
      S--;
    else if (dp[S][T] == dp[S][T - 1])
      T--;
    else {
      res = s[S - 1] + res;
      T--;
      S--;
    }
  }
  cout << res << endl;
  return 0;
}
Problem p03165 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
int dp[1002][1002];
string sa, sb;
int rec(int a, int b) {
  if (a == sa.size() or b == sb.size())
    return 0;
  if (dp[a][b] != -1)
    return dp[a][b];
  //    dp[a][b]=0;
  if (sa[a] == sb[b]) {
    dp[a][b] = rec(a + 1, b + 1) + 1;
  } else
    dp[a][b] = max(rec(a + 1, b), rec(a, b + 1));
  return dp[a][b];
}
void recons(int a, int b) {
  if (a == sa.size() or b == sb.size())
    return;
  else if (dp[a][b] == rec(a + 1, b + 1) + 1 and sa[a] == sb[b]) {
    cout << sb[b];
    recons(a + 1, b + 1);
  } else {
    if (dp[a][b] == rec(a + 1, b))
      recons(a + 1, b);
    else if (dp[a][b] == rec(a, b + 1))
      recons(a, b + 1);
  }
}

int main() {
  //    freopen("entra.in","r",stdin);
  //    freopen("sale.out","w",stdout);
  //   cin>>sa>>sb;
  while (getline(cin, sa)) {
    getline(cin, sb);
    memset(dp, -1, sizeof dp);
    rec(0, 0);
    recons(0, 0);
    cout << "\n";
  }
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int dp[3003][3003];
string sa, sb;
int rec(int a, int b) {
  if (a == sa.size() or b == sb.size())
    return 0;
  if (dp[a][b] != -1)
    return dp[a][b];
  //    dp[a][b]=0;
  if (sa[a] == sb[b]) {
    dp[a][b] = rec(a + 1, b + 1) + 1;
  } else
    dp[a][b] = max(rec(a + 1, b), rec(a, b + 1));
  return dp[a][b];
}
void recons(int a, int b) {
  if (a == sa.size() or b == sb.size())
    return;
  else if (dp[a][b] == rec(a + 1, b + 1) + 1 and sa[a] == sb[b]) {
    cout << sb[b];
    recons(a + 1, b + 1);
  } else {
    if (dp[a][b] == rec(a + 1, b))
      recons(a + 1, b);
    else if (dp[a][b] == rec(a, b + 1))
      recons(a, b + 1);
  }
}

int main() {
  //    freopen("entra.in","r",stdin);
  //    freopen("sale.out","w",stdout);
  //   cin>>sa>>sb;
  while (getline(cin, sa)) {
    getline(cin, sb);
    memset(dp, -1, sizeof dp);
    rec(0, 0);
    recons(0, 0);
    cout << "\n";
  }
}
Problem p03165 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <vector>
#define rep(i, n) for (int i = 0; i < n; ++i)
using namespace std;
int N;
string s, t;
int DP[3002][3002]; // sのi文字目までとtのj文字目まで探索し、その最大共通個数

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

  cin >> s >> t;
  rep(i, s.size()) {
    rep(j, t.size()) {
      if (s[i] == t[j]) {
        DP[i + 1][j + 1] = max(DP[i][j] + 1, DP[i + 1][j + 1]);
      }
      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]);
    }
  }

  string ans = "";
  int i = s.size();
  int j = t.size();
  while (i > 1 && j > 1) {
    if (DP[i][j] == DP[i - 1][j])
      i--;
    if (DP[i][j] == DP[i][j - 1])
      j--;
    if (DP[i][j] == DP[i - 1][j - 1]) {
      ans = ans[i - 1] + ans;
      i--;
      j--;
    }
  }
  cout << ans << "\n";
  return 0;
}

Fixed submission:
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <vector>
#define rep(i, n) for (int i = 0; i < n; ++i)
using namespace std;
int N;
string s, t;
int DP[3002][3002]; // sのi文字目までとtのj文字目まで探索し、その最大共通個数

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

  cin >> s >> t;
  rep(i, s.size()) {
    rep(j, t.size()) {
      if (s[i] == t[j]) {
        DP[i + 1][j + 1] = max(DP[i][j] + 1, DP[i + 1][j + 1]);
      }
      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]);
    }
  }

  string ans = "";
  int s_idx = (int)s.size();
  int t_idx = (int)t.size();
  while (s_idx > 0 && t_idx > 0) {
    if (DP[s_idx][t_idx] == DP[s_idx - 1][t_idx])
      s_idx--;
    else if (DP[s_idx][t_idx] == DP[s_idx][t_idx - 1])
      t_idx--;
    else {
      ans = s[s_idx - 1] + ans;
      s_idx--;
      t_idx--;
    }
  }
  cout << ans << "\n";
  return 0;
}
Problem p03165 (C++)

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

using namespace std;
string a, b;
int dp[1009][1009];

int solve(int idx1, int idx2) {
  if (idx1 >= a.length() || idx2 >= b.length())
    return 0;

  if (dp[idx1][idx2] != -1)
    return dp[idx1][idx2];

  if (a[idx1] == b[idx2]) {
    dp[idx1][idx2] = solve(idx1 + 1, idx2 + 1) + 1;
  } else {
    dp[idx1][idx2] = max(solve(idx1 + 1, idx2), solve(idx1, idx2 + 1));
  }
  return dp[idx1][idx2];
}
void build(int idx1, int idx2) {
  if (idx1 >= a.length() || idx2 >= b.length())
    return;
  if (a[idx1] == b[idx2]) {
    cout << a[idx1];
    idx1++;
    idx2++;
  } else if (dp[idx1][idx2] == solve(idx1 + 1, idx2)) {
    idx1++;
  } else if (dp[idx1][idx2] == solve(idx1, idx2 + 1)) {
    idx2++;
  }
  build(idx1, idx2);
  return;
}

int main() {

  memset(dp, -1, sizeof(dp));
  cin >> a >> b;
  solve(0, 0);
  build(0, 0);
}


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

using namespace std;
string a, b;
int dp[3009][3009];

int solve(int idx1, int idx2) {
  if (idx1 >= a.length() || idx2 >= b.length())
    return 0;

  if (dp[idx1][idx2] != -1)
    return dp[idx1][idx2];

  if (a[idx1] == b[idx2]) {
    dp[idx1][idx2] = solve(idx1 + 1, idx2 + 1) + 1;
  } else {
    dp[idx1][idx2] = max(solve(idx1 + 1, idx2), solve(idx1, idx2 + 1));
  }
  return dp[idx1][idx2];
}
void build(int idx1, int idx2) {
  if (idx1 >= a.length() || idx2 >= b.length())
    return;
  if (a[idx1] == b[idx2]) {
    cout << a[idx1];
    idx1++;
    idx2++;
  } else if (dp[idx1][idx2] == solve(idx1 + 1, idx2)) {
    idx1++;
  } else if (dp[idx1][idx2] == solve(idx1, idx2 + 1)) {
    idx2++;
  }
  build(idx1, idx2);
  return;
}

int main() {

  memset(dp, -1, sizeof(dp));
  cin >> a >> b;
  solve(0, 0);
  build(0, 0);
}