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

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

using namespace std;

int recurse(int i, int j, vector<vector<int>> &dp, string s, string t) {
  if (i == 0 || j == 0)
    return 0;
  else {
    if (dp[i][j] != -1)
      return dp[i][j];
    else {
      if (s[i - 1] == t[j - 1])
        dp[i][j] = recurse(i - 1, j - 1, dp, s, t) + 1;
      else
        dp[i][j] =
            max(recurse(i - 1, j, dp, s, t), recurse(i, j - 1, dp, s, t));
      return dp[i][j];
    }
  }
}

int main(int argc, char const *argv[]) {
  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));
  dp[n][m] = recurse(n, m, dp, s, t);

  int i = n;
  int j = m;
  int k = 1;
  char lcs[dp[n][m] + 1];
  lcs[dp[n][m]] = '\0';
  while (i > 0 && j > 0) {
    if (s[i - 1] == t[j - 1]) {
      lcs[dp[n][m] - k] = s[i - 1];
      k++;
      i--;
      j--;
    } else {
      if (dp[i - 1][j] > dp[i][j - 1])
        i--;
      else
        j--;
    }
  }
  cout << lcs;
  return 0;
}


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

using namespace std;

int recurse(int i, int j, vector<vector<int>> &dp, string &s, string &t) {
  if (i == 0 || j == 0)
    return 0;
  else {
    if (dp[i][j] != -1)
      return dp[i][j];
    else {
      if (s[i - 1] == t[j - 1])
        dp[i][j] = recurse(i - 1, j - 1, dp, s, t) + 1;
      else
        dp[i][j] =
            max(recurse(i - 1, j, dp, s, t), recurse(i, j - 1, dp, s, t));
      return dp[i][j];
    }
  }
}

int main(int argc, char const *argv[]) {
  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));
  dp[n][m] = recurse(n, m, dp, s, t);

  int i = n;
  int j = m;
  int k = 1;
  char lcs[dp[n][m] + 1];
  lcs[dp[n][m]] = '\0';
  while (i > 0 && j > 0) {
    if (s[i - 1] == t[j - 1]) {
      lcs[dp[n][m] - k] = s[i - 1];
      k++;
      i--;
      j--;
    } else {
      if (dp[i - 1][j] > dp[i][j - 1])
        i--;
      else
        j--;
    }
  }
  cout << lcs;
  return 0;
}
Problem p03165 (C++)

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

int dp[3005][3005];

int lcs(string a, string b, int n, int m) {
  if (n == 0 or m == 0)
    return 0;
  if (dp[n][m] != -1)
    return dp[n][m];
  if (a[n - 1] == b[m - 1]) {
    return dp[n][m] = 1 + lcs(a, b, n - 1, m - 1);
  }
  int x = lcs(a, b, n - 1, m);
  int y = lcs(a, b, n, m - 1);
  return dp[n][m] = max(x, y);
}

void solve() {
  string a, b;
  cin >> a >> b;

  int n = a.size(), m = b.size();
  memset(dp, -1, sizeof(dp));

  int len = lcs(a, b, n, m);
  char ans[len + 1];
  ans[len] = '\0';
  int i = n, j = m;
  while (i > 0 and j > 0) {
    if (a[i - 1] == b[j - 1]) {
      ans[len - 1] = a[i - 1];
      i--, j--, len--;
    } else if (dp[i - 1][j] > dp[i][j - 1])
      i--;
    else
      j--;
  }
  for (int k = 0; ans[k] != '\0'; ++k)
    cout << ans[k];
}
int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);

  int t = 1;
  while (t--)
    solve();

  return 0;
}

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

int dp[3005][3005];

int lcs(string &a, string &b, int n, int m) {
  if (n == 0 or m == 0)
    return 0;
  if (dp[n][m] != -1)
    return dp[n][m];
  if (a[n - 1] == b[m - 1]) {
    return dp[n][m] = 1 + lcs(a, b, n - 1, m - 1);
  }
  int x = lcs(a, b, n - 1, m);
  int y = lcs(a, b, n, m - 1);
  return dp[n][m] = max(x, y);
}

void solve() {
  string a, b;
  cin >> a >> b;

  int n = a.size(), m = b.size();
  memset(dp, -1, sizeof(dp));

  int len = lcs(a, b, n, m);
  char ans[len + 1];
  ans[len] = '\0';
  int i = n, j = m;
  while (i > 0 and j > 0) {
    if (a[i - 1] == b[j - 1]) {
      ans[len - 1] = a[i - 1];
      i--, j--, len--;
    } else if (dp[i - 1][j] > dp[i][j - 1])
      i--;
    else
      j--;
  }
  for (int k = 0; ans[k] != '\0'; ++k)
    cout << ans[k];
}
int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);

  int t = 1;
  while (t--)
    solve();

  return 0;
}
Problem p03165 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define ALL(x) (x).begin(), (x).end()
typedef long long ll;
typedef pair<int, int> pii;
const int INF = 1e9;
const ll LINF = 1e15;
const int MOD = 1000000007;
const double PI = acos(-1);
int dx[4] = {0, 1, 0, -1};
int dy[4] = {1, 0, -1, 0};

int dp[3001][3001];

int main() {
  string s, t;
  cin >> s >> t;
  int n = s.size();
  int m = t.size();
  memset(dp, 0, sizeof(dp));
  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 + 1][j], dp[i][j + 1]);
      }
    }
  }

  int length = dp[n][m];
  int l = n;
  int r = m;
  string ans = "";
  while (length >= 0) {
    if (s[l - 1] == t[r - 1]) {
      ans += s[l - 1];
      l--;
      r--;
      length--;
    } else if (dp[l][r] == dp[l - 1][r])
      l--;
    else
      r--;
  }
  reverse(ALL(ans));
  cout << ans << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define ALL(x) (x).begin(), (x).end()
typedef long long ll;
typedef pair<int, int> pii;
const int INF = 1e9;
const ll LINF = 1e15;
const int MOD = 1000000007;
const double PI = acos(-1);
int dx[4] = {0, 1, 0, -1};
int dy[4] = {1, 0, -1, 0};

int dp[3001][3001];

int main() {
  string s, t;
  cin >> s >> t;
  int n = s.size();
  int m = t.size();
  memset(dp, 0, sizeof(dp));
  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 + 1][j], dp[i][j + 1]);
      }
    }
  }

  int length = dp[n][m];
  int l = n;
  int r = m;
  string ans = "";
  while (length > 0) {
    if (s[l - 1] == t[r - 1]) {
      ans += s[l - 1];
      l--;
      r--;
      length--;
    } else if (dp[l][r] == dp[l - 1][r])
      l--;
    else
      r--;
  }
  reverse(ALL(ans));
  cout << ans << endl;
}
Problem p03165 (C++)

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

typedef long long int ll;
typedef unsigned long long int llu;

int dp[100][100];
/*  Memoized LCS
int lcs(string a,string b,int n,int m){
        if(n==0 || m==0){
                return 0;
        }
        if(dp[n][m]!= (-1)){
                return dp[n][m];
        }
        if(a[n-1]==b[m-1]){
                return dp[n][m]=1+lcs(a,b,n-1,m-1);
        }
        else{
                return dp[n][m] =  max(lcs(a,b,n-1,m),lcs(a,b,n,m-1));
        }
}
*/
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  string a, b;
  cin >> a >> b;
  int n, m;
  n = a.length();
  m = b.length();
  for (int i = 0; i <= n; i++) {
    for (int j = 0; j <= m; j++) {
      if (i == 0 || j == 0) {
        dp[i][j] = 0;
      }
    }
  }
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      if (a[i - 1] == b[j - 1]) {
        dp[i][j] = 1 + dp[i - 1][j - 1];
      } else {
        dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]);
      }
    }
  }
  // cout<<lcs(a,b,a.size(),b.size());
  // length of lcs
  // cout<<dp[n][m]<<"\n";
  // Printing lcs
  int i = n, j = m;
  string res = "";
  while (i > 0 && j > 0) {
    if (a[i - 1] == b[j - 1]) {
      res.push_back(a[i - 1]);
      i--, j--;
    } else {
      if (dp[i][j - 1] >= dp[i - 1][j]) {
        j--;
      } else {
        i--;
      }
    }
  }
  reverse(res.begin(), res.end());
  cout << res;
  return 0;
}


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

typedef long long int ll;
typedef unsigned long long int llu;

int dp[3001][3001];
/*  Memoized LCS
int lcs(string a,string b,int n,int m){
        if(n==0 || m==0){
                return 0;
        }
        if(dp[n][m]!= (-1)){
                return dp[n][m];
        }
        if(a[n-1]==b[m-1]){
                return dp[n][m]=1+lcs(a,b,n-1,m-1);
        }
        else{
                return dp[n][m] =  max(lcs(a,b,n-1,m),lcs(a,b,n,m-1));
        }
}
*/
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  string a, b;
  cin >> a >> b;
  int n, m;
  n = a.length();
  m = b.length();
  for (int i = 0; i <= n; i++) {
    for (int j = 0; j <= m; j++) {
      if (i == 0 || j == 0) {
        dp[i][j] = 0;
      }
    }
  }
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      if (a[i - 1] == b[j - 1]) {
        dp[i][j] = 1 + dp[i - 1][j - 1];
      } else {
        dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]);
      }
    }
  }
  // cout<<lcs(a,b,a.size(),b.size());
  // length of lcs
  // cout<<dp[n][m]<<"\n";
  // Printing lcs
  int i = n, j = m;
  string res = "";
  while (i > 0 && j > 0) {
    if (a[i - 1] == b[j - 1]) {
      res.push_back(a[i - 1]);
      i--, j--;
    } else {
      if (dp[i][j - 1] >= dp[i - 1][j]) {
        j--;
      } else {
        i--;
      }
    }
  }
  reverse(res.begin(), res.end());
  cout << res;
  return 0;
}
Problem p03165 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
int main() {
  string s, d;
  cin >> s >> d;

  char c[1010];

  int p = s.size();
  int q = d.size();

  int dp[p + 2][q + 2];

  for (int i = 0; i <= p; i++) {
    for (int j = 0; j <= q; j++) {
      if (i == 0 || j == 0) {
        dp[i][j] = 0;
      } else if (s[i - 1] == d[j - 1]) {
        dp[i][j] = dp[i - 1][j - 1] + 1;
      } else {
        dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]);
      }
    }
  }
  // cout << dp[p][q] << endl;
  int i = p;
  int j = q;
  int k = 0;
  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 {
      c[k] = s[i - 1];
      i--;
      j--;
      k++;
    }
  }
  // cout << p << " " << q << endl;

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

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int main() {
  string s, d;
  cin >> s >> d;

  char c[3010];

  int p = s.size();
  int q = d.size();

  int dp[p + 2][q + 2];

  for (int i = 0; i <= p; i++) {
    for (int j = 0; j <= q; j++) {
      if (i == 0 || j == 0) {
        dp[i][j] = 0;
      } else if (s[i - 1] == d[j - 1]) {
        dp[i][j] = dp[i - 1][j - 1] + 1;
      } else {
        dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]);
      }
    }
  }
  // cout << dp[p][q] << endl;
  int i = p;
  int j = q;
  int k = 0;
  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 {
      c[k] = s[i - 1];
      i--;
      j--;
      k++;
    }
  }
  // cout << p << " " << q << endl;

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

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

using namespace std;
const int maxn = 3010;
int dp[maxn][maxn];

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


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

using namespace std;
const int maxn = 3010;
int dp[maxn][maxn];

int main() {
  string s, t;
  cin >> s >> t;
  for (int i = 0; i < 3010; i++) {
    dp[i][0] = 0;
    dp[0][i] = 0;
  }
  for (int i = 1; i <= s.size(); i++) {
    for (int j = 1; j <= t.size(); j++) {
      if (s[i - 1] == t[j - 1]) {
        dp[i][j] = dp[i - 1][j - 1] + 1;
      } else
        dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]);
    }
  }
  string ans;
  int i = s.size(), j = t.size();
  while (i > 0 && j > 0) {
    if (s[i - 1] == t[j - 1]) {
      ans += s[i - 1];
      i--;
      j--;
    } else if (dp[i][j] == dp[i][j - 1]) {
      j--;
    } else if (dp[i][j] == dp[i - 1][j]) {
      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;

char DIR[3000][3000];

void printLCS(int l1, int l2, string str) {
  if (l1 == 0 || l2 == 0) {
    return;
  }
  if (DIR[l1][l2] == 'A') {
    printLCS(l1 - 1, l2 - 1, str);
    cout << str[l1 - 1];
  } else if (DIR[l1][l2] == 'U') {
    printLCS(l1 - 1, l2, str);
  } else {
    printLCS(l1, l2 - 1, str);
  }
}

void findLCS(string str1, string str2) {

  int l1 = str1.length();
  int l2 = str2.length();

  int M[l1 + 1][l2 + 1];

  for (int i = 0; i <= l1; i++) {
    M[i][0] = 0;
  }
  for (int i = 0; i <= l2; i++) {
    M[0][i] = 0;
  }

  for (int i = 0; i <= l1; i++) {
    for (int j = 0; j <= l2; j++) {
      DIR[i][j] = 'N';
    }
  }

  for (int i = 1; i <= l1; i++) {
    for (int j = 1; j <= l2; j++) {
      if (str1[i - 1] == str2[j - 1]) {
        M[i][j] = M[i - 1][j - 1] + 1;
        DIR[i][j] = 'A';
      } else {
        if (M[i - 1][j] >= M[i][j - 1]) {
          M[i][j] = M[i - 1][j];
          DIR[i][j] = 'U';
        } else {
          M[i][j] = M[i][j - 1];
          DIR[i][j] = 'L';
        }
      }
    }
  }

  /*  for(int i=0;i<=l1;i++){
        for(int j=0;j<=l2;j++){
            cout<<M[i][j]<<" ";
        }
        cout<<endl;
    }*/

  printLCS(l1, l2, str1);
}

int main() {
  string str1, str2;
  cin >> str1 >> str2;
  findLCS(str1, str2);
  return 0;
}


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

using namespace std;

char DIR[3005][3005];

void printLCS(int l1, int l2, string str) {
  if (l1 == 0 || l2 == 0) {
    return;
  }
  if (DIR[l1][l2] == 'A') {
    printLCS(l1 - 1, l2 - 1, str);
    cout << str[l1 - 1];
  } else if (DIR[l1][l2] == 'U') {
    printLCS(l1 - 1, l2, str);
  } else {
    printLCS(l1, l2 - 1, str);
  }
}

void findLCS(string str1, string str2) {

  int l1 = str1.length();
  int l2 = str2.length();

  int M[l1 + 1][l2 + 1];

  for (int i = 0; i <= l1; i++) {
    M[i][0] = 0;
  }
  for (int i = 0; i <= l2; i++) {
    M[0][i] = 0;
  }

  for (int i = 0; i <= l1; i++) {
    for (int j = 0; j <= l2; j++) {
      DIR[i][j] = 'N';
    }
  }

  for (int i = 1; i <= l1; i++) {
    for (int j = 1; j <= l2; j++) {
      if (str1[i - 1] == str2[j - 1]) {
        M[i][j] = M[i - 1][j - 1] + 1;
        DIR[i][j] = 'A';
      } else {
        if (M[i - 1][j] >= M[i][j - 1]) {
          M[i][j] = M[i - 1][j];
          DIR[i][j] = 'U';
        } else {
          M[i][j] = M[i][j - 1];
          DIR[i][j] = 'L';
        }
      }
    }
  }

  /*  for(int i=0;i<=l1;i++){
        for(int j=0;j<=l2;j++){
            cout<<M[i][j]<<" ";
        }
        cout<<endl;
    }*/

  printLCS(l1, l2, str1);
}

int main() {
  string str1, str2;
  cin >> str1 >> str2;
  findLCS(str1, str2);
  return 0;
}
Problem p03165 (C++)

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

using namespace std;

string a, b;
long long n, m;
const long long N = 1e3 + 10;
long long dp[N][N];
long long dfs(long long x, long long y) {
  if (x >= n)
    return 0;
  if (y >= m)
    return 0;
  if (dp[x][y] != -1)
    return dp[x][y];
  long long choice1 = 0;
  long long choice2 = 0;
  long long choice3 = 0;
  if (a[x] == b[y]) {
    choice1 = 1 + dfs(x + 1, y + 1);
  } else {
    choice2 = dfs(x + 1, y);
    choice3 = dfs(x, y + 1);
  }
  return dp[x][y] = max({choice1, choice2, choice3});
}
string ans = "";
void build(long long x, long long y) {
  if (x >= n)
    return;
  if (y >= m)
    return;
  // if ( dp[x][y] != -1 ) return dp[x][y] ;
  long long choice1 = 0;
  long long choice2 = 0;
  long long choice3 = 0;
  if (a[x] == b[y]) {
    choice1 = 1 + dfs(x + 1, y + 1);
  } else {
    choice2 = dfs(x + 1, y);
    choice3 = dfs(x, y + 1);
  }
  long long optimal = dfs(x, y);

  if (optimal == choice2) {

    build(x + 1, y);
  } else if (optimal == choice3) {

    build(x, y + 1);
  } else if (optimal == choice1) {
    ans += a[x];
    build(x + 1, y + 1);
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  memset(dp, -1, sizeof dp);
  cin >> a >> b;
  n = (long long)a.size();
  m = (long long)b.size();
  dfs(0, 0);
  build(0, 0);
  cout << ans << endl;
  return 0;
}


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

using namespace std;

string a, b;
long long n, m;
const long long N = 3e3 + 10;
long long dp[N][N];
long long dfs(long long x, long long y) {
  if (x >= n)
    return 0;
  if (y >= m)
    return 0;
  if (dp[x][y] != -1)
    return dp[x][y];
  long long choice1 = 0;
  long long choice2 = 0;
  long long choice3 = 0;
  if (a[x] == b[y]) {
    choice1 = 1 + dfs(x + 1, y + 1);
  } else {
    choice2 = dfs(x + 1, y);
    choice3 = dfs(x, y + 1);
  }
  return dp[x][y] = max({choice1, choice2, choice3});
}
string ans = "";
void build(long long x, long long y) {
  if (x >= n)
    return;
  if (y >= m)
    return;
  // if ( dp[x][y] != -1 ) return dp[x][y] ;
  long long choice1 = 0;
  long long choice2 = 0;
  long long choice3 = 0;
  if (a[x] == b[y]) {
    choice1 = 1 + dfs(x + 1, y + 1);
  } else {
    choice2 = dfs(x + 1, y);
    choice3 = dfs(x, y + 1);
  }
  long long optimal = dfs(x, y);

  if (optimal == choice2) {

    build(x + 1, y);
  } else if (optimal == choice3) {

    build(x, y + 1);
  } else if (optimal == choice1) {
    ans += a[x];
    build(x + 1, y + 1);
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  memset(dp, -1, sizeof dp);
  cin >> a >> b;
  n = (long long)a.size();
  m = (long long)b.size();
  dfs(0, 0);
  build(0, 0);
  cout << ans << endl;
  return 0;
}
Problem p03165 (C++)

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

void LCS(string s, string t) {
  vector<vector<ll>> dp(MAX_N, vector<ll>(MAX_M));
  vector<vector<pair<int, int>>> root(MAX_N, vector<pair<int, int>>(MAX_M));

  rep(i, s.size()) {
    rep(j, t.size()) {
      if (s[i] == t[j]) {
        dp[i + 1][j + 1] = dp[i][j] + 1;
        root[i + 1][j + 1].first = i;
        root[i + 1][j + 1].second = j;
      } else {
        if (dp[i + 1][j] > dp[i][j + 1]) {
          dp[i + 1][j + 1] = dp[i + 1][j];
          root[i + 1][j + 1].first = i + 1;
          root[i + 1][j + 1].second = j;
        } else {
          dp[i + 1][j + 1] = dp[i][j + 1];
          root[i + 1][j + 1].first = i;
          root[i + 1][j + 1].second = j + 1;
        }
      }
    }
  }
  string res;
  int rx = t.size();
  int ry = s.size();
  int ny = root[s.size()][t.size()].first;
  int nx = root[s.size()][t.size()].second;
  if ((rx - nx) == 1 && (ry - ny) == 1) {
    res = s[ny] + res;
  }

  while (nx != 0 && ny != 0) {

    rx = nx;
    ry = ny;
    ny = root[ry][rx].first;
    nx = root[ry][rx].second;
    if ((rx - nx) == 1 && (ry - ny) == 1) {
      res = s[ny] + res;
    }
  };
  cout << res << endl;
}
int main() {
  string s;
  string t;
  cin >> s;
  cin >> t;
  LCS(s, t);

  return 0;
}

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

void LCS(string s, string t) {
  vector<vector<ll>> dp(MAX_N + 1, vector<ll>(MAX_M + 1));
  vector<vector<pair<int, int>>> root(MAX_N + 1,
                                      vector<pair<int, int>>(MAX_M + 1));

  rep(i, s.size()) {
    rep(j, t.size()) {
      if (s[i] == t[j]) {
        dp[i + 1][j + 1] = dp[i][j] + 1;
        root[i + 1][j + 1].first = i;
        root[i + 1][j + 1].second = j;
      } else {
        if (dp[i + 1][j] > dp[i][j + 1]) {
          dp[i + 1][j + 1] = dp[i + 1][j];
          root[i + 1][j + 1].first = i + 1;
          root[i + 1][j + 1].second = j;
        } else {
          dp[i + 1][j + 1] = dp[i][j + 1];
          root[i + 1][j + 1].first = i;
          root[i + 1][j + 1].second = j + 1;
        }
      }
    }
  }
  string res;
  int rx = t.size();
  int ry = s.size();
  int ny = root[s.size()][t.size()].first;
  int nx = root[s.size()][t.size()].second;
  if ((rx - nx) == 1 && (ry - ny) == 1) {
    res = s[ny] + res;
  }

  while (nx != 0 && ny != 0) {

    rx = nx;
    ry = ny;
    ny = root[ry][rx].first;
    nx = root[ry][rx].second;
    if ((rx - nx) == 1 && (ry - ny) == 1) {
      res = s[ny] + res;
    }
  };
  cout << res << endl;
}
int main() {
  string s;
  string t;
  cin >> s;
  cin >> t;
  LCS(s, t);

  return 0;
}
Problem p03165 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
const int N = 1010;
string s1, s2;
int n, m;
int dp[N + 7][N + 7], dir[N + 7][N + 7];
int solve(int i, int j) {
  if (i >= n || j >= m)
    return 0;
  if (dp[i][j] != -1)
    return dp[i][j];
  int ret1 = 0, ret2 = 0, ret3 = 0;
  if (s1[i] == s2[j]) {
    ret1 = solve(i + 1, j + 1) + 1;
  } else {
    ret2 = solve(i + 1, j);
    if (ret1 < ret2) {
      ret1 = ret2;
    }
    ret3 = solve(i, j + 1);
    if (ret1 < ret3) {
      ret1 = ret3;
    }
  }
  return dp[i][j] = ret1;
}
string s;
void print(int i, int j) {
  if (i >= n || j >= m)
    return;
  int ret1 = 0, ret2 = 0, ret3 = 0;
  if (s1[i] == s2[j]) {
    s += s1[i];
    print(i + 1, j + 1);
  } else {
    ret2 = solve(i + 1, j);
    ret3 = solve(i, j + 1);
    if (ret2 == dp[i][j] && ret3 == dp[i][j]) {
      if (s1[i] < s2[j]) {
        print(i + 1, j);
      } else {
        print(i, j + 1);
      }
    } else if (ret2 == dp[i][j]) {
      print(i + 1, j);
    } else if (ret3 == dp[i][j]) {
      print(i, j + 1);
    }
  }
}
int main() {
  cin >> s1 >> s2;
  n = (int)s1.length(), m = (int)s2.length();
  memset(dp, -1, sizeof(dp));
  solve(0, 0);
  print(0, 0);
  cout << s << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
const int N = 3010;
string s1, s2;
int n, m;
int dp[N + 7][N + 7], dir[N + 7][N + 7];
int solve(int i, int j) {
  if (i >= n || j >= m)
    return 0;
  if (dp[i][j] != -1)
    return dp[i][j];
  int ret1 = 0, ret2 = 0, ret3 = 0;
  if (s1[i] == s2[j]) {
    ret1 = solve(i + 1, j + 1) + 1;
  } else {
    ret2 = solve(i + 1, j);
    if (ret1 < ret2) {
      ret1 = ret2;
    }
    ret3 = solve(i, j + 1);
    if (ret1 < ret3) {
      ret1 = ret3;
    }
  }
  return dp[i][j] = ret1;
}
string s;
void print(int i, int j) {
  if (i >= n || j >= m)
    return;
  int ret1 = 0, ret2 = 0, ret3 = 0;
  if (s1[i] == s2[j]) {
    s += s1[i];
    print(i + 1, j + 1);
  } else {
    ret2 = solve(i + 1, j);
    ret3 = solve(i, j + 1);
    if (ret2 == dp[i][j] && ret3 == dp[i][j]) {
      if (s1[i] < s2[j]) {
        print(i + 1, j);
      } else {
        print(i, j + 1);
      }
    } else if (ret2 == dp[i][j]) {
      print(i + 1, j);
    } else if (ret3 == dp[i][j]) {
      print(i, j + 1);
    }
  }
}
int main() {
  cin >> s1 >> s2;
  n = (int)s1.length(), m = (int)s2.length();
  memset(dp, -1, sizeof(dp));
  solve(0, 0);
  print(0, 0);
  cout << s << endl;
  return 0;
}
Problem p03165 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <deque>
#include <fstream>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stdlib.h>
#include <string>
#include <vector>

using namespace std;

#define ll long long
vector<vector<int>> vv;

string a, b;
void ans(int i, int j) {
  if (vv[i][j] == vv[i - 1][j]) {
    ans(i - 1, j);
    return;
  }
  if (vv[i][j] == vv[i][j - 1]) {
    ans(i, j - 1);
    return;
  }

  if (vv[i - 1][j - 1])
    ans(i - 1, j - 1);
  cout << a[i - 1];
  return;
}

signed main() {
  cin >> a >> b;

  vv.resize(a.size() + 1, (vector<int>(b.size() + 1)));
  for (int i = 0; i < a.size(); i++)
    vv[i][0];
  for (int i = 0; i < b.size(); i++)
    vv[0][i];

  for (int i = 1; i <= a.size(); i++)
    for (int j = 1; j <= b.size(); j++) {
      if (a[i - 1] == b[j - 1])
        vv[i][j] = vv[i - 1][j - 1] + 1;
      else
        vv[i][j] = max(vv[i - 1][j], vv[i][j - 1]);
    }
  ans(int(a.size()), int(b.size()));
  cin >> a;
}

Fixed submission:
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <deque>
#include <fstream>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stdlib.h>
#include <string>
#include <vector>

using namespace std;

#define ll long long
vector<vector<int>> vv;

string a, b;
void ans(int i, int j) {
  if (!vv[i][j])
    return;
  if (vv[i][j] == vv[i - 1][j]) {
    ans(i - 1, j);
    return;
  }
  if (vv[i][j] == vv[i][j - 1]) {
    ans(i, j - 1);
    return;
  }

  if (vv[i - 1][j - 1])
    ans(i - 1, j - 1);
  cout << a[i - 1];
  return;
}

signed main() {
  cin >> a >> b;

  vv.resize(a.size() + 1, (vector<int>(b.size() + 1)));
  for (int i = 0; i < a.size(); i++)
    vv[i][0];
  for (int i = 0; i < b.size(); i++)
    vv[0][i];

  for (int i = 1; i <= a.size(); i++)
    for (int j = 1; j <= b.size(); j++) {
      if (a[i - 1] == b[j - 1])
        vv[i][j] = vv[i - 1][j - 1] + 1;
      else
        vv[i][j] = max(vv[i - 1][j], vv[i][j - 1]);
    }
  ans(int(a.size()), int(b.size()));
  cin >> a;
}
Problem p03165 (C++)

Buggy submission (status: Runtime Error):
// #pragma GCC optimize("03")
#include <bits/stdc++.h>
#define ll long long
#define fi first
#define se second
#define mod 1000000007

using namespace std;

int n, m;
int dp[3010][3010];
string a, b;
vector<char> sol;

ll getsum(int l, int r) { return dp[r] - dp[l - 1]; }

int main() {
  // ifstream cin("tst.in");
  // ofstream cout("tst.out");
  ios_base::sync_with_stdio(0);
  cin.tie(NULL);

  cin >> a >> b;
  n = a.size();
  m = b.size();

  a = '+' + a;
  b = '+' + b;

  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++)
      if (a[i] == b[j])
        dp[i][j] = dp[i - 1][j - 1] + 1;
      else
        dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]);

  while (n > 0 && m > 0) {
    while (dp[n - 1][m] == dp[n][m])
      n--;
    while (dp[n][m - 1] == dp[n][m])
      m--;
    sol.push_back(a[n]);
    n--;
    m--;
  }

  reverse(sol.begin(), sol.end());
  for (auto it : sol)
    cout << it;

  return 0;
}

Fixed submission:
// #pragma GCC optimize("03")
#include <bits/stdc++.h>
#define ll long long
#define fi first
#define se second
#define mod 1000000007

using namespace std;

int n, m;
int dp[3010][3010];
string a, b;
vector<char> sol;

ll getsum(int l, int r) { return dp[r] - dp[l - 1]; }

int main() {
  // ifstream cin("tst.in");
  // ofstream cout("tst.out");
  ios_base::sync_with_stdio(0);
  cin.tie(NULL);

  cin >> a >> b;
  n = a.size();
  m = b.size();

  a = '+' + a;
  b = '+' + b;

  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++)
      if (a[i] == b[j])
        dp[i][j] = dp[i - 1][j - 1] + 1;
      else
        dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]);

  while (n > 0 && m > 0 && dp[n][m] > 0) {
    while (dp[n - 1][m] == dp[n][m])
      n--;
    while (dp[n][m - 1] == dp[n][m])
      m--;
    sol.push_back(a[n]);
    n--;
    m--;
  }

  reverse(sol.begin(), sol.end());
  for (auto it : sol)
    cout << it;

  return 0;
}
Problem p03165 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#define ios                                                                    \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(0);                                                                  \
  cout.tie(0);
typedef long long int ll;
using namespace std;
const int N = 3e3 + 5;
#define fi first
#define se second
#define pb push_back
#define endl "\n"
ll ans[N][N];
int main() {
  string s, t;
  cin >> s >> t;
  ll n = s.length(), m = t.length();
  for (ll i = 1; i <= n; ++i) {
    for (ll j = 1; j <= m; ++j) {
      if (s[i - 1] == t[j - 1]) {
        ans[i][j] = ans[i - 1][j - 1] + 1;
      } else
        ans[i][j] = max(ans[i][j - 1], ans[i - 1][j]);
    }
  }
  string res = "";
  ll i = n, j = m;
  while (i || j) {
    if (ans[i][j] != ans[i][j - 1] && ans[i][j] != ans[i - 1][j]) {
      res += t[j - 1];
      --i;
      --j;
    } else if (ans[i][j] == ans[i][j - 1]) {
      // res+=t[j-1];
      --j;
    } else if (ans[i][j] == ans[i - 1][j]) {
      --i;
    }
  }
  ll u = res.size();
  for (ll i = u - 1; i >= 0; --i)
    cout << res[i];
  /* for(ll i=0;i<=n;++i)
   {
       for(ll j=0;j<=m;++j)
       {
           cout<<ans[i][j]<<" ";
       }
       cout<<endl;
   }*/
}


Fixed submission:
#include <bits/stdc++.h>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#define ios                                                                    \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(0);                                                                  \
  cout.tie(0);
typedef long long int ll;
using namespace std;
const int N = 3e3 + 5;
#define fi first
#define se second
#define pb push_back
#define endl "\n"
ll ans[N][N];
int main() {
  string s, t;
  cin >> s >> t;
  ll n = s.length(), m = t.length();
  for (ll i = 1; i <= n; ++i) {
    for (ll j = 1; j <= m; ++j) {
      if (s[i - 1] == t[j - 1]) {
        ans[i][j] = ans[i - 1][j - 1] + 1;
      } else
        ans[i][j] = max(ans[i][j - 1], ans[i - 1][j]);
    }
  }
  string res = "";
  ll i = n, j = m;
  while (i && j) {
    if (ans[i][j] != ans[i][j - 1] && ans[i][j] != ans[i - 1][j]) {
      res += t[j - 1];
      --i;
      --j;
    } else if (ans[i][j] == ans[i][j - 1]) {
      // res+=t[j-1];
      --j;
    } else if (ans[i][j] == ans[i - 1][j]) {
      --i;
    }
  }
  ll u = res.size();
  for (ll i = u - 1; i >= 0; --i)
    cout << res[i];
  /* for(ll i=0;i<=n;++i)
   {
       for(ll j=0;j<=m;++j)
       {
           cout<<ans[i][j]<<" ";
       }
       cout<<endl;
   }*/
}
Problem p03165 (C++)

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

typedef long long ll;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef pair<ll, ll> pll;
typedef vector<bool> vb;
const ll oo = 0x3f3f3f3f3f3f3f3f;
const double eps = 1e-9;
#define sz(c) ll((c).size())
#define all(c) begin(c), end(c)
#define FOR(i, a, b) for (ll i = (a); i < (b); i++)
#define FORD(i, a, b) for (ll i = (b)-1; i >= (a); i--)
#define mp make_pair
#define pb push_back
#define eb emplace_back
#define xx first
#define yy second
#define TR(X)                                                                  \
  ({                                                                           \
    if (1)                                                                     \
      cerr << "TR: " << (#X) << " = " << (X) << endl;                          \
  })

string a, b;
const ll N = 3010;
ll dp[N][N];

ll rec(ll i, ll j) {
  if (i == sz(a) || j == sz(b))
    return 0;
  if (dp[i][j] != -1)
    return dp[i][j];

  if (a[i] == b[j])
    return dp[i][j] = 1 + rec(i + 1, j + 1);
  return dp[i][j] = max(rec(i + 1, j), rec(i, j + 1));
}

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

  cin >> a >> b;
  memset(dp, -1, sizeof dp);

  ll i = 0, j = 0;
  while (i < sz(a) || j < sz(b)) {
    if (a[i] == b[j]) {
      cout << a[i];
      i++, j++;
    } else {
      if (rec(i, j) == rec(i + 1, j))
        i++;
      else
        j++;
    }
  }
  cout << endl;
}


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

typedef long long ll;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef pair<ll, ll> pll;
typedef vector<bool> vb;
const ll oo = 0x3f3f3f3f3f3f3f3f;
const double eps = 1e-9;
#define sz(c) ll((c).size())
#define all(c) begin(c), end(c)
#define FOR(i, a, b) for (ll i = (a); i < (b); i++)
#define FORD(i, a, b) for (ll i = (b)-1; i >= (a); i--)
#define mp make_pair
#define pb push_back
#define eb emplace_back
#define xx first
#define yy second
#define TR(X)                                                                  \
  ({                                                                           \
    if (1)                                                                     \
      cerr << "TR: " << (#X) << " = " << (X) << endl;                          \
  })

string a, b;
const ll N = 3010;
ll dp[N][N];

ll rec(ll i, ll j) {
  if (i == sz(a) || j == sz(b))
    return 0;
  if (dp[i][j] != -1)
    return dp[i][j];

  if (a[i] == b[j])
    return dp[i][j] = 1 + rec(i + 1, j + 1);
  return dp[i][j] = max(rec(i + 1, j), rec(i, j + 1));
}

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

  cin >> a >> b;
  memset(dp, -1, sizeof dp);

  ll i = 0, j = 0;
  while (i < sz(a) && j < sz(b)) {
    if (a[i] == b[j]) {
      cout << a[i];
      i++, j++;
    } else {
      if (rec(i, j) == rec(i + 1, j))
        i++;
      else
        j++;
    }
  }
  cout << endl;
}
Problem p03165 (C++)

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

typedef long long ll;

int main() {
  string s, t;
  cin >> s >> t;
  int s_len = s.length(), t_len = t.length();
  int dp[305][305];
  for (int i = 0; i <= s_len; i++) {
    for (int j = 0; j <= t_len; j++) {
      dp[i][j] = 0;
    }
  }
  for (int i = 0; i <= s_len; i++) {
    for (int j = 0; j <= t_len; j++) {
      dp[i + 1][j] = max(dp[i + 1][j], dp[i][j]);
      dp[i][j + 1] = max(dp[i][j + 1], dp[i][j]);
      if (i < s_len && j < t_len && s[i] == t[j]) {
        dp[i + 1][j + 1] = max(dp[i][j] + 1, dp[i + 1][j + 1]);
      }
    }
  }

#ifdef __DEBUG__
  for (int i = 0; i <= s_len; i++) {
    for (int j = 0; j <= t_len; j++) {
      cout << dp[i][j] << " ";
    }
    cout << endl;
  }
#endif

  string ret;
  ret.reserve(min(s_len, t_len));
  int i = s_len, j = t_len;
  while (i > 0 && j > 0) {
    if (dp[i][j] == dp[i][j - 1]) {
      j--;
    } else if (dp[i][j] == dp[i - 1][j]) {
      i--;
    } else {
      ret.push_back(s[i - 1]);
      i--;
      j--;
    }
  }
  reverse(ret.begin(), ret.end());
  cout << ret << endl;
}


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

typedef long long ll;

int main() {
  string s, t;
  cin >> s >> t;
  int s_len = s.length(), t_len = t.length();
  int dp[3005][3005];
  for (int i = 0; i <= s_len; i++) {
    for (int j = 0; j <= t_len; j++) {
      dp[i][j] = 0;
    }
  }
  for (int i = 0; i <= s_len; i++) {
    for (int j = 0; j <= t_len; j++) {
      dp[i + 1][j] = max(dp[i + 1][j], dp[i][j]);
      dp[i][j + 1] = max(dp[i][j + 1], dp[i][j]);
      if (i < s_len && j < t_len && s[i] == t[j]) {
        dp[i + 1][j + 1] = max(dp[i][j] + 1, dp[i + 1][j + 1]);
      }
    }
  }

#ifdef __DEBUG__
  for (int i = 0; i <= s_len; i++) {
    for (int j = 0; j <= t_len; j++) {
      cout << dp[i][j] << " ";
    }
    cout << endl;
  }
#endif

  string ret;
  ret.reserve(min(s_len, t_len));
  int i = s_len, j = t_len;
  while (i > 0 && j > 0) {
    if (dp[i][j] == dp[i][j - 1]) {
      j--;
    } else if (dp[i][j] == dp[i - 1][j]) {
      i--;
    } else {
      ret.push_back(s[i - 1]);
      i--;
      j--;
    }
  }
  reverse(ret.begin(), ret.end());
  cout << ret << endl;
}
Problem p03165 (C++)

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

const int N = 3 * 1e3 + 10;

int dp[N][N], al, bl, r, c;
string a, b;
vector<char> ans;

int main() {
  cin >> a >> b;
  al = a.length();
  bl = b.length();

  for (int i = 1; i <= al; i++) {
    for (int j = 1; j <= bl; j++) {
      if (a[i] == b[j])
        dp[i][j] = max(dp[i - 1][j - 1] + 1, max(dp[i - 1][j], dp[i][j - 1]));
      else
        dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]);
    }
  }
  int f = dp[al][bl];
  r = al;
  c = bl;
  while (f != 0) {
    if (a[r - 1] == b[c - 1]) {
      ans.push_back(a[r - 1]);
      r--;
      c--;
      f--;
    }

    else {
      if (dp[r - 1][c] == dp[r][c - 1]) {
        if (r - 1 != 0)
          r--;
        else
          c--;
      } else if (dp[r - 1][c] > dp[r][c - 1])
        r--;
      else
        c--;
    }
  }
  // cout<<dp[al][bl]<<"\n";
  for (auto x = ans.crbegin(); x != ans.crend(); x++)
    cout << *x;
}

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

const int N = 3 * 1e3 + 10;

int dp[N][N], al, bl, r, c;
string a, b;
vector<char> ans;

int main() {
  cin >> a >> b;
  al = a.length();
  bl = b.length();

  for (int i = 1; i <= al; i++) {
    for (int j = 1; j <= bl; j++) {
      if (a[i - 1] == b[j - 1])
        dp[i][j] = max(dp[i - 1][j - 1] + 1, max(dp[i - 1][j], dp[i][j - 1]));
      else
        dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]);
    }
  }
  int f = dp[al][bl];
  r = al;
  c = bl;
  while (f != 0) {
    if (a[r - 1] == b[c - 1]) {
      ans.push_back(a[r - 1]);
      r--;
      c--;
      f--;
    }

    else {
      if (dp[r - 1][c] == dp[r][c - 1]) {
        if (r - 1 != 0)
          r--;
        else
          c--;
      } else if (dp[r - 1][c] > dp[r][c - 1])
        r--;
      else
        c--;
    }
  }
  // cout<<dp[al][bl]<<"\n";
  for (auto x = ans.crbegin(); x != ans.crend(); x++)
    cout << *x;
}
Problem p03165 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
string ch, ch1;
int n, m, dp[4000][4000];
int lcs(int i, int j) {
  if (i == n || j == m)
    return 0;
  int x, y;
  if (ch[i] == ch1[j])
    return dp[i][j] = 1 + lcs(i + 1, j + 1);
  x = lcs(i + 1, j);
  y = lcs(i, j + 1);
  return dp[i][j] = max(x, y);
}
int main() {
  cin >> ch >> ch1;
  memset(dp, -1, sizeof(dp));
  n = ch.size();
  m = ch1.size();
  int l = lcs(0, 0);
  int i = 0, j = 0;
  while (i < n && j < m) {
    if (ch[i] == ch1[j]) {
      printf("%c", ch[i]);
      i++;
      j++;
    } else if (dp[i + 1][j] >= dp[i][j + 1])
      i++;
    else
      j++;
  }
  //  cout<<l<<endl ;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
string ch, ch1;
int n, m, dp[4000][4000];
int lcs(int i, int j) {
  if (i == n || j == m)
    return 0;
  if (dp[i][j] != -1)
    return dp[i][j];
  int x, y;
  if (ch[i] == ch1[j])
    return dp[i][j] = 1 + lcs(i + 1, j + 1);
  x = lcs(i + 1, j);
  y = lcs(i, j + 1);
  return dp[i][j] = max(x, y);
}
int main() {
  cin >> ch >> ch1;
  memset(dp, -1, sizeof(dp));
  n = ch.size();
  m = ch1.size();
  int l = lcs(0, 0);
  int i = 0, j = 0;
  while (i < n && j < m) {
    if (ch[i] == ch1[j]) {
      printf("%c", ch[i]);
      i++;
      j++;
    } else if (dp[i + 1][j] >= dp[i][j + 1])
      i++;
    else
      j++;
  }
  //  cout<<l<<endl ;
}
Problem p03165 (C++)

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

#define fi first
#define se second

const int N = 3030;
const long long mod = 1e9 + 9;

using namespace std;

string s, t;
int d[N][N];
int p[N][N];

int main() {
  // freopen("input.txt", "r", stdin);
  // freopen("output.txt", "w", stdout);
  ios_base::sync_with_stdio(0);

  cin >> s >> t;
  for (int i = 1; i <= s.size(); i++) {
    for (int j = 1; j <= t.size(); j++) {
      if (s[i - 1] == t[j - 1]) {
        d[i][j] = d[i - 1][j - 1] + 1;
        p[i][j] = 3;
      } else if (d[i - 1][j] > d[i][j - 1]) {
        d[i][j] = d[i - 1][j];
        p[i][j] = 1;
      } else {
        d[i][j] = d[i][j - 1];
        p[i][j] = 2;
      }
    }
  }
  string res = "";
  int n = s.size(), m = t.size();
  while (n || m) {
    if (p[n][m] == 3) {
      res += s[n - 1];
    }
    int dif = p[n][m];
    if (dif & 1) {
      n -= 1;
    }
    if (dif & 2) {
      m -= 1;
    }
  }
  reverse(res.begin(), res.end());
  cout << res << "\n";
}


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

#define fi first
#define se second

const int N = 3030;
const long long mod = 1e9 + 9;

using namespace std;

string s, t;
int d[N][N];
int p[N][N];

int main() {
  // freopen("input.txt", "r", stdin);
  // freopen("output.txt", "w", stdout);
  ios_base::sync_with_stdio(0);

  cin >> s >> t;
  for (int i = 1; i <= s.size(); i++) {
    for (int j = 1; j <= t.size(); j++) {
      if (s[i - 1] == t[j - 1]) {
        d[i][j] = d[i - 1][j - 1] + 1;
        p[i][j] = 3;
      } else if (d[i - 1][j] > d[i][j - 1]) {
        d[i][j] = d[i - 1][j];
        p[i][j] = 1;
      } else {
        d[i][j] = d[i][j - 1];
        p[i][j] = 2;
      }
    }
  }
  string res = "";
  int n = s.size(), m = t.size();
  while (n && m) {
    if (p[n][m] == 3) {
      res += s[n - 1];
    }
    int dif = p[n][m];
    if (dif & 1) {
      n -= 1;
    }
    if (dif & 2) {
      m -= 1;
    }
  }
  reverse(res.begin(), res.end());
  cout << res << "\n";
}
Problem p03165 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define vi vector<int>
#define all(x) (x).begin(), (x).end()
#define F first
#define S second
#define pb push_back
#define pp pair<int, int>
#define rep(i, l, r) for (int i = l; i < r; i++)
int main() {
#ifndef ONLINE_JUDGE
  freopen("input.txt.txt", "r", stdin);
  freopen("output.txt.txt", "w", stdout);
#endif
  ios::sync_with_stdio(false);
  cin.tie(0);
  string s, t;
  cin >> s >> t;
  int m = s.size();
  int n = t.size();
  int dp[m][n];
  pp par[m][n];
  rep(i, 0, m) {
    rep(j, 0, n) {
      dp[i][j] = 0;
      if (s[i] == t[j]) {
        if (i > 0 and j > 0)
          dp[i][j] = 1 + dp[i - 1][j - 1];
        else
          dp[i][j] = 1;
      } else {
        if (i > 0)
          dp[i][j] = max(dp[i][j], dp[i - 1][j]);
        if (j > 0)
          dp[i][j] = max(dp[i][j], dp[i][j - 1]);
      }
    }
  }

  rep(i, 0, m) {
    rep(j, 0, n) {
      par[i][j] = {-1, -1};
      if (s[i] == t[j]) {
        if (i > 0 and j > 0 and dp[i][j] == 1 + dp[i - 1][j - 1])
          par[i][j] = {i - 1, j - 1};
        else
          par[i][j] = {-1, -1};
      } else {
        if (i > 0 and dp[i][j] == dp[i - 1][j])
          par[i][j] = {i - 1, j};
        if (j > 0 and dp[i][j] == dp[i][j - 1])
          par[i][j] = {i, j - 1};
      }
    }
  }
  int i = m - 1;
  int j = n - 1;
  string ans;
  while (pp(i, j) != pp(-1, -1)) {
    if (s[i] == t[j])
      ans.pb(s[i]);
    int t = par[i][j].first;
    j = par[i][j].second;
    i = t;
  }
  reverse(all(ans));
  cout << ans << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define vi vector<int>
#define all(x) (x).begin(), (x).end()
#define F first
#define S second
#define pb push_back
#define pp pair<int, int>
#define rep(i, l, r) for (int i = l; i < r; i++)
int main() {
  // #ifndef ONLINE_JUDGE
  // freopen("input.txt.txt","r",stdin);
  // freopen("output.txt.txt","w",stdout);
  // #endif
  ios::sync_with_stdio(false);
  cin.tie(0);
  string s, t;
  cin >> s >> t;
  int m = s.size();
  int n = t.size();
  int dp[m][n];
  pp par[m][n];
  rep(i, 0, m) {
    rep(j, 0, n) {
      dp[i][j] = 0;
      if (s[i] == t[j]) {
        if (i > 0 and j > 0)
          dp[i][j] = 1 + dp[i - 1][j - 1];
        else
          dp[i][j] = 1;
      } else {
        if (i > 0)
          dp[i][j] = max(dp[i][j], dp[i - 1][j]);
        if (j > 0)
          dp[i][j] = max(dp[i][j], dp[i][j - 1]);
      }
    }
  }

  rep(i, 0, m) {
    rep(j, 0, n) {
      par[i][j] = {-1, -1};
      if (s[i] == t[j]) {
        if (i > 0 and j > 0 and dp[i][j] == 1 + dp[i - 1][j - 1])
          par[i][j] = {i - 1, j - 1};
        else
          par[i][j] = {-1, -1};
      } else {
        if (i > 0 and dp[i][j] == dp[i - 1][j])
          par[i][j] = {i - 1, j};
        if (j > 0 and dp[i][j] == dp[i][j - 1])
          par[i][j] = {i, j - 1};
      }
    }
  }
  int i = m - 1;
  int j = n - 1;
  string ans;
  while (pp(i, j) != pp(-1, -1)) {
    if (s[i] == t[j])
      ans.pb(s[i]);
    int t = par[i][j].first;
    j = par[i][j].second;
    i = t;
  }
  reverse(all(ans));
  cout << ans << endl;
}
Problem p03165 (C++)

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

int dp[3010][3010];
int main() {
  int dp[1010][1010];
  string S, T;
  cin >> S;
  cin >> T;
  memset(dp, 0, sizeof(dp));
  for (int i = 0; i < S.size(); i++) {
    for (int j = 0; j < T.size(); j++) {
      if (S[i] == T[j])
        dp[i + 1][j + 1] = dp[i][j] + 1;
      dp[i + 1][j + 1] = max(dp[i + 1][j + 1], dp[i][j + 1]);
      dp[i + 1][j + 1] = max(dp[i + 1][j + 1], dp[i + 1][j]);
    }
  }
  string str = "";
  int i = S.size();
  int j = T.size();
  while (0 < i && 0 < j) {
    int x = dp[i][j];
    if (dp[i - 1][j] == x) {
      i--;
    } else if (dp[i][j - 1] == x) {
      j--;
    } else {
      str = S[i - 1] + str;
      i--;
      j--;
    }
  }
  cout << str << endl;
  return 0;
}


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

int dp[3010][3010];
int main() {
  string S, T;
  cin >> S;
  cin >> T;
  memset(dp, 0, sizeof(dp));
  for (int i = 0; i < S.size(); i++) {
    for (int j = 0; j < T.size(); j++) {
      if (S[i] == T[j])
        dp[i + 1][j + 1] = dp[i][j] + 1;
      dp[i + 1][j + 1] = max(dp[i + 1][j + 1], dp[i][j + 1]);
      dp[i + 1][j + 1] = max(dp[i + 1][j + 1], dp[i + 1][j]);
    }
  }
  string str = "";
  int i = S.size();
  int j = T.size();
  while (0 < i && 0 < j) {
    int x = dp[i][j];
    if (dp[i - 1][j] == x) {
      i--;
    } else if (dp[i][j - 1] == x) {
      j--;
    } else {
      str = S[i - 1] + str;
      i--;
      j--;
    }
  }
  cout << str << endl;
  return 0;
}
Problem p03165 (C++)

Buggy submission (status: Time Limit Exceeded):
#include "bits/stdc++.h"
using namespace std;
using ll = long long;
using ull = unsigned long long;
using vb = vector<bool>;
using vvb = vector<vb>;
using vi = vector<int>;
using vvi = vector<vi>;
using vl = vector<ll>;
using vvl = vector<vl>;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
#define FOR(i, a, b) for (ll i = (a); i < (ll)(b); ++i)
#define REP(i, n) FOR(i, 0, n)
#define RREP(i, n) for (ll i = (ll)(n)-1; i >= 0; --i)
#define ALL(obj) (obj).begin(), (obj).end()
#define rALL(obj) (obj).rbegin(), (obj).rend()
#define eb(val) emplace_back(val)
const double PI = acos(-1);
const double EPS = 1e-10;
const ll MOD = 1e9 + 7;
// #define int ll
void cioacc() { // accelerate cin/cout
  cin.tie(0);
  ios::sync_with_stdio(false);
}
signed main() {
  cioacc();
  string s, t;
  cin >> s >> t;
  ll ssz = (ll)s.size(), tsz = (ll)t.size();
  vector<vector<string>> dp(ssz + 1, vector<string>(tsz + 1));
  vector<string> last(tsz + 1);
  REP(i, ssz) {
    vector<string> now(tsz + 1);
    REP(j, tsz) {
      if (s[i] == t[j]) {
        now[j + 1] = last[j] + s[i];
        continue;
      }
      now[j + 1] = (now[j].size() >= last[j + 1].size() ? now[j] : last[j + 1]);
    }
    last = now;
  }
  cout << last[tsz] << endl;
}


Fixed submission:
#include "bits/stdc++.h"
using namespace std;
using ll = long long;
using ull = unsigned long long;
using vb = vector<bool>;
using vvb = vector<vb>;
using vi = vector<int>;
using vvi = vector<vi>;
using vl = vector<ll>;
using vvl = vector<vl>;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
#define FOR(i, a, b) for (ll i = (a); i < (ll)(b); ++i)
#define REP(i, n) FOR(i, 0, n)
#define RREP(i, n) for (ll i = (ll)(n)-1; i >= 0; --i)
#define ALL(obj) (obj).begin(), (obj).end()
#define rALL(obj) (obj).rbegin(), (obj).rend()
#define eb(val) emplace_back(val)
const double PI = acos(-1);
const double EPS = 1e-10;
const ll MOD = 1e9 + 7;
// #define int ll
void cioacc() { // accelerate cin/cout
  cin.tie(0);
  ios::sync_with_stdio(false);
}
signed main() {
  cioacc();
  string s, t;
  cin >> s >> t;
  ll ssz = (ll)s.size(), tsz = (ll)t.size();
  vector<vector<string>> dp(ssz + 1, vector<string>(tsz + 1));
  vector<string> last(tsz + 1);
  REP(i, ssz) {
    vector<string> now(tsz + 1);
    REP(j, tsz) {
      if (s[i] == t[j]) {
        now[j + 1] = last[j] + s[i];
        continue;
      }
      now[j + 1] = (now[j].size() >= last[j + 1].size() ? now[j] : last[j + 1]);
    }
    last = move(now);
  }
  cout << last[tsz] << endl;
}