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

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define ms(x, t) memset(x, t, sizeof(x))
typedef long long int ll;
typedef unsigned long long int ull;
const int limit = 3e3 + 1;
const int inf = 1e7;

int main() {
  string s, t;
  cin >> s >> t;
  int sl = s.length();
  int tl = t.length();
  int m[sl + 1][tl + 1];
  stack<char> st;
  ms(m, 0);
  for (int i = 1; i <= sl; i++) {
    for (int j = 1; j <= tl; j++) {
      if (s[i - 1] == t[j - 1]) {
        m[i][j] = 1 + m[i - 1][j - 1];
      } else {
        m[i][j] = max(m[i][j - 1], m[i - 1][j]);
      }
    }
  }
  int i = sl;
  int j = tl;
  for (; i != 0 || j != 0;) {
    if (m[i][j] == m[i - 1][j - 1] + 1) {
      st.push(s[i - 1]);
      i--;
      j--;
    } else if (m[i][j] == m[i - 1][j]) {
      i--;
    } else {
      j--;
    }
  }
  while (!st.empty()) {
    cout << st.top();
    st.pop();
  }
  cout << endl;

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define ms(x, t) memset(x, t, sizeof(x))
typedef long long int ll;
typedef unsigned long long int ull;
const int limit = 3e3 + 1;
const int inf = 1e7;

int main() {
  string s, t;
  cin >> s >> t;
  int sl = s.length();
  int tl = t.length();
  int m[sl + 1][tl + 1];
  stack<char> st;
  ms(m, 0);
  for (int i = 1; i <= sl; i++) {
    for (int j = 1; j <= tl; j++) {
      if (s[i - 1] == t[j - 1]) {
        m[i][j] = 1 + m[i - 1][j - 1];
      } else {
        m[i][j] = max(m[i][j - 1], m[i - 1][j]);
      }
    }
  }
  int i = sl;
  int j = tl;
  for (; i != 0 && j != 0;) {
    if (s[i - 1] == t[j - 1]) {
      st.push(s[i - 1]);
      i--;
      j--;
    } else if (m[i][j] == m[i - 1][j]) {
      i--;
    } else {
      j--;
    }
  }
  while (!st.empty()) {
    cout << st.top();
    st.pop();
  }
  cout << endl;

  return 0;
}
Problem p03165 (C++)

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

#define N (int)3e2 + 5
#define f1 first
#define s2 second
#define INF 9999999
#define pb push_back
#define ll long long
#define ull unsigned long long;
#define all(a) a.begin(), a.end()
#define fri(a) freopen(a, "r", stdin);
#define fro(a) freopen(a, "w", stdout);

string str1, str2, ans;
int dp[N][N], n, m;

void lcs() {
  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 (str1[i - 1] == str2[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 main() {
  // fri("in.txt");
  // fro("out.txt");
  cin >> str1 >> str2;
  n = str1.length();
  m = str2.length();
  memset(dp, -1, sizeof(dp));
  lcs();
  while (n > 0 && m > 0) {
    if (str1[n - 1] == str2[m - 1]) {
      ans.pb(str1[n - 1]);
      n--;
      m--;
    } else if (dp[n - 1][m] > dp[n][m - 1]) {
      n--;
    } else
      m--;
  }
  reverse(all(ans));
  cout << ans << endl;
  return 0;
}

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

#define N (int)3e3 + 5
#define f1 first
#define s2 second
#define INF 9999999
#define pb push_back
#define ll long long
#define ull unsigned long long;
#define all(a) a.begin(), a.end()
#define fri(a) freopen(a, "r", stdin);
#define fro(a) freopen(a, "w", stdout);

string str1, str2, ans;
int dp[N][N], n, m;

void lcs() {
  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 (str1[i - 1] == str2[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 main() {
  // fri("in.txt");
  // fro("out.txt");
  cin >> str1 >> str2;
  n = str1.length();
  m = str2.length();
  memset(dp, -1, sizeof(dp));
  lcs();
  while (n > 0 && m > 0) {
    if (str1[n - 1] == str2[m - 1]) {
      ans.pb(str1[n - 1]);
      n--;
      m--;
    } else if (dp[n - 1][m] > dp[n][m - 1]) {
      n--;
    } else
      m--;
  }
  reverse(all(ans));
  cout << ans << endl;
  return 0;
}
Problem p03165 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#include <unordered_map>
#define M 1000000007
#define T 998244353
#define PI 3.142
#define ll long long

using namespace std;

int ldp[3001][3001];

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

#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif

  string s, t;
  cin >> s >> t;
  int i, j;
  for (i = 0; i < 3001; i++) {
    ldp[0][i] = 0;
    ldp[i][0] = 0;
  }
  for (i = 1; i <= s.length(); i++) {
    for (j = 1; j <= t.length(); j++) {
      if (s[i - 1] == t[j - 1]) {
        ldp[i][j] = ldp[i - 1][j - 1] + 1;
      } else {
        if (ldp[i - 1][j] > ldp[i][j - 1]) {
          ldp[i][j] = ldp[i - 1][j];
        } else {
          ldp[i][j] = ldp[i][j - 1];
        }
      }
    }
  }
  i = s.length();
  j = t.length();
  int ml = ldp[i][j];
  char d[ml];
  int ind = ml - 1;
  while (i > 0 && j > 0) {
    if (s[i - 1] == t[j - 1]) {
      d[ind] = s[i - 1];
      i--;
      j--;
      ind--;
    } else if (ldp[i - 1][j] > ldp[i][j - 1]) {
      i--;
    } else {
      j--;
    }
  }
  for (i = 0; i < ml; i++) {
    cout << d[i];
  }
  cout << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#include <unordered_map>
#define M 1000000007
#define T 998244353
#define PI 3.142
#define ll long long

using namespace std;

int ldp[3001][3001];

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

  string s, t;
  cin >> s >> t;
  int i, j;
  for (i = 0; i < 3001; i++) {
    ldp[0][i] = 0;
    ldp[i][0] = 0;
  }
  for (i = 1; i <= s.length(); i++) {
    for (j = 1; j <= t.length(); j++) {
      if (s[i - 1] == t[j - 1]) {
        ldp[i][j] = ldp[i - 1][j - 1] + 1;
      } else {
        if (ldp[i - 1][j] > ldp[i][j - 1]) {
          ldp[i][j] = ldp[i - 1][j];
        } else {
          ldp[i][j] = ldp[i][j - 1];
        }
      }
    }
  }
  i = s.length();
  j = t.length();
  int ml = ldp[i][j];
  char d[ml];
  int ind = ml - 1;
  while (i > 0 && j > 0) {
    if (s[i - 1] == t[j - 1]) {
      d[ind] = s[i - 1];
      i--;
      j--;
      ind--;
    } else if (ldp[i - 1][j] > ldp[i][j - 1]) {
      i--;
    } else {
      j--;
    }
  }
  for (i = 0; i < ml; i++) {
    cout << d[i];
  }
  cout << endl;
  return 0;
}
Problem p03165 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep1(i, n) for (int i = 1; i <= (int)(n); i++)
typedef int64_t Int;

int main() {
  string S, T;
  cin >> S >> T;
  S = " " + S;
  T = " " + T;

  vector<string> now(3010, ""), before(3010, "");

  rep1(i, S.length() - 1) {
    rep1(j, T.length() - 1) {
      if (S[i] == T[j]) {
        now[j] = before[j - 1] + S[i];
      } else {
        int L1 = before[j].length();
        int L2 = now[j - 1].length();

        if (L1 < L2)
          now[j] = now[j - 1];
        else
          now[j] = before[j];
      }
    }
    swap(now, before);
  }

  cout << before[T.length() - 1] << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep1(i, n) for (int i = 1; i <= (int)(n); i++)
typedef int64_t Int;

int main() {
  string S, T;
  cin >> S >> T;
  S = " " + S;
  T = " " + T;

  vector<string> now(3010, ""), before(3010, "");

  rep1(i, S.length() - 1) {
    rep1(j, T.length() - 1) {
      if (S[i] == T[j]) {
        now[j] = before[j - 1] + S[i];
      } else {
        int L1 = before[j].length();
        int L2 = now[j - 1].length();

        if (L1 < L2)
          now[j] = now[j - 1];
        else
          now[j] = before[j];
      }
    }

    before = now;
  }

  cout << before[T.length() - 1] << endl;
}
Problem p03165 (C++)

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

int main() {
  string s, t;
  cin >> s;
  cin >> t;
  int n = s.length();
  int m = t.length();
  vector<vector<int>> dp(n + 1, vector<int>(m + 1, 0));

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

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

int main() {
  string s, t;
  cin >> s;
  cin >> t;
  int n = s.length();
  int m = t.length();
  vector<vector<int>> dp(n + 1, vector<int>(m + 1, 0));

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

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define ll long long int
ll k;
int dp[3001][3001];
string final;
int lcs(string x, string y, int n, int m) {
  if (n == 0 || m == 0) {
    return 0;
  }

  if (dp[n][m] != -1) { // cout<<"here"<<endl;
    return dp[n][m];
  }

  if (x[n - 1] == y[m - 1]) {
    // final.push_back(x[n-1]);
    dp[n][m] = 1 + lcs(x, y, n - 1, m - 1);
    return dp[n][m];
  } else {
    dp[n][m] = max(lcs(x, y, n - 1, m), lcs(x, y, n, m - 1));
    return dp[n][m];
  }
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  memset(dp, -1, sizeof(dp));
  string x;
  cin >> x;
  string y;
  cin >> y;
  int n, m;
  n = x.length();
  m = y.length();
  k = lcs(x, y, n, m);
  int i = n, j = m;
  while (i > 0 && j > 0) {
    if (x[i - 1] == y[j - 1]) {
      final.push_back(x[i - 1]);

      i--;
      j--;
    }

    else if (dp[i - 1][j] > dp[i][j - 1])
      i--;
    else
      j--;
  }

  if (final.length() > 0) {
    for (int i = final.length() - 1; i >= 0; i--) {
      cout << final[i];
    }
  }

  // cout<<endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define ll long long int
ll k;
int dp[3001][3001];
string final;
int lcs(string &x, string &y, int n, int m) {
  if (n == 0 || m == 0) {
    return 0;
  }

  if (dp[n][m] != -1) { // cout<<"here"<<endl;
    return dp[n][m];
  }

  if (x[n - 1] == y[m - 1]) {
    // final.push_back(x[n-1]);
    dp[n][m] = 1 + lcs(x, y, n - 1, m - 1);
    return dp[n][m];
  } else {
    dp[n][m] = max(lcs(x, y, n - 1, m), lcs(x, y, n, m - 1));
    return dp[n][m];
  }
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  memset(dp, -1, sizeof(dp));
  string x;
  cin >> x;
  string y;
  cin >> y;
  int n, m;
  n = x.length();
  m = y.length();
  k = lcs(x, y, n, m);
  int i = n, j = m;
  while (i > 0 && j > 0) {
    if (x[i - 1] == y[j - 1]) {
      final.push_back(x[i - 1]);

      i--;
      j--;
    }

    else if (dp[i - 1][j] > dp[i][j - 1])
      i--;
    else
      j--;
  }

  if (final.length() > 0) {
    for (int i = final.length() - 1; i >= 0; i--) {
      cout << final[i];
    }
  }

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

Buggy submission (status: Runtime Error):
#include <cstring>
#include <iostream>
#include <string>
using namespace std;
int main() {
  string s1, s2;
  cin >> s1 >> s2;
  int **dp = new int *[s2.length() + 1];
  for (int i = 0; i <= s2.length(); i++) {
    dp[i] = new int[s1.length()];
    memset(dp[i], 0, s1.length() * sizeof(int));
  }
  for (int i = 1; i <= s2.length(); i++) {
    for (int j = 1; j <= s1.length(); j++) {
      dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]);
      if (s2[i - 1] == s1[j - 1]) {
        dp[i][j] = dp[i - 1][j - 1] + 1;
      }
    }
  }
  string revOut = "";
  int len = dp[s2.length()][s1.length()];
  int row = s2.length(), col = s1.length();
  while (row > 0 && col > 0) {
    if (dp[row][col] == dp[row - 1][col]) {
      row--;
    } else if (dp[row][col] == dp[row][col - 1]) {
      col--;
    } else if (dp[row][col] == dp[row - 1][col - 1] + 1) {
      revOut += s2[row - 1];
      row--;
      col--;
      // len--;
    }
  }
  /*
  for(int i=1;i<=s2.length();i++) {
        for(int j=1;j<=s1.length();j++) {
      cout<<dp[i][j]<<" ";
    }
    cout<<endl;
  }
  */
  for (int i = revOut.length() - 1; i >= 0; i--) {
    cout << revOut[i];
  }
  cout << endl;
  // cout<<dp[s2.length()][s1.length()]<<endl;
  return 0;
}

Fixed submission:
#include <cstring>
#include <iostream>
#include <string>
using namespace std;
int main() {
  string s1, s2;
  cin >> s1 >> s2;
  int **dp = new int *[s2.length() + 1];
  for (int i = 0; i <= s2.length(); i++) {
    dp[i] = new int[s1.length() + 1];
    memset(dp[i], 0, (s1.length() + 1) * sizeof(int));
  }
  for (int i = 1; i <= s2.length(); i++) {
    for (int j = 1; j <= s1.length(); j++) {
      dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]);
      if (s2[i - 1] == s1[j - 1]) {
        dp[i][j] = dp[i - 1][j - 1] + 1;
      }
    }
  }
  string revOut = "";
  int len = dp[s2.length()][s1.length()];
  int row = s2.length(), col = s1.length();
  while (row > 0 && col > 0) {
    if (dp[row][col] == dp[row - 1][col]) {
      row--;
    } else if (dp[row][col] == dp[row][col - 1]) {
      col--;
    } else if (dp[row][col] == dp[row - 1][col - 1] + 1) {
      revOut += s2[row - 1];
      row--;
      col--;
      // len--;
    }
  }
  /*
  for(int i=1;i<=s2.length();i++) {
        for(int j=1;j<=s1.length();j++) {
      cout<<dp[i][j]<<" ";
    }
    cout<<endl;
  }
  */
  for (int i = revOut.length() - 1; i >= 0; i--) {
    cout << revOut[i];
  }
  cout << endl;
  // cout<<dp[s2.length()][s1.length()]<<endl;
  return 0;
}
Problem p03165 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;
using ll = long long;

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

  vector<vector<int>> dp(ls + 1, vector<int>(lt + 1));
  rep(i, ls) {
    rep(j, lt) {
      if (s[i] == t[j]) {
        dp[i + 1][j + 1] = dp[i][j] + 1;
      } else {
        dp[i + 1][j + 1] = max(dp[i + 1][j], dp[i][j + 1]);
      }
    }
  }

  int p = ls, q = lt;
  string ans;
  while (p > 0 || q > 0) {
    if (dp[p][q] == dp[p - 1][q])
      p--;
    else if (dp[p][q] == dp[p][q - 1])
      q--;
    else {
      p--;
      q--;
      ans = s[p] + ans;
    }
  }

  cout << ans << endl;
}


Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;
using ll = long long;

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

  vector<vector<int>> dp(ls + 1, vector<int>(lt + 1));
  rep(i, ls) {
    rep(j, lt) {
      if (s[i] == t[j]) {
        dp[i + 1][j + 1] = dp[i][j] + 1;
      } else {
        dp[i + 1][j + 1] = max(dp[i + 1][j], dp[i][j + 1]);
      }
    }
  }

  int p = ls, q = lt;
  string ans;
  while (p > 0 && q > 0) {
    if (dp[p][q] == dp[p - 1][q])
      p--;
    else if (dp[p][q] == dp[p][q - 1])
      q--;
    else {
      p--;
      q--;
      ans = s[p] + ans;
    }
  }

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

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e3 + 100;
int n, W;
string a, b;
int dp[maxn], l[maxn][maxn];
stack<char> S;
int main() {
  cin >> a >> b;
  for (int j = 0; j < max(a.length(), b.length()); j++)
    for (int i = 0; i < b.length(); i++)
      l[j][i] = -1;
  for (int i = 0; i < a.length(); i++) {
    int maxans = 0, maxi = -1;
    for (int j = 0; j < b.length(); j++) {
      int maxans2 = maxans, maxi2 = maxi;
      if (dp[j] > maxans2) {
        maxans2 = dp[j];
        maxi2 = j;
      }
      if (a[i] == b[j]) {
        if (dp[j] < maxans + 1) {
          dp[j] = maxans + 1;
          l[dp[j]][j] = maxi;
        }
      }
      maxans = maxans2;
      maxi = maxi2;
    }
  }
  int maxans = 0, maxi = -1;
  for (int i = 0; i < b.length(); i++) {
    if (dp[i] > maxans) {
      maxans = dp[i];
      maxi = i;
    }
  }
  while (maxi != -1) {
    // cout<<maxi<<endl;
    S.push(b[maxi]);
    maxi = l[maxans--][maxi];
  }
  // cout<<maxans<<endl;
  while (!S.empty()) {
    cout << S.top();
    S.pop();
  }
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
const int maxn = 3e3 + 100;
int n, W;
string a, b;
int dp[maxn], l[maxn][maxn];
stack<char> S;
int main() {
  cin >> a >> b;
  for (int j = 0; j < max(a.length(), b.length()); j++)
    for (int i = 0; i < b.length(); i++)
      l[j][i] = -1;
  for (int i = 0; i < a.length(); i++) {
    int maxans = 0, maxi = -1;
    for (int j = 0; j < b.length(); j++) {
      int maxans2 = maxans, maxi2 = maxi;
      if (dp[j] > maxans2) {
        maxans2 = dp[j];
        maxi2 = j;
      }
      if (a[i] == b[j]) {
        if (dp[j] < maxans + 1) {
          dp[j] = maxans + 1;
          l[dp[j]][j] = maxi;
        }
      }
      maxans = maxans2;
      maxi = maxi2;
    }
  }
  int maxans = 0, maxi = -1;
  for (int i = 0; i < b.length(); i++) {
    if (dp[i] > maxans) {
      maxans = dp[i];
      maxi = i;
    }
  }
  while (maxi != -1) {
    // cout<<maxi<<endl;
    S.push(b[maxi]);
    maxi = l[maxans--][maxi];
  }
  // cout<<maxans<<endl;
  while (!S.empty()) {
    cout << S.top();
    S.pop();
  }
  return 0;
}
Problem p03165 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define ll int
const ll N = 3e3 + 5;
string s, t, dp[2][N];
int main() {
  ios::sync_with_stdio(0), cin.tie(0);
  cin >> s >> t;
  for (ll i = 0; i < s.size(); i++)
    for (ll j = 0; j < t.size(); j++)
      if (s[i] == t[j])
        dp[i % 2][j + 1] = dp[!(i % 2)][j] + s[i];
      else if (dp[i % 2][j].size() > dp[!(i % 2)][j + 1].size())
        dp[i % 2][j + 1] = dp[i % 2][j];
      else
        dp[i % 2][j + 1] = dp[!(i % 2)][j + 1];
  cout << dp[!(s.size() % 2)][t.size()];
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define ll int
const ll N = 3e3 + 5;
string s, t, dp[2][N];
int main() {
  ios::sync_with_stdio(0), cin.tie(0);
  cin >> s >> t;
  for (ll i = 0; i < s.size(); i++)
    for (ll j = 0; j < t.size(); j++)
      if (s[i] == t[j])
        dp[i % 2][j + 1] = dp[!(i % 2)][j], dp[i % 2][j + 1].push_back(s[i]);
      else if (dp[i % 2][j].size() > dp[!(i % 2)][j + 1].size())
        dp[i % 2][j + 1] = dp[i % 2][j];
      else
        dp[i % 2][j + 1] = dp[!(i % 2)][j + 1];
  cout << dp[!(s.size() % 2)][t.size()];
}
Problem p03165 (C++)

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

int main() {
  // your code goes here
  string s, t;
  cin >> s >> t;
  int l1 = s.length();
  int l2 = t.length();

  vector<vector<int>> dp(l1 + 1, vector<int>(l2 + 1, 0));
  for (int i = 0; i <= l1; i++) {
    for (int j = 0; j <= l2; 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]);
    }
  }

  int i = l1, j = l2;
  string ans = "";
  while (i >= 0 && j >= 0) {
    if (s[i - 1] == t[j - 1]) {
      ans.push_back(s[i - 1]);
      i--;
      j--;
    } else if (dp[i][j - 1] >= dp[i - 1][j]) {
      j--;
    } else
      i--;
  }
  reverse(ans.begin(), ans.end());
  cout << ans;
  return 0;
}


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

int main() {
  // your code goes here
  string s, t;
  cin >> s >> t;
  int l1 = s.length();
  int l2 = t.length();

  vector<vector<int>> dp(l1 + 1, vector<int>(l2 + 1, 0));
  for (int i = 0; i <= l1; i++) {
    for (int j = 0; j <= l2; 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]);
    }
  }

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

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define sc second
#define fr first
#define mp make_pair
const int N = 2e3 + 10, Max = 50 + 10;
const ll MOD = 998244353, OO = 1e14 + 2;
int dp[N][N];
string a, b;
vector<char> sol;
int solve(int i, int j) {
  if (i == a.size() || j == b.size())
    return 0;
  int &ret = dp[i][j];
  if (ret != -1)
    return ret;
  int c1 = solve(i + 1, j);
  int c2 = solve(i, j + 1);
  int c3 = 0;
  if (a[i] == b[j]) {
    c3 = 1 + solve(i + 1, j + 1);
  }
  return ret = max(c1, max(c2, c3));
}
void path(int i, int j) {
  if (i == a.size() || j == b.size())
    return;
  int optimal = solve(i, j);
  int c1 = solve(i + 1, j);
  int c2 = solve(i, j + 1);
  int c3 = 0;
  if (a[i] == b[i]) {
    c3 = 1 + solve(i + 1, j + 1);
  }
  if (optimal == c1) {
    path(i + 1, j);
  } else if (optimal == c2) {
    path(i, j + 1);
  } else {
    sol.pb(a[i]);
    path(i + 1, j + 1);
  }
}
int main() {
  memset(dp, -1, sizeof(dp));
  cin >> a >> b;
  path(0, 0);
  for (int i = 0; i < sol.size(); ++i)
    cout << sol[i];
  cout << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define sc second
#define fr first
#define mp make_pair
const int N = 3e3 + 10, Max = 50 + 10;
const ll MOD = 998244353, OO = 1e14 + 2;
int dp[N][N];
string a, b;
vector<char> sol;
int solve(int i, int j) {
  if (i == a.size() || j == b.size())
    return 0;
  int &ret = dp[i][j];
  if (ret != -1)
    return ret;
  int c1 = solve(i + 1, j);
  int c2 = solve(i, j + 1);
  int c3 = 0;
  if (a[i] == b[j]) {
    c3 = 1 + solve(i + 1, j + 1);
  }
  return ret = max(c1, max(c2, c3));
}
void path(int i, int j) {
  if (i == a.size() || j == b.size())
    return;
  int optimal = solve(i, j);
  int c1 = solve(i + 1, j);
  int c2 = solve(i, j + 1);
  int c3 = 0;
  if (a[i] == b[i]) {
    c3 = 1 + solve(i + 1, j + 1);
  }
  if (optimal == c1) {
    path(i + 1, j);
  } else if (optimal == c2) {
    path(i, j + 1);
  } else {
    sol.pb(a[i]);
    path(i + 1, j + 1);
  }
}
int main() {
  memset(dp, -1, sizeof(dp));
  cin >> a >> b;
  path(0, 0);
  for (int i = 0; i < sol.size(); ++i)
    cout << sol[i];
  cout << endl;
}
Problem p03165 (C++)

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

using namespace std;
#define ll long long int
#define X first
#define Y second
const int MAXN = 1e5 + 5;
string s, t;
int n, m;
int dp[105][105];

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

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


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

using namespace std;
#define ll long long int
#define X first
#define Y second
const int MAXN = 1e5 + 5;
string s, t;
int n, m;
int dp[3005][3005];

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

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

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

const int INF = 1145141919;
const long long INFL = 1LL << 60;

template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

int dp[3 * 1000][3 * 1000] = {};
int main() {
  string s, t;
  cin >> s >> t;
  s = "@" + s;
  t = "@" + t;
  for (int i = 1; i < s.size(); ++i) {
    for (int j = 1; j < t.size(); ++j) {
      if (s[i] == t[j]) {
        dp[i][j] = dp[i - 1][j - 1] + 1;
      } else {
        dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]);
      }
    }
  }
  int len = dp[s.size() - 1][t.size() - 1];
  int i = s.size() - 1;
  int j = t.size() - 1;
  string ans;
  while (len > 0) {
    if (s[i] == t[j]) {
      ans += s[i];
      --i;
      --j;
      --len;
    } else if (dp[i][j] == dp[i - 1][j]) {
      --i;
    } else {
      --j;
    }
  }
  for (int i = 0; i < ans.size(); ++i) {
    cout << ans[ans.size() - 1 - i];
  }
  cout << endl;
  return 0;
}

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

const int INF = 1145141919;
const long long INFL = 1LL << 60;

template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

int dp[3 * 1100][3 * 1100] = {};
int main() {
  string s, t;
  cin >> s >> t;
  s = "@" + s;
  t = "@" + t;
  for (int i = 1; i < s.size(); ++i) {
    for (int j = 1; j < t.size(); ++j) {
      if (s[i] == t[j]) {
        dp[i][j] = dp[i - 1][j - 1] + 1;
      } else {
        dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]);
      }
    }
  }
  int len = dp[s.size() - 1][t.size() - 1];
  int i = s.size() - 1;
  int j = t.size() - 1;
  string ans;
  while (len > 0) {
    if (s[i] == t[j]) {
      ans += s[i];
      --i;
      --j;
      --len;
    } else if (dp[i][j] == dp[i - 1][j]) {
      --i;
    } else {
      --j;
    }
  }
  for (int i = 0; i < ans.size(); ++i) {
    cout << ans[ans.size() - 1 - i];
  }
  cout << endl;
  return 0;
}
Problem p03165 (C++)

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

  if (dp[s.size()][t.size()] == 0) {
    cout << "";
    return 0;
  }
  // branch and bound
  int i = s.size(), j = t.size();
  string ans = "";
  while (i > 0 && j > 0) {
    if (s[i - 1] == t[j - 1]) {
      ans = ans + s[i - 1];
      i--, j--;
    } else {
      if (dp[i - 1][j] > dp[i][j - 1]) {
        i--;
      } else {
        j--;
      }
    }
  }
  reverse(ans.begin(), ans.end());
  cout << ans;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
const int N = 10001;
int dp[N][N];
int main() {
  string s, t;
  cin >> s;
  cin >> t;
  for (int i = 1; i <= s.size(); i++) {
    for (int j = 1; j <= t.size(); j++) {
      if (s[i - 1] == t[j - 1]) {
        dp[i][j] = max(dp[i][j - 1], 1 + dp[i - 1][j - 1]);
      } else {
        dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]);
      }
    }
  }

  if (dp[s.size()][t.size()] == 0) {
    cout << "";
    return 0;
  }
  // branch and bound
  int i = s.size(), j = t.size();
  string ans = "";
  while (i > 0 && j > 0) {
    if (s[i - 1] == t[j - 1]) {
      ans = ans + s[i - 1];
      i--, j--;
    } else {
      if (dp[i - 1][j] > dp[i][j - 1]) {
        i--;
      } else {
        j--;
      }
    }
  }
  reverse(ans.begin(), ans.end());
  cout << ans;
  return 0;
}
Problem p03165 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
string a, b;
int S[1001][1001];
vector<char> ans;
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> a >> b;
  for (int i = 0; i < a.size(); i++) {
    for (int j = 0; j < b.size(); j++) {
      if (a[i] == b[j]) {
        S[i + 1][j + 1] = S[i][j] + 1;
      } else {
        S[i + 1][j + 1] = max(S[i + 1][j], S[i][j + 1]);
      }
    }
  }
  int k = S[a.size()][b.size()];
  int i = a.size();
  int j = b.size();
  // cout<<k<<"\n";
  while (k) { // 대칭성이 있다는게 큰 장점인듯. 그러면 n^2/2로 줄이는것도 되겠네
    while (j && S[i][j] == k)
      j--;
    j++;
    while (i && S[i][j] == k)
      i--;
    i++;
    ans.push_back(b[j - 1]);
    k--;
    i--;
    j--;
  }
  for (int i = ans.size() - 1; i >= 0; i--) {
    cout << ans[i];
  }
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
string a, b;
int S[3001][3001];
vector<char> ans;
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> a >> b;
  for (int i = 0; i < a.size(); i++) {
    for (int j = 0; j < b.size(); j++) {
      if (a[i] == b[j]) {
        S[i + 1][j + 1] = S[i][j] + 1;
      } else {
        S[i + 1][j + 1] = max(S[i + 1][j], S[i][j + 1]);
      }
    }
  }
  int k = S[a.size()][b.size()];
  int i = a.size();
  int j = b.size();
  // cout<<k<<"\n";
  while (k) { // 대칭성이 있다는게 큰 장점인듯. 그러면 n^2/2로 줄이는것도 되겠네
    while (j && S[i][j] == k)
      j--;
    j++;
    while (i && S[i][j] == k)
      i--;
    i++;
    ans.push_back(b[j - 1]);
    k--;
    i--;
    j--;
  }
  for (int i = ans.size() - 1; i >= 0; i--) {
    cout << ans[i];
  }
}
Problem p03165 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;
#define int long long
#define All(v) (v).begin(), (v).end()
int dy[4] = {-1, 0, 1, 0};
int dx[4] = {0, 1, 0, -1};
int Dy[8] = {-1, -1, 0, 1, 1, 1, 0, -1};
int Dx[8] = {0, 1, 1, 1, 0, -1, -1, -1};
const int mod = 1000000007;
const int inf = mod * mod;
const int d5 = 100100;
int dp[3000][3000];
signed main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  string s, t;
  cin >> s >> t;
  int n = s.size(), m = t.size();

  // dp[i][j]:sをi番目まで,tをj番目まで見た時のLCS
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      dp[i][j] = 0;
    }
  }
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      if (s[i] == t[j])
        dp[i + 1][j + 1] = dp[i][j] + 1;
      else
        dp[i + 1][j + 1] = max(dp[i][j + 1], dp[i + 1][j]);
    }
  }
  string res = "";
  while (n && m) {
    if (dp[n][m] == dp[n - 1][m])
      n--;
    else if (dp[n][m] == dp[n][m - 1])
      m--;
    else {
      res += s[n - 1];
      n--;
      m--;
    }
  }
  reverse(All(res));
  cout << res << endl;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;
#define int long long
#define All(v) (v).begin(), (v).end()
int dy[4] = {-1, 0, 1, 0};
int dx[4] = {0, 1, 0, -1};
int Dy[8] = {-1, -1, 0, 1, 1, 1, 0, -1};
int Dx[8] = {0, 1, 1, 1, 0, -1, -1, -1};
const int mod = 1000000007;
const int inf = mod * mod;
const int d5 = 100100;
int dp[3030][3030];
signed main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  string s, t;
  cin >> s >> t;
  int n = s.size(), m = t.size();

  // dp[i][j]:sをi番目まで,tをj番目まで見た時のLCS
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      dp[i][j] = 0;
    }
  }
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      if (s[i] == t[j])
        dp[i + 1][j + 1] = dp[i][j] + 1;
      else
        dp[i + 1][j + 1] = max(dp[i][j + 1], dp[i + 1][j]);
    }
  }
  string res = "";
  while (n && m) {
    if (dp[n][m] == dp[n - 1][m])
      n--;
    else if (dp[n][m] == dp[n][m - 1])
      m--;
    else {
      res += s[n - 1];
      n--;
      m--;
    }
  }
  reverse(All(res));
  cout << res << endl;
}
Problem p03165 (C++)

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

int main(int argc, char const *argv[]) {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  ll t = 1;
  // cin>>t;
  while (t--) {
    string inp1, inp2;
    cin >> inp1 >> inp2;
    ll n = inp1.size(), m = inp2.size();
    ll dp[n + 1][m + 1];
    for (ll i = 0; i <= n; i++) {
      for (ll j = 0; j <= m; j++) {
        if (i == 0 || j == 0) {
          dp[i][j] = 0;
        } else if (inp1[i - 1] == inp2[j - 1]) {
          dp[i][j] = dp[i - 1][j - 1] + 1;
        } else {
          dp[i][j] = max(dp[i][j - 1], dp[i - 1][j]);
        }
      }
    }
    vector<char> v;
    ll i = n, j = m;
    while (i > 0 && j > 0) {
      if (inp1[i - 1] == inp2[j - 1]) {
        v.push_back(inp1[i - 1]);
        i--, j--;

      } else if (dp[i - 1][j] >= dp[i][j - 1]) {
        i--;
      } else
        j--;
    }
    reverse(v.begin(), v.end());
    for (auto elem : v) {
      cout << elem;
    }
    cout << endl;
  }
  return 0;
}


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

int main() {

  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  ll t = 1;
  // cin>>t;
  while (t--) {
    string inp1, inp2;
    cin >> inp1 >> inp2;
    ll n = inp1.size(), m = inp2.size();
    ll dp[n + 1][m + 1];
    for (ll i = 0; i <= n; i++) {
      for (ll j = 0; j <= m; j++) {
        if (i == 0 || j == 0) {
          dp[i][j] = 0;
        } else if (inp1[i - 1] == inp2[j - 1]) {
          dp[i][j] = dp[i - 1][j - 1] + 1;
        } else {
          dp[i][j] = max(dp[i][j - 1], dp[i - 1][j]);
        }
      }
    }
    vector<char> v;
    ll i = n, j = m;
    while (i > 0 && j > 0) {
      if (inp1[i - 1] == inp2[j - 1]) {
        v.push_back(inp1[i - 1]);
        i--, j--;

      } else if (dp[i - 1][j] >= dp[i][j - 1]) {
        i--;
      } else
        j--;
    }
    reverse(v.begin(), v.end());
    for (auto elem : v) {
      cout << elem;
    }
    cout << endl;
  }
  return 0;
}
Problem p03165 (C++)

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

const int SIZE = 1e3 + 5;

int DP[SIZE][SIZE];
char z[SIZE][SIZE];

string B = "0", A = "0", K = "0", P, Q;

void read() {
  cin >> P >> Q;
  A += P;
  B += Q;
}

void COUNT_LCS() {
  for (int i = 1; i <= A.size() - 1; i++)
    for (int j = 1; j <= B.size() - 1; j++) {
      if (A[i] == B[j]) {
        DP[i][j] += DP[i - 1][j - 1] + 1;
        z[i][j] = A[i];
      } else {
        if (DP[i - 1][j] > DP[i][j - 1]) {
          DP[i][j] += DP[i - 1][j];
          z[i][j] = '#';
        } else {
          DP[i][j] += DP[i][j - 1];
          z[i][j] = '$';
        }
      }
    }
}

void odzyskaj() {
  int j = B.size() - 1;
  for (int i = A.size() - 1; i >= 1;) {
    if (z[i][j] == '#') {
      i--;
      continue;
    }
    if (z[i][j] == '$') {
      j--;
      continue;
    }
    if (z[i][j] != 0) {
      K += z[i][j];
      i--;
      j--;
      continue;
    }
    break;
  }
}

void RESULT() {
  for (int i = K.size() - 1; i >= 1; i--)
    cout << K[i];
}

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

  read();
  COUNT_LCS();
  odzyskaj();
  RESULT();
}


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

const int SIZE = 3e3 + 5;

int DP[SIZE][SIZE];
char z[SIZE][SIZE];

string B = "0", A = "0", K = "0", P, Q;

void read() {
  cin >> P >> Q;
  A += P;
  B += Q;
}

void COUNT_LCS() {
  for (int i = 1; i <= A.size() - 1; i++)
    for (int j = 1; j <= B.size() - 1; j++) {
      if (A[i] == B[j]) {
        DP[i][j] += DP[i - 1][j - 1] + 1;
        z[i][j] = A[i];
      } else {
        if (DP[i - 1][j] > DP[i][j - 1]) {
          DP[i][j] += DP[i - 1][j];
          z[i][j] = '#';
        } else {
          DP[i][j] += DP[i][j - 1];
          z[i][j] = '$';
        }
      }
    }
}

void odzyskaj() {
  int j = B.size() - 1;
  for (int i = A.size() - 1; i >= 1;) {
    if (z[i][j] == '#') {
      i--;
      continue;
    }
    if (z[i][j] == '$') {
      j--;
      continue;
    }
    if (z[i][j] != 0) {
      K += z[i][j];
      i--;
      j--;
      continue;
    }
    break;
  }
}

void RESULT() {
  for (int i = K.size() - 1; i >= 1; i--)
    cout << K[i];
}

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

  read();
  COUNT_LCS();
  odzyskaj();
  RESULT();
}
Problem p03165 (C++)

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

using namespace std;

string s, t;
int c[3000][3000] = {};
pair<int, int> w[3000][3000] = {};
string ans = "";

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

  cin >> s >> t;

  c[0][0] = (s[0] == t[0]);

  for (int i = 1; i < s.length(); i++) {
    c[i][0] = (c[i - 1][0] | (s[i] == t[0]));
    w[i][0] = {-1, 0};
  }
  for (int i = 1; i < t.length(); i++) {
    c[0][i] = (c[0][i - 1] | (s[0] == t[i]));
    w[0][i] = {0, -1};
  }

  for (int i = 1; i < s.length(); i++) {
    for (int j = 1; j < t.length(); j++) {
      if (s[i] == t[j]) {
        c[i][j] = c[i - 1][j - 1] + 1;
        w[i][j] = {-1, -1};
      } else {
        if (c[i - 1][j] > c[i][j - 1]) {
          c[i][j] = c[i - 1][j];
          w[i][j] = {-1, 0};
        } else {
          c[i][j] = c[i][j - 1];
          w[i][j] = {0, -1};
        }
      }
    }
  }

  int i = s.length() - 1;
  int j = t.length() - 1;

  while (1) {
    int x = w[i][j].first;
    int y = w[i][j].second;

    if ((s[i] == t[j] && c[i][j] == 1) || (x == -1 && y == -1))
      ans += s[i];
    if (s[i] == t[j] && c[i][j] == 1)
      break;

    i += x;
    j += y;
  }

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

  cout << ans;

  return 0;
}


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

using namespace std;

string s, t;
int c[3000][3000] = {};
pair<int, int> w[3000][3000] = {};
string ans = "";

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

  cin >> s >> t;

  c[0][0] = (s[0] == t[0]);

  for (int i = 1; i < s.length(); i++) {
    c[i][0] = (c[i - 1][0] | (s[i] == t[0]));
    w[i][0] = {-1, 0};
  }
  for (int i = 1; i < t.length(); i++) {
    c[0][i] = (c[0][i - 1] | (s[0] == t[i]));
    w[0][i] = {0, -1};
  }

  for (int i = 1; i < s.length(); i++) {
    for (int j = 1; j < t.length(); j++) {
      if (s[i] == t[j]) {
        c[i][j] = c[i - 1][j - 1] + 1;
        w[i][j] = {-1, -1};
      } else {
        if (c[i - 1][j] > c[i][j - 1]) {
          c[i][j] = c[i - 1][j];
          w[i][j] = {-1, 0};
        } else {
          c[i][j] = c[i][j - 1];
          w[i][j] = {0, -1};
        }
      }
    }
  }

  int i = s.length() - 1;
  int j = t.length() - 1;

  if (c[i][j] == 0)
    return 0;

  while (1) {
    int x = w[i][j].first;
    int y = w[i][j].second;

    if ((s[i] == t[j] && c[i][j] == 1) || (x == -1 && y == -1))
      ans += s[i];
    if (s[i] == t[j] && c[i][j] == 1)
      break;

    i += x;
    j += y;
  }

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

  cout << ans;

  return 0;
}
Problem p03165 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define fore(i, init, n) for (long long i = init; i < n; i++)
#define FIN                                                                    \
  ios::sync_with_stdio(0);                                                     \
  cin.tie(0);                                                                  \
  cout.tie(0);
typedef long long ll;

const int MAXN = 1e4 + 5;
int sn, tn, dp[MAXN][MAXN];
string s, t;
bool vis[MAXN], v[MAXN];

void debug() {
  cout << endl << endl;
  fore(i, 0, sn) {
    fore(j, 0, tn) { cout << dp[i][j] << " "; }
    cout << s[i];
    cout << endl;
  }
  fore(i, 0, tn + 1) cout << t[i] << " ";
}

int main() {
  FIN;

  cin >> s >> t;

  sn = s.size();
  tn = t.size();

  for (int i = sn - 1; i >= 0; i--) {
    for (int j = tn - 1; j >= 0; j--) {
      if (s[i] == t[j]) {
        dp[i][j] = 1 + dp[i + 1][j + 1];
      }
      dp[i][j] = max({dp[i][j], dp[i + 1][j], dp[i][j + 1]});
    }
  }

  int i = 0, j = 0;
  string res = "";
  while (i < sn || j < tn) {
    if (s[i] == t[j]) {
      res += s[i];
      i++;
      j++;
    } else {
      if (dp[i + 1][j] > dp[i][j + 1])
        i++;
      else
        j++;
    }
  }

  cout << res << endl;

  // debug();
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define fore(i, init, n) for (long long i = init; i < n; i++)
#define FIN                                                                    \
  ios::sync_with_stdio(0);                                                     \
  cin.tie(0);                                                                  \
  cout.tie(0);
typedef long long ll;

const int MAXN = 1e4 + 5;
int sn, tn, dp[MAXN][MAXN];
string s, t;
bool vis[MAXN], v[MAXN];

void debug() {
  cout << endl << endl;
  fore(i, 0, sn) {
    fore(j, 0, tn) { cout << dp[i][j] << " "; }
    cout << s[i];
    cout << endl;
  }
  fore(i, 0, tn + 1) cout << t[i] << " ";
}

int main() {
  FIN;

  cin >> s >> t;

  sn = s.size();
  tn = t.size();

  for (int i = sn - 1; i >= 0; i--) {
    for (int j = tn - 1; j >= 0; j--) {
      if (s[i] == t[j]) {
        dp[i][j] = 1 + dp[i + 1][j + 1];
      }
      dp[i][j] = max({dp[i][j], dp[i + 1][j], dp[i][j + 1]});
    }
  }

  int i = 0, j = 0;
  string res = "";
  while (i < sn && j < tn) {
    if (s[i] == t[j]) {
      res += s[i];
      i++;
      j++;
    } else {
      if (dp[i + 1][j] > dp[i][j + 1])
        i++;
      else
        j++;
    }
  }

  cout << res << endl;

  // debug();
}
Problem p03165 (C++)

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

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

string print_lcs(string &s, string &t, int len) {

  string LCS = "";
  int i = 0, j = 0;

  while (len > 0) {
    if (s[i] == t[j]) {
      LCS += s[i];
      i++;
      j++;
      len--;
    } else {
      if (dp[i][j + 1] > dp[i + 1][j]) {
        j++;
      } else {
        i++;
      }
    }
  }
  return LCS;
}

int length_lcs(string &s, string &t, int i, int j) {

  if (i >= s.length() || j >= t.length()) {
    return 0;
  }

  if (s[i] == t[j]) {
    return dp[i][j] =
               1 +
               length_lcs(
                   s, t, i + 1,
                   j + 1); /// here dp[i][j] represents length of lcs of string
                           /// s from i to end and string t from j to end
  } else {
    return dp[i][j] =
               max(length_lcs(s, t, i + 1, j), length_lcs(s, t, i, j + 1));
  }
}

int main() {

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

  memset(dp, -1, sizeof(dp));

  int len = length_lcs(s, t, 0, 0);

  string LCS;

  LCS = print_lcs(s, t, len);

  cout << LCS << "\n";

  return 0;
}


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

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

string print_lcs(string &s, string &t, int len) {

  string LCS = "";
  int i = 0, j = 0;

  while (len > 0) {
    if (s[i] == t[j]) {
      LCS += s[i];
      i++;
      j++;
      len--;
    } else {
      if (dp[i][j + 1] > dp[i + 1][j]) {
        j++;
      } else {
        i++;
      }
    }
  }
  return LCS;
}

int length_lcs(string &s, string &t, int i, int j) {

  if (i >= s.length() || j >= t.length()) {
    return 0;
  }
  if (dp[i][j] != -1) {
    return dp[i][j];
  }

  if (s[i] == t[j]) {
    return dp[i][j] =
               1 +
               length_lcs(
                   s, t, i + 1,
                   j + 1); /// here dp[i][j] represents length of lcs of string
                           /// s from i to end and string t from j to end
  } else {
    return dp[i][j] =
               max(length_lcs(s, t, i + 1, j), length_lcs(s, t, i, j + 1));
  }
}

int main() {

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

  memset(dp, -1, sizeof(dp));

  int len = length_lcs(s, t, 0, 0);

  string LCS;

  LCS = print_lcs(s, t, len);

  cout << LCS << "\n";

  return 0;
}
Problem p03165 (C++)

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

using namespace std;

int main() {

#ifndef ONLINE_JUDGE
  // for getting input from input.txt
  freopen("input.txt", "r", stdin);
  // for writing output to output.txt
  freopen("output.txt", "w", stdout);
#endif
  string s1;
  string s2;
  cin >> s1 >> s2;
  int n = s1.size();
  int m = s2.size();
  int dp[n + 1][m + 1];
  for (int i = 0; i <= n; i++)
    dp[i][0] = 0;
  for (int i = 0; i <= m; i++)
    dp[0][i] = 0;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      if (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;
  string ans = "";
  while (i != 0 || j != 0) {
    if (s1[i - 1] == s2[j - 1]) {
      ans += s1[i - 1];
      i--;
      j--;
    } else {
      if (dp[i - 1][j] > dp[i][j - 1])
        i--;
      else
        j--;
    }
  }
  reverse(ans.begin(), ans.end());
  cout << ans << endl;
}


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

using namespace std;

int main() {

#ifndef ONLINE_JUDGE
  // for getting input from input.txt
  freopen("input.txt", "r", stdin);
  // for writing output to output.txt
  freopen("output.txt", "w", stdout);
#endif
  string s1;
  string s2;
  cin >> s1 >> s2;
  int n = s1.size();
  int m = s2.size();
  int dp[n + 1][m + 1];
  for (int i = 0; i <= n; i++)
    dp[i][0] = 0;
  for (int i = 0; i <= m; i++)
    dp[0][i] = 0;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      if (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;
  string ans = "";
  while (i != 0 && j != 0) {
    if (s1[i - 1] == s2[j - 1]) {
      ans += s1[i - 1];
      i--;
      j--;
    } else {
      if (dp[i - 1][j] > dp[i][j - 1])
        i--;
      else
        j--;
    }
  }
  reverse(ans.begin(), ans.end());
  cout << ans << endl;
}