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

Buggy submission (status: Runtime Error):
// https://atcoder.jp/contests/dp/tasks/dp_f
// shauryakr
#include <bits/stdc++.h>
using namespace std;
string a, b;
int dp[3005][3005];
void solve() {
  for (int i = 1; i <= a.length(); i++) {
    for (int j = 1; j <= b.length(); j++) {
      if (a[i - 1] == b[j - 1]) {
        dp[i][j] = dp[i - 1][j - 1] + 1;
      } else {
        dp[i][j] = max(dp[i][j - 1], dp[i - 1][j]);
      }
    }
  }

  // for(int i=0; i<=b.length(); i++)
  // {
  //     for(int j=0; j<=a.length(); j++)
  //     {
  //         cout << dp[j][i] << " ";
  //     }
  //     cout << endl;
  // }

  int i = a.length(), j = b.length();
  string subseq = "";
  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 {
      subseq = subseq + a[i - 1];
      i--;
      j--;
    }
  }

  reverse(subseq.begin(), subseq.end());
  cout << subseq << endl;
}
int main() {
  cin >> a >> b;
  solve();
  return 0;
}

Fixed submission:
// https://atcoder.jp/contests/dp/tasks/dp_f
// shauryakr
#include <bits/stdc++.h>
using namespace std;
string a, b;
int dp[3005][3005];
void solve() {
  for (int i = 1; i <= a.length(); i++) {
    for (int j = 1; j <= b.length(); j++) {
      if (a[i - 1] == b[j - 1]) {
        dp[i][j] = dp[i - 1][j - 1] + 1;
      } else {
        dp[i][j] = max(dp[i][j - 1], dp[i - 1][j]);
      }
    }
  }

  // for(int i=0; i<=b.length(); i++)
  // {
  //     for(int j=0; j<=a.length(); j++)
  //     {
  //         cout << dp[j][i] << " ";
  //     }
  //     cout << endl;
  // }

  int i = a.length(), j = b.length();
  string subseq = "";
  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 {
      subseq = subseq + a[i - 1];
      i--;
      j--;
    }
  }

  reverse(subseq.begin(), subseq.end());
  cout << subseq << endl;
}
int main() {
  cin >> a >> b;
  solve();
  return 0;
}
Problem p03165 (C++)

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

    } else {
      if (dp[i][j - 1] > dp[i - 1][j])
        j--;
      else
        i--;
    }
  }
  n = str.size();
  for (i = n - 1; i >= 0; i--)
    cout << str[i];
  return 0;
}


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

    } else {
      if (dp[i][j - 1] > dp[i - 1][j])
        j--;
      else
        i--;
    }
  }
  n = str.size();
  for (i = n - 1; i >= 0; i--)
    cout << str[i];
  return 0;
}
Problem p03165 (C++)

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

int len(string &s, string &t, int i, int j, vector<vector<int>> &dp) {
  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 + len(s, t, i + 1, j + 1, dp);
  } else {
    int option1 = len(s, t, i + 1, j, dp);
    int option2 = len(s, t, i, j + 1, dp);
    return dp[i][j] = max(option1, option2);
  }
}

string get_lcs(string &s, string &t, int l, vector<vector<int>> &dp) {
  string str;
  str.reserve(l);
  int i = 0, j = 0;
  while (l > 0) {
    if (s[i] == t[j]) {
      str.push_back(s[i]);
      ++i;
      ++j;
      --l;
    } else {
      if (dp[i + 1][j] > dp[i][j + 1]) {
        ++i;
      } else {
        ++j;
      }
    }
  }
  return str;
}

int main() {
  string s, t;
  cin >> s >> t;
  int n = s.length();
  int m = t.length();
  vector<vector<int>> dp(n, vector<int>(m, -1));
  int l = len(s, t, 0, 0, dp);
  cout << get_lcs(s, t, l, dp);
  return 0;
}


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

int len(string &s, string &t, int i, int j, vector<vector<int>> &dp) {
  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 + len(s, t, i + 1, j + 1, dp);
  } else {
    int option1 = len(s, t, i + 1, j, dp);
    int option2 = len(s, t, i, j + 1, dp);
    return dp[i][j] = max(option1, option2);
  }
}

string get_lcs(string &s, string &t, int l, vector<vector<int>> &dp) {
  string str;
  str.reserve(l);
  int i = 0, j = 0;
  while (l > 0) {
    if (s[i] == t[j]) {
      str.push_back(s[i]);
      ++i;
      ++j;
      --l;
    } else {
      if (dp[i + 1][j] > dp[i][j + 1]) {
        ++i;
      } else {
        ++j;
      }
    }
  }
  return str;
}

int main() {
  string s, t;
  cin >> s >> t;
  int n = s.length();
  int m = t.length();
  vector<vector<int>> dp(n + 1, vector<int>(m + 1, -1));
  int l = len(s, t, 0, 0, dp);
  cout << get_lcs(s, t, l, dp);
  return 0;
}
Problem p03165 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define In_The_Name_Of_Allah_The_Merciful                                      \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);                                                               \
  cout.tie(NULL);
#define Alhamdulillah return 0;
#define all(v) v.begin(), v.end()
#define debug(n) cerr << "[" << #n << " = " << n << "]" << endl

const int N = 3000;
ll dp[100][N];
int n, m;
string s, w;

ll calc(int i, int j) {

  if (i == n || j == m)
    return 0;

  ll &ret = dp[i][j];
  if (~ret)
    return ret;

  if (s[i] == w[j])
    ret = calc(i + 1, j + 1) + 1;
  else
    ret = max(calc(i + 1, j), calc(i, j + 1));

  return ret;
}

void display(int i, int j) {

  if (i == n || j == m)
    return;

  if (s[i] == w[j]) {
    cout << s[i];
    display(i + 1, j + 1);
    return;
  }

  if (calc(i, j) == calc(i + 1, j))
    display(i + 1, j);
  else
    display(i, j + 1);
}

void Suhaib_Sawalha() {

  cin >> s >> w;
  n = s.size();
  m = w.size();

  memset(dp, -1, sizeof dp);
  calc(0, 0);
  display(0, 0);
}

int main() {
  In_The_Name_Of_Allah_The_Merciful /* بسم الله الرحمن الرحيم  */
#ifndef ONLINE_JUDGE
      freopen("SuhaibSawalha1.txt", "r", stdin);
#endif
  // int _;for(cin>>_;_;--_,cout<<'\n')
  Suhaib_Sawalha();
  Alhamdulillah /* الحمد لله */
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define In_The_Name_Of_Allah_The_Merciful                                      \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);                                                               \
  cout.tie(NULL);
#define Alhamdulillah return 0;
#define all(v) v.begin(), v.end()
#define debug(n) cerr << "[" << #n << " = " << n << "]" << endl

const int N = 3000;
ll dp[N][N];
int n, m;
string s, w;

ll calc(int i, int j) {

  if (i == n || j == m)
    return 0;

  ll &ret = dp[i][j];
  if (~ret)
    return ret;

  if (s[i] == w[j])
    ret = calc(i + 1, j + 1) + 1;
  else
    ret = max(calc(i + 1, j), calc(i, j + 1));

  return ret;
}

void display(int i, int j) {

  if (i == n || j == m)
    return;

  if (s[i] == w[j]) {
    cout << s[i];
    display(i + 1, j + 1);
    return;
  }

  if (calc(i, j) == calc(i + 1, j))
    display(i + 1, j);
  else
    display(i, j + 1);
}

void Suhaib_Sawalha() {

  cin >> s >> w;
  n = s.size();
  m = w.size();

  memset(dp, -1, sizeof dp);
  calc(0, 0);
  display(0, 0);
}

int main() {
  In_The_Name_Of_Allah_The_Merciful /* بسم الله الرحمن الرحيم  */
#ifndef ONLINE_JUDGE
      freopen("SuhaibSawalha1.txt", "r", stdin);
#endif
  // int _;for(cin>>_;_;--_,cout<<'\n')
  Suhaib_Sawalha();
  Alhamdulillah /* الحمد لله */
}
Problem p03165 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
#define in(v)                                                                  \
  v;                                                                           \
  cin >> v;
#define rep(i, n) for (int i = 0, _i = (n); i < _i; ++i)
#define rrep(i, n) for (long long i = (n); i >= 0; --i)
int main() {
  string in(s);
  string in(t);

  int N = s.size(), M = t.size();
  int dp[N + 1][M + 1];
  rep(i, N + 1) rep(j, M + 1) dp[i][j] = 0;

  rep(i, N) rep(j, M) {
    if (s[i] == t[j])
      dp[i + 1][j + 1] = dp[i][j] + 1;
    else
      dp[i + 1][j + 1] = max(dp[i][j], max(dp[i + 1][j], dp[i][j + 1]));
  }
  string ans;
  int m = M;
  int lcs = dp[N][M];
  rrep(i, N) {
    if (dp[i][m] != lcs) {
      ans += s[i];
      --lcs;
      while (dp[i][m - 1] == lcs)
        --m;
    }
  }
  reverse(ans.begin(), ans.end());
  cout << ans << endl;

  return 0;
}


Fixed submission:
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
#define in(v)                                                                  \
  v;                                                                           \
  cin >> v;
#define rep(i, n) for (int i = 0, _i = (n); i < _i; ++i)
#define rrep(i, n) for (long long i = (n); i >= 0; --i)
int main() {
  string in(s);
  string in(t);

  int N = s.size(), M = t.size();
  int dp[N + 1][M + 1];
  rep(i, N + 1) rep(j, M + 1) dp[i][j] = 0;

  rep(i, N) rep(j, M) {
    if (s[i] == t[j])
      dp[i + 1][j + 1] = dp[i][j] + 1;
    else
      dp[i + 1][j + 1] = max(dp[i][j], max(dp[i + 1][j], dp[i][j + 1]));
  }
  string ans;
  int m = M;
  int lcs = dp[N][M];
  rrep(i, N) {
    if (dp[i][m] != lcs) {
      ans += s[i];
      --lcs;
      while (m - 1 >= 0 && dp[i][m - 1] == lcs)
        --m;
    }
  }
  reverse(ans.begin(), ans.end());
  cout << ans << endl;

  return 0;
}
Problem p03165 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define ll long long int
string x;
int dp[3001][3001], ans = 0;

// vector<vector<string> >dp;
int fn(int i, int j, string s, string t) {
  if (i == 0 || j == 0)
    return 0;

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

    return dp[i][j];
  }
  if (s[i - 1] == t[j - 1]) {
    x += s[i - 1];
    ans = fn(i - 1, j - 1, s, t) + 1;
    dp[i][j] = ans;
    return ans;
  }

  ans = max(fn(i - 1, j, s, t), fn(i, j - 1, s, t));

  dp[i][j] = ans;
  return ans;
}
int main() {
  string s, t, a;
  cin >> s >> t;
  for (int i = 0; i <= s.size(); i++)
    for (int j = 0; j <= t.size(); j++)
      dp[i][j] = -1;

  int i = s.size(), j = t.size();
  int y = fn(i, j, s, t);
  // cout<<dp[i][j];
  while (i > 0 && j > 0) {
    if (s[i - 1] == t[j - 1]) {
      a += s[i - 1];
      i--;
      j--;
    } else if (dp[i - 1][j] > dp[i][j - 1]) {
      i--;
    } else
      j--;
  }
  reverse(a.begin(), a.end());
  cout << a;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define ll long long int
string x;
int dp[3001][3001], ans = 0;

// vector<vector<string> >dp;
int fn(int i, int j, string &s, string &t) {
  if (i == 0 || j == 0)
    return 0;

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

    return dp[i][j];
  }
  if (s[i - 1] == t[j - 1]) {
    x += s[i - 1];
    ans = fn(i - 1, j - 1, s, t) + 1;
    dp[i][j] = ans;
    return ans;
  }

  ans = max(fn(i - 1, j, s, t), fn(i, j - 1, s, t));

  dp[i][j] = ans;
  return ans;
}
int main() {
  string s, t, a;
  cin >> s >> t;
  for (int i = 0; i <= s.size(); i++)
    for (int j = 0; j <= t.size(); j++)
      dp[i][j] = -1;

  int i = s.size(), j = t.size();
  int y = fn(i, j, s, t);
  // cout<<dp[i][j];
  while (i > 0 && j > 0) {
    if (s[i - 1] == t[j - 1]) {
      a += s[i - 1];
      i--;
      j--;
    } else if (dp[i - 1][j] > dp[i][j - 1]) {
      i--;
    } else
      j--;
  }
  reverse(a.begin(), a.end());
  cout << a;
}
Problem p03165 (C++)

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

const int mxN = 1e3 + 2;
string s, t;
int n, m, dp[mxN][mxN];
pair<int, int> parent[mxN][mxN];

int calc(int i, int j) {
  if (i < 0 || j < 0)
    return 0;
  return dp[i][j];
}

int main() {
  cin >> s >> t;
  n = s.length();
  m = t.length();

  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      dp[i][j] = 0;
      if (calc(i - 1, j) >= dp[i][j]) {
        parent[i][j] = {i - 1, j};
        dp[i][j] = calc(i - 1, j);
      }
      if (calc(i, j - 1) >= dp[i][j]) {
        parent[i][j] = {i, j - 1};
        dp[i][j] = calc(i, j - 1);
      }
      if (s[i] == t[j] && calc(i - 1, j - 1) + 1 >= dp[i][j]) {
        parent[i][j] = {i - 1, j - 1};
        dp[i][j] = calc(i - 1, j - 1) + 1;
      }
    }
  }

  string ret = "";
  pair<int, int> cur = {n - 1, m - 1};
  while (cur.first >= 0 && cur.second >= 0) {
    if (s[cur.first] == t[cur.second])
      ret.push_back(s[cur.first]);
    cur = parent[cur.first][cur.second];
  }
  reverse(ret.begin(), ret.end());

  cout << ret << endl;
}

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

const int mxN = 3e3 + 2;
string s, t;
int n, m, dp[mxN][mxN];
pair<int, int> parent[mxN][mxN];

int calc(int i, int j) {
  if (i < 0 || j < 0)
    return 0;
  return dp[i][j];
}

int main() {
  cin >> s >> t;
  n = s.length();
  m = t.length();

  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      dp[i][j] = 0;
      if (calc(i - 1, j) >= dp[i][j]) {
        parent[i][j] = {i - 1, j};
        dp[i][j] = calc(i - 1, j);
      }
      if (calc(i, j - 1) >= dp[i][j]) {
        parent[i][j] = {i, j - 1};
        dp[i][j] = calc(i, j - 1);
      }
      if (s[i] == t[j] && calc(i - 1, j - 1) + 1 >= dp[i][j]) {
        parent[i][j] = {i - 1, j - 1};
        dp[i][j] = calc(i - 1, j - 1) + 1;
      }
    }
  }

  string ret = "";
  pair<int, int> cur = {n - 1, m - 1};
  while (cur.first >= 0 && cur.second >= 0) {
    if (s[cur.first] == t[cur.second])
      ret.push_back(s[cur.first]);
    cur = parent[cur.first][cur.second];
  }
  reverse(ret.begin(), ret.end());

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

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define ll long long
#define pii pair<int, int>
#define f first
#define s second
#define read freopen("input.txt", "r", stdin)
#define write freopen("output.txt", "w", stdout)
#define mx 5e12

using namespace std;
ll dp[3010][3010];
int main() {
  string s, t, ans;
  cin >> s;
  cin >> t;

  int 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;
      } else {
        dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]);
      }
    }
  }

  //    cout<<dp[n][m];

  for (int i = n, j = m; i > 0, j > 0;) {
    if (dp[i][j] != dp[i - 1][j] && dp[i][j] != dp[i][j - 1]) {
      ans = s[i - 1] + ans;
      i--;
      j--;
    } else if (dp[i][j] == dp[i][j - 1]) {
      j--;
    } else
      i--;
  }

  cout << ans;
}


Fixed submission:
#include <bits/stdc++.h>
#define ll long long
#define pii pair<int, int>
#define f first
#define s second
#define read freopen("input.txt", "r", stdin)
#define write freopen("output.txt", "w", stdout)
#define mx 5e12

using namespace std;
ll dp[3010][3010];
int main() {
  string s, t, ans;
  cin >> s;
  cin >> t;

  int 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;
      } else {
        dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]);
      }
    }
  }

  //    cout<<dp[n][m];

  for (int i = n, j = m; i > 0 & j > 0;) {
    if (dp[i][j] != dp[i - 1][j] && dp[i][j] != dp[i][j - 1]) {
      ans = s[i - 1] + ans;
      i--;
      j--;
    } else if (dp[i][j] == dp[i][j - 1]) {
      j--;
    } else
      i--;
  }

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

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define IOS                                                                    \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);
#define ll long long

int dp[3001][3001];

int main() {
  IOS;
  memset(dp, 0, sizeof(dp));
  string s, t;
  cin >> s >> t;
  s = "$" + s;
  t = "#" + t;
  int n = s.size();
  int m = t.size();

  for (int i = 1; i <= n; ++i) {
    for (int j = 1; j <= m; ++j) {
      if (s[i] == t[j]) {
        dp[i][j] = 1 + dp[i - 1][j - 1];
      } else
        dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]);
    }
  }
  string ans = "";
  int i = n;
  int j = m;
  while (i > 0 && j > 0) {
    if (s[i] == t[j]) {
      // cout << s[i] << endl;
      ans += s[i];
      i--;
      j--;
    } else {
      if (dp[i - 1][j] >= dp[i][j - 1]) {
        i--;
      } else
        j--;
    }
  }
  reverse(ans.begin(), ans.end());
  cout << ans.substr(0, ans.size() - 1) << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define IOS                                                                    \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);
#define ll long long

int dp[3005][3005];

int main() {
  IOS;
  memset(dp, 0, sizeof(dp));
  string s, t;
  cin >> s >> t;
  s = "$" + s;
  t = "#" + t;
  int n = s.size();
  int m = t.size();

  for (int i = 1; i <= n; ++i) {
    for (int j = 1; j <= m; ++j) {
      if (s[i] == t[j]) {
        dp[i][j] = 1 + dp[i - 1][j - 1];
      } else
        dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]);
    }
  }
  string ans = "";
  int i = n;
  int j = m;
  while (i > 0 && j > 0) {
    if (s[i] == t[j]) {
      // cout << s[i] << endl;
      ans += s[i];
      i--;
      j--;
    } else {
      if (dp[i - 1][j] >= dp[i][j - 1]) {
        i--;
      } else
        j--;
    }
  }
  reverse(ans.begin(), ans.end());
  cout << ans.substr(0, ans.size() - 1) << endl;
}
Problem p03165 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
ll mod = 1e9 + 7;
ll const maxn = 1e3 + 5;
ll const inf = 1e18;
ll add(ll a, ll b) { return ((a % mod) + (b % mod)) % mod; }
ll mul(ll a, ll b) { return ((a % mod) * (b % mod)) % mod; }
ll powm(ll x, ll n, ll M) {
  ll result = 1;
  while (n > 0) {
    if (n % 2 == 1)
      result = (result * x) % M;
    x = (x * x) % M;
    n = n / 2;
  }
  return result;
}
bool prime(ll x) {
  if (x < 2)
    return false;
  for (int i = 2; i <= sqrt(x); i++) {
    if (x % i == 0)
      return false;
  }
  return true;
}
int main() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  string s, t;
  cin >> s >> t;
  ll n = s.size();
  ll m = t.size();
  ll dp[n + 1][m + 1];
  memset(dp, 0, sizeof(dp));
  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 x = "";
  ll a = n, b = m;
  while (a > 0 and b > 0) {
    if (s[a - 1] == t[b - 1]) {
      x += s[a - 1];
      a--;
      b--;
    } else if (dp[a - 1][b] > dp[a][b - 1])
      a--;
    else
      b--;
  }
  reverse(x.begin(), x.end());
  cout << x;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
ll mod = 1e9 + 7;
ll const maxn = 1e3 + 5;
ll const inf = 1e18;
ll add(ll a, ll b) { return ((a % mod) + (b % mod)) % mod; }
ll mul(ll a, ll b) { return ((a % mod) * (b % mod)) % mod; }
ll powm(ll x, ll n, ll M) {
  ll result = 1;
  while (n > 0) {
    if (n % 2 == 1)
      result = (result * x) % M;
    x = (x * x) % M;
    n = n / 2;
  }
  return result;
}
bool prime(ll x) {
  if (x < 2)
    return false;
  for (int i = 2; i <= sqrt(x); i++) {
    if (x % i == 0)
      return false;
  }
  return true;
}
int main() {
  // #ifndef ONLINE_JUDGE
  // freopen("input.txt","r",stdin);
  // freopen("output.txt","w",stdout);
  // #endif
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  string s, t;
  cin >> s >> t;
  ll n = s.size();
  ll m = t.size();
  ll dp[n + 1][m + 1];
  memset(dp, 0, sizeof(dp));
  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 x = "";
  ll a = n, b = m;
  while (a > 0 and b > 0) {
    if (s[a - 1] == t[b - 1]) {
      x += s[a - 1];
      a--;
      b--;
    } else if (dp[a - 1][b] > dp[a][b - 1])
      a--;
    else
      b--;
  }
  reverse(x.begin(), x.end());
  cout << x;
  return 0;
}
Problem p03165 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define ll long long
int const N = 1e3 + 5;
string s, t;
int n, m;
int mem[N][N];
int oo = 0x3f3f3f3f;
int DP(int i = 0, int j = 0) {
  if (i == n || j == m)
    return 0;
  int &ret = mem[i][j];
  if (~ret)
    return ret;
  return ret =
             max(DP(i + 1, j),
                 max(DP(i, j + 1), (s[i] == t[j] ? DP(i + 1, j + 1) + 1 : 0)));
}
string res;
void print(int i = 0, int j = 0) {
  if (i == n || j == m)
    return;
  if (DP(i, j) == DP(i + 1, j))
    return print(i + 1, j);
  else if (DP(i, j) == DP(i, j + 1))
    return print(i, j + 1);
  else {
    res.push_back(s[i]);
    print(i + 1, j + 1);
  }
}

int main() {
#ifdef ONLINE_JUDGE
  ios::sync_with_stdio(false), cin.tie(0);
#endif
#ifndef ONLINE_JUDGE
  freopen("in.txt", "r", stdin);
  freopen("out.txt", "w", stdout);
#endif
  memset(mem, -1, sizeof(mem));
  cin >> s >> t;
  n = s.size(), m = t.size();
  DP(0, 0);
  print();
  cout << res << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define ll long long
int const N = 3e3 + 5;
string s, t;
int n, m;
int mem[N][N];
int oo = 0x3f3f3f3f;
int DP(int i = 0, int j = 0) {
  if (i == n || j == m)
    return 0;
  int &ret = mem[i][j];
  if (~ret)
    return ret;
  return ret =
             max(DP(i + 1, j),
                 max(DP(i, j + 1), (s[i] == t[j] ? DP(i + 1, j + 1) + 1 : 0)));
}
string res;
void print(int i = 0, int j = 0) {
  if (i == n || j == m)
    return;
  if (DP(i, j) == DP(i + 1, j))
    return print(i + 1, j);
  else if (DP(i, j) == DP(i, j + 1))
    return print(i, j + 1);
  else {
    res.push_back(s[i]);
    print(i + 1, j + 1);
  }
}

int main() {
#ifdef ONLINE_JUDGE
  ios::sync_with_stdio(false), cin.tie(0);
#endif
#ifndef ONLINE_JUDGE
  freopen("in.txt", "r", stdin);
  freopen("out.txt", "w", stdout);
#endif
  memset(mem, -1, sizeof(mem));
  cin >> s >> t;
  n = s.size(), m = t.size();
  DP(0, 0);
  print();
  cout << res << endl;
}
Problem p03165 (C++)

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

using namespace std;

const int N = 1007;

long long dp[N][N];

string s, t;

int abs(int val) { return max(val, -val); }

int main() {
  int n, tw, val, cst;
  cin >> s >> t;
  for (int i = 1; i <= s.size(); ++i)
    for (int j = 1; j <= t.size(); ++j)
      dp[i][j] = max(max(dp[i - 1][j], dp[i][j - 1]),
                     dp[i - 1][j - 1] + (s[i - 1] == t[j - 1]));
  int ci(s.size()), cj(t.size());
  string ans;
  while (ci && cj) {
    if (dp[ci][cj] == dp[ci - 1][cj]) {
      --ci;
    } else if (dp[ci][cj] == dp[ci][cj - 1]) {
      --cj;
    } else {
      --ci;
      --cj;
      ans = ans + s[ci];
    }
  }
  reverse(ans.begin(), ans.end());
  cout << ans;
  return 0;
}


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

using namespace std;

const int N = 3007;

long long dp[N][N];

string s, t;

int abs(int val) { return max(val, -val); }

int main() {
  int n, tw, val, cst;
  cin >> s >> t;
  for (int i = 1; i <= s.size(); ++i)
    for (int j = 1; j <= t.size(); ++j)
      dp[i][j] = max(max(dp[i - 1][j], dp[i][j - 1]),
                     dp[i - 1][j - 1] + (s[i - 1] == t[j - 1]));
  int ci(s.size()), cj(t.size());
  string ans;
  while (ci && cj) {
    if (dp[ci][cj] == dp[ci - 1][cj]) {
      --ci;
    } else if (dp[ci][cj] == dp[ci][cj - 1]) {
      --cj;
    } else {
      --ci;
      --cj;
      ans = ans + s[ci];
    }
  }
  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 lcsMemo(int i, int j, string &s, string &t, vector<vector<int>> &dp) {
  if (i >= (int)s.size() || j >= (int)t.size() || i < 0 || j < 0)
    return 0;
  if (dp[i][j] != -1) {
    return dp[i][j];
  } else if (s[i] == t[j]) {
    dp[i][j] = 1 + lcsMemo(i + 1, j + 1, s, t, dp);
    return dp[i][j];
  } else {
    int firstRemoval = lcsMemo(i + 1, j, s, t, dp);
    int secondRemoval = lcsMemo(i, j + 1, s, t, dp);
    dp[i][j] = max(firstRemoval, secondRemoval);
    return dp[i][j];
  }
}
string lcsWord(vector<vector<int>> &dp, string s, string t) {
  int i = 0, j = 0;
  string res = "";
  while (i < dp.size() && j < dp[0].size()) {
    // cout<<i<<" "<<j<<" ";
    if (s[i] == t[j]) {
      // cout<<"1"<<endl;
      res += s[i];
      i++;
      j++;
    } else if (dp[i + 1][j] > dp[i][j + 1]) {
      // cout<<2<<endl;
      i++;
    } else {
      // cout<<3<<endl;
      j++;
    }
  }
  // reverse(res.begin(), res.end());
  return res;
}
int main() {
  string s, t;
  cin >> s >> t;
  vector<vector<int>> dp((int)s.size(), vector<int>((int)t.size(), -1));
  int lcsLength = lcsMemo(0, 0, s, t, dp);
  // for(int i=0; i< (int)s.size(); i++){
  //     for(int j=0; j< (int)t.size(); j++){
  //         cout<<dp[i][j]<<" ";
  //     }
  //     cout<<endl;
  // }
  string answer = lcsWord(dp, s, t);
  cout << answer << endl;
  return 0;
}

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

*/
int lcsMemo(int i, int j, string &s, string &t, vector<vector<int>> &dp) {
  if (i >= (int)s.size() || j >= (int)t.size() || i < 0 || j < 0)
    return 0;
  if (dp[i][j] != -1) {
    return dp[i][j];
  } else if (s[i] == t[j]) {
    dp[i][j] = 1 + lcsMemo(i + 1, j + 1, s, t, dp);
    return dp[i][j];
  } else {
    int firstRemoval = lcsMemo(i + 1, j, s, t, dp);
    int secondRemoval = lcsMemo(i, j + 1, s, t, dp);
    dp[i][j] = max(firstRemoval, secondRemoval);
    return dp[i][j];
  }
}
string lcsWord(vector<vector<int>> &dp, string s, string t) {
  int i = 0, j = 0;
  string res = "";
  while (i < dp.size() && j < dp[0].size()) {
    // cout<<i<<" "<<j<<" ";
    if (s[i] == t[j]) {
      // cout<<"1"<<endl;
      res += s[i];
      i++;
      j++;
    } else if (i + 1 == dp.size()) {
      j++;
    } else if (j + 1 == dp[0].size()) {
      i++;
    } else if (dp[i + 1][j] > dp[i][j + 1]) {
      // cout<<2<<endl;
      i++;
    } else {
      // cout<<3<<endl;
      j++;
    }
  }
  // reverse(res.begin(), res.end());
  return res;
}
int main() {
  string s, t;
  cin >> s >> t;
  vector<vector<int>> dp((int)s.size(), vector<int>((int)t.size(), -1));
  int lcsLength = lcsMemo(0, 0, s, t, dp);
  // for(int i=0; i< (int)s.size(); i++){
  //     for(int j=0; j< (int)t.size(); j++){
  //         cout<<dp[i][j]<<" ";
  //     }
  //     cout<<endl;
  // }
  string answer = lcsWord(dp, s, t);
  cout << answer << endl;
  return 0;
}
Problem p03165 (C++)

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

using namespace std;

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

  string a = "", b = "";
  cin >> a >> b;
  int i, j;
  int al = a.length();
  int bl = b.length();
  int dp[al + 1][bl + 1];
  for (i = 0; i <= al; i++) {
    dp[0][i] = 0;
  }
  for (i = 1; i <= bl; i++) {
    dp[i][0] = 0;
    for (j = 1; j <= al; j++) {
      if (b[i - 1] == a[j - 1])
        dp[i][j] = dp[i - 1][j - 1] + 1;
      else
        dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]);
    }
  }
  /* for(i=0;i<=bl;i++){
       for(j=0;j<=al;j++){
           cout << dp[i][j] << " ";
       }
       cout << "\n";
   }*/
  string s = "";
  i = bl;
  j = al;
  while (dp[i][j] > 0) {

    if (a[j - 1] == b[i - 1]) {
      s = s + b[i - 1];
      i--;
      j--;
    } else if (dp[i - 1][j] > dp[i][j - 1]) {
      i--;
    } else
      j--;
  }
  reverse(s.begin(), s.end());
  cout << s;
}

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

using namespace std;

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

  string a = "", b = "";
  cin >> a >> b;
  int i, j;
  int al = a.length();
  int bl = b.length();
  int dp[bl + 1][al + 1];
  for (i = 0; i <= al; i++) {
    dp[0][i] = 0;
  }
  for (i = 1; i <= bl; i++) {
    dp[i][0] = 0;
    for (j = 1; j <= al; j++) {
      if (b[i - 1] == a[j - 1])
        dp[i][j] = dp[i - 1][j - 1] + 1;
      else
        dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]);
    }
  }
  /* for(i=0;i<=bl;i++){
       for(j=0;j<=al;j++){
           cout << dp[i][j] << " ";
       }
       cout << "\n";
   }*/
  string s = "";
  i = bl;
  j = al;
  while (dp[i][j] > 0) {

    if (a[j - 1] == b[i - 1]) {
      s = s + b[i - 1];
      i--;
      j--;
    } else if (dp[i - 1][j] > dp[i][j - 1]) {
      i--;
    } else
      j--;
  }
  reverse(s.begin(), s.end());
  cout << s;
}
Problem p03165 (C++)

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

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  string s, t;
  cin >> s >> t;
  int dp[s.size() + 1][t.size() + 1];
  dp[0][0] = 1;
  for (int i1 = 1; i1 <= t.size(); i1++)
    dp[0][i1] = 0;
  for (int i2 = 1; i2 <= s.size(); i2++)
    dp[i2][0] = 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]; // popoff from both
      else
        dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]); // try both, choose max
    }
  }
  stack<char> lcs;
  int i, j;
  i = s.size();
  j = t.size();
  while (i != 0 || j != 0) {
    if (dp[i][j] != max(dp[i - 1][j], dp[i][j - 1])) {
      lcs.push(s[i - 1]);
      i--;
      j--;
    } else {
      if (dp[i][j - 1] <= dp[i - 1][j])
        i--; // dont push, just reach there and in next iteration it will be
             // pushed
      else
        j--;
    }
  }
  while (!lcs.empty())
    cout << lcs.top(), lcs.pop();
  return 0;
}

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

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  string s, t;
  cin >> s >> t;
  int dp[s.size() + 1][t.size() + 1];
  dp[0][0] = 1;
  for (int i1 = 1; i1 <= t.size(); i1++)
    dp[0][i1] = 0;
  for (int i2 = 1; i2 <= s.size(); i2++)
    dp[i2][0] = 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]; // popoff from both
      else
        dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]); // try both, choose max
    }
  }
  stack<char> lcs;
  int i, j;
  i = s.size();
  j = t.size();
  while (i != 0 && j != 0) {
    if (dp[i][j] > max(dp[i - 1][j], dp[i][j - 1])) {
      lcs.push(s[i - 1]);
      i--;
      j--;
    } else {
      if (dp[i][j - 1] <= dp[i - 1][j])
        i--; // dont push, just reach there and in next iteration it will be
             // pushed
      else
        j--;
    }
  }
  while (!lcs.empty())
    cout << lcs.top(), lcs.pop();
  return 0;
}
Problem p03165 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define flb(i, a, b) for (int i = a; i < b; i++)
#define ll long long int
#define mp make_pair
#define pb push_back
#define inf (long long)(1e18 + 1e15)
typedef pair<int, int> ii;
typedef vector<ll> vi;
typedef vector<pair<int, int>> vii;
typedef vector<vector<int>> vvi;
int calculated[100][100];
string s, t;
int n, m;
int solve(int i, int j) {
  if (i == n || j == m)
    return 0;
  if (calculated[i][j] != -1)
    return calculated[i][j];
  if (s[i] == t[j]) {
    calculated[i][j] = 1 + solve(i + 1, j + 1);
  } else {
    calculated[i][j] = max(solve(i + 1, j), solve(i, j + 1));
  }
  return calculated[i][j];
}
int main() {
  cin >> s >> t;
  n = s.size();
  m = t.size();
  int ans = 0;
  memset(calculated, -1, sizeof(calculated));
  solve(0, 0);
  string temp;
  int i = 0, j = 0;
  while (i < n && j < m) {
    if (s[i] == t[j]) {
      temp.push_back(s[i]);
      i++;
      j++;
    } else if (calculated[i + 1][j] > calculated[i][j + 1]) {
      i++;
    } else
      j++;
  }
  cout << temp;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define flb(i, a, b) for (int i = a; i < b; i++)
#define ll long long int
#define mp make_pair
#define pb push_back
#define inf (long long)(1e18 + 1e15)
typedef pair<int, int> ii;
typedef vector<ll> vi;
typedef vector<pair<int, int>> vii;
typedef vector<vector<int>> vvi;
int calculated[3001][3001];
string s, t;
int n, m;
int solve(int i, int j) {
  if (i == n || j == m)
    return 0;
  if (calculated[i][j] != -1)
    return calculated[i][j];
  if (s[i] == t[j]) {
    calculated[i][j] = 1 + solve(i + 1, j + 1);
  } else {
    calculated[i][j] = max(solve(i + 1, j), solve(i, j + 1));
  }
  return calculated[i][j];
}
int main() {
  cin >> s >> t;
  n = s.size();
  m = t.size();
  int ans = 0;
  memset(calculated, -1, sizeof(calculated));
  solve(0, 0);
  string temp;
  int i = 0, j = 0;
  while (i < n && j < m) {
    if (s[i] == t[j]) {
      temp.push_back(s[i]);
      i++;
      j++;
    } else if (calculated[i + 1][j] > calculated[i][j + 1]) {
      i++;
    } else
      j++;
  }
  cout << temp;
}
Problem p03165 (C++)

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

#define ll long long int

int main() {
  string s1, s2;
  cin >> s1 >> s2;
  int n = s1.size();
  int m = s2.size();

  vector<vector<int>> dp(n + 1, vector<int>(m + 1, 0));
  string ans = "";
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      if (s1[i - 1] == s2[j - 1])
        dp[i][j] = 1 + dp[i - 1][j - 1];
      else
        dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]);
    }
  }
  int i = n, j = m;
  if (dp[n][m] == 0) {
    cout << ans << endl;
    return 0;
  }
  while (i != 0 || j != 0) {
    if (s1[i - 1] == s2[j - 1]) {
      ans = s1[i - 1] + ans;
      i--;
      j--;
    } else {
      if (dp[i][j - 1] >= dp[i - 1][j])
        j--;
      else
        i--;
    }
  }
  cout << ans << endl;
}

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

#define ll long long int

int main() {
  string s1, s2;
  cin >> s1 >> s2;
  int n = s1.size();
  int m = s2.size();

  vector<vector<int>> dp(n + 1, vector<int>(m + 1, 0));
  string ans = "";
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      if (s1[i - 1] == s2[j - 1])
        dp[i][j] = 1 + dp[i - 1][j - 1];
      else
        dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]);
    }
  }
  int i = n, j = m;
  if (dp[n][m] == 0) {
    cout << ans << endl;
    return 0;
  }
  while (i > 0 && j > 0) {
    if (s1[i - 1] == s2[j - 1]) {
      ans = s1[i - 1] + ans;
      i--;
      j--;
    } else {
      if (dp[i][j - 1] >= dp[i - 1][j])
        j--;
      else
        i--;
    }
  }
  cout << ans << endl;
}
Problem p03165 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <stack>
#include <string>
using namespace std;

int main() {
  string a, b;
  cin >> a;
  cin >> b;
  int dp[1001][1001];
  for (int i = 0; i <= (int)a.length(); i++)
    dp[i][0] = 0;
  for (int i = 0; i <= (int)b.length(); i++)
    dp[0][i] = 0;
  for (int i = 1; i <= (int)a.length(); i++) {
    for (int j = 1; j <= (int)b.length(); 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]);
      }
    }
  }
  // cout << dp[(int)a.length()][(int)b.length()];
  int i = (int)a.length(), j = (int)b.length();
  stack<char> st;
  while (dp[i][j] != 0) {
    if (dp[i][j] > max(dp[i - 1][j], dp[i][j - 1])) {
      st.push(a[i - 1]);
      i--;
      j--;
    } else {
      if (dp[i - 1][j] > dp[i][j - 1]) {
        i--;
      } else
        j--;
    }
  }
  while (!st.empty()) {
    cout << st.top();
    st.pop();
  }
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <stack>
#include <string>
using namespace std;

int main() {
  string a, b;
  cin >> a;
  cin >> b;
  int dp[3001][3001];
  for (int i = 0; i <= (int)a.length(); i++)
    dp[i][0] = 0;
  for (int i = 0; i <= (int)b.length(); i++)
    dp[0][i] = 0;
  for (int i = 1; i <= (int)a.length(); i++) {
    for (int j = 1; j <= (int)b.length(); 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]);
      }
    }
  }
  // cout << dp[(int)a.length()][(int)b.length()];
  int i = (int)a.length(), j = (int)b.length();
  stack<char> st;
  while (dp[i][j] != 0) {
    if (dp[i][j] > max(dp[i - 1][j], dp[i][j - 1])) {
      st.push(a[i - 1]);
      i--;
      j--;
    } else {
      if (dp[i - 1][j] > dp[i][j - 1]) {
        i--;
      } else
        j--;
    }
  }
  while (!st.empty()) {
    cout << st.top();
    st.pop();
  }
}
Problem p03165 (C++)

Buggy submission (status: Runtime Error):
// define DEBUG for debug
#define DEBUG

#include <bits/stdc++.h>
using namespace std;
#define F first
#define S second
#define mp make_pair
#define pb push_back
#define MAX 3000
#define MOD 1000000007
typedef long long int ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
int dp[MAX + 10][MAX + 10];
string cevap;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
#ifdef DEBUG
  freopen("i.txt", "r", stdin);
  freopen("o.txt", "w", stdout);
#endif
  string a, b;
  cin >> a >> b;
  int x = a.size(), y = b.size();
  for (int i = 1; i <= x; i++)
    for (int j = 1; j <= y; 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 index = dp[x][y];
  while (x > 0 || y > 0) {
    if (x > 0 && dp[x - 1][y] == dp[x][y]) {
      x--;
    } else if (y > 0 && dp[x][y - 1] == dp[x][y]) {
      y--;
    } else {
      cevap += a[x - 1];
      x--;
      y--;
    }
  }
  for (int i = index - 1; i >= 0; i--)
    cout << cevap[i];
  return 0;
}

Fixed submission:
// define DEBUG for debug
// #define DEBUG

#include <bits/stdc++.h>
using namespace std;
#define F first
#define S second
#define mp make_pair
#define pb push_back
#define MAX 3000
#define MOD 1000000007
typedef long long int ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
int dp[MAX + 10][MAX + 10];
string cevap;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
#ifdef DEBUG
  freopen("i.txt", "r", stdin);
  freopen("o.txt", "w", stdout);
#endif
  string a, b;
  cin >> a >> b;
  int x = a.size(), y = b.size();
  for (int i = 1; i <= x; i++)
    for (int j = 1; j <= y; 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 index = dp[x][y];
  while (x > 0 || y > 0) {
    if (x > 0 && dp[x - 1][y] == dp[x][y]) {
      x--;
    } else if (y > 0 && dp[x][y - 1] == dp[x][y]) {
      y--;
    } else {
      cevap += a[x - 1];
      x--;
      y--;
    }
  }
  for (int i = index - 1; i >= 0; i--)
    cout << cevap[i];
  return 0;
}
Problem p03165 (C++)

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

using namespace std;
typedef long long ll;

int T, n;
const int maxl = 3000 + 5;
int dp[maxl][maxl];
int path[maxl][maxl];

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  string s, t;
  cin >> s >> t;

  dp[0][0] = 0;
  dp[1][0] = 0;
  dp[0][1] = 0;
  path[0][0] = path[0][1] = path[1][0] = -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, path[i][j] = 0;
      else if (dp[i - 1][j] > dp[i][j - 1])
        dp[i][j] = dp[i - 1][j], path[i][j] = 1;
      else
        dp[i][j] = dp[i][j - 1], path[i][j] = 2;
    }
  // cout << dp[s.size()][t.size()];
  char ans[maxl];
  int x = s.size(), y = t.size();
  int cur = dp[x][y];
  ans[cur--] = '\0';
  while (path[x][y] != -1) {
    if (path[x][y] == 0)
      x--, y--, ans[cur--] = s[x];
    else if (path[x][y] == 1)
      x--;
    else
      y--;
  }
  cout << ans;

  return 0;
}


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

using namespace std;
typedef long long ll;

int T, n;
const int maxl = 3000 + 5;
int dp[maxl][maxl];
int path[maxl][maxl];

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  string s, t;
  cin >> s >> t;

  for (int i = 0; i <= max(s.size(), t.size()); i++)
    dp[0][i] = dp[i][0] = 0, path[0][i] = path[i][0] = -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, path[i][j] = 0;
      else if (dp[i - 1][j] > dp[i][j - 1])
        dp[i][j] = dp[i - 1][j], path[i][j] = 1;
      else
        dp[i][j] = dp[i][j - 1], path[i][j] = 2;
    }
  // cout << dp[s.size()][t.size()];
  char ans[maxl];
  int x = s.size(), y = t.size();
  int cur = dp[x][y];
  ans[cur--] = '\0';
  while (path[x][y] != -1) {
    if (path[x][y] == 0)
      x--, y--, ans[cur--] = s[x];
    else if (path[x][y] == 1)
      x--;
    else
      y--;
  }
  cout << ans;

  return 0;
}
Problem p03165 (C++)

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

const int INF = 0x3f3f3f3f;
const int N = 1e3 + 5;

int dp[N][N];
string a, b, ans;

int dpf(int i, int j) {
  if (dp[i][j] != -INF)
    return dp[i][j];
  if (!i or !j)
    return dp[i][j] = 0;
  if (a[i - 1] == b[j - 1])
    return dp[i][j] = 1 + dpf(i - 1, j - 1);
  return dp[i][j] = max(dpf(i - 1, j), dpf(i, j - 1));
}

void rec(int i, int j) {
  if (!i or !j)
    return;
  if (a[i - 1] == b[j - 1])
    ans += a[i - 1], rec(i - 1, j - 1);
  else if (dp[i][j] == dp[i - 1][j])
    rec(i - 1, j);
  else
    rec(i, j - 1);
}

int main() {
  ios_base::sync_with_stdio(0), cin.tie(0);
  cin >> a >> b;
  int n = a.size(), m = b.size();
  for (int i = 0; i < N; i++)
    for (int j = 0; j < N; j++)
      dp[i][j] = -INF;
  dpf(n, m);
  rec(n, m);
  reverse(ans.begin(), ans.end());
  cout << ans << "\n";
}


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

const int INF = 0x3f3f3f3f;
const int N = 3e3 + 5;

int dp[N][N];
string a, b, ans;

int dpf(int i, int j) {
  if (dp[i][j] != -INF)
    return dp[i][j];
  if (!i or !j)
    return dp[i][j] = 0;
  if (a[i - 1] == b[j - 1])
    return dp[i][j] = 1 + dpf(i - 1, j - 1);
  return dp[i][j] = max(dpf(i - 1, j), dpf(i, j - 1));
}

void rec(int i, int j) {
  if (!i or !j)
    return;
  if (a[i - 1] == b[j - 1])
    ans += a[i - 1], rec(i - 1, j - 1);
  else if (dp[i][j] == dp[i - 1][j])
    rec(i - 1, j);
  else
    rec(i, j - 1);
}

int main() {
  ios_base::sync_with_stdio(0), cin.tie(0);
  cin >> a >> b;
  int n = a.size(), m = b.size();
  for (int i = 0; i < N; i++)
    for (int j = 0; j < N; j++)
      dp[i][j] = -INF;
  dpf(n, m);
  rec(n, m);
  reverse(ans.begin(), ans.end());
  cout << ans << "\n";
}
Problem p03165 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define mod 1000000007
#define fast                                                                   \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(0);                                                                  \
  cout.tie(0);

ll w[100], v[100], ar[100][100000], sum;

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

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define mod 1000000007
#define fast                                                                   \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(0);                                                                  \
  cout.tie(0);

ll w[100], v[100], ar[100][100000], sum;

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

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#include <ctype.h>
#include <iostream>
#include <stdio.h>
#define lldi unsigned long long int
using namespace std;

string cadena;
int dp[400][400] = {0};
int lcs(string s1, string s2) {

  int tam1 = s1.length();
  int tam2 = s2.length();

  for (int i = 1; i <= tam1; i++) {
    for (int j = 1; j <= tam2; 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]);
    }
  }

  int i = tam1;
  int j = tam2;
  int band = 0;
  while (i > 0 && j > 0) {

    if (s1[i - 1] == s2[j - 1]) {
      cadena.push_back(s1[i - 1]);
      i--;
      j--;
    }

    else if (dp[i][j - 1] > dp[i - 1][j])
      j--;
    else
      i--;
  }
  if (cadena.size() == 0)
    printf("\n");
  else {
    for (int i = cadena.size() - 1; i >= 0; i--)
      cout << cadena[i];
    cout << endl;
  }
}

int main() {
  string s1, s2;
  cin >> s1;
  cin >> s2;
  lcs(s1, s2);

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#include <ctype.h>
#include <iostream>
#include <stdio.h>
#define lldi unsigned long long int
using namespace std;

string cadena;
int dp[4000][4000] = {0};
int lcs(string s1, string s2) {

  int tam1 = s1.length();
  int tam2 = s2.length();

  for (int i = 1; i <= tam1; i++) {
    for (int j = 1; j <= tam2; 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]);
    }
  }

  int i = tam1;
  int j = tam2;
  int band = 0;
  while (i > 0 && j > 0) {

    if (s1[i - 1] == s2[j - 1]) {
      cadena.push_back(s1[i - 1]);
      i--;
      j--;
    }

    else if (dp[i][j - 1] > dp[i - 1][j])
      j--;
    else
      i--;
  }
  if (cadena.size() == 0)
    printf("\n");
  else {
    for (int i = cadena.size() - 1; i >= 0; i--)
      cout << cadena[i];
    cout << endl;
  }
}

int main() {
  string s1, s2;
  cin >> s1;
  cin >> s2;
  lcs(s1, s2);

  return 0;
}