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

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

string LCS(string s, string t) {

  int n = s.length();
  int m = t.length();

  // int dp[n+1][m+1];
  int **dp = new int *[n + 1];
  for (int i = 0; i <= n; i++) {
    dp[i] = new int[m + 1];
  }
  // base case
  for (int j = 0; j <= m; j++) {
    dp[n][j] = 0;
  }
  for (int i = 0; i <= n; i++) {
    dp[i][m] = 0;
  }

  // fill dp array
  for (int i = n - 1; i >= 0; i--) {
    for (int j = m - 1; j >= 0; 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]);
      }
    }
  } /*
   for(int i=0;i<=n;i++){
     for(int j=0;j<=m;j++){
       cout<<dp[i][j]<<" ";
     }
     cout<<endl;
   }*/
  // cout<<dp[0][0];
  string ans = "";
  // now backtrack the dp array to find the string
  int i = 0, j = 0;
  while (i <= n && j <= m) {

    if (s[i] == t[j]) {
      ans += s[i];
      i++;
      j++;
    } else if (dp[i + 1][j] < dp[i][j + 1]) {
      j++;
    } else {
      i++;
    }
  }

  return ans;
}

int main() {

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

  string ans = LCS(s, t);
  cout << ans << endl;
  return 0;
}


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

string LCS(string s, string t) {

  int n = s.length();
  int m = t.length();

  // int dp[n+1][m+1];
  int **dp = new int *[n + 1];
  for (int i = 0; i <= n; i++) {
    dp[i] = new int[m + 1];
  }
  // base case
  for (int j = 0; j <= m; j++) {
    dp[n][j] = 0;
  }
  for (int i = 0; i <= n; i++) {
    dp[i][m] = 0;
  }

  // fill dp array
  for (int i = n - 1; i >= 0; i--) {
    for (int j = m - 1; j >= 0; 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]);
      }
    }
  } /*
   for(int i=0;i<=n;i++){
     for(int j=0;j<=m;j++){
       cout<<dp[i][j]<<" ";
     }
     cout<<endl;
   }*/
  // cout<<dp[0][0];
  string ans = "";
  // now backtrack the dp array to find the string
  int i = 0, j = 0;
  while (i < n && j < m) {

    if (s[i] == t[j]) {
      ans += s[i];
      i++;
      j++;
    } else if (dp[i + 1][j] < dp[i][j + 1]) {
      j++;
    } else {
      i++;
    }
  }

  return ans;
}

int main() {

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

  string ans = LCS(s, t);
  cout << ans << endl;
  return 0;
}
Problem p03165 (C++)

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

string S, T;
int dp[1010][1010];

int main() {

  cin >> S >> T;
  int n = (int)S.size();
  int m = (int)T.size();

  // LCSの長さを求める
  dp[0][0] = 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] = max(dp[i + 1][j + 1], dp[i][j] + 1);
      }
      dp[i + 1][j + 1] = max(dp[i + 1][j + 1], dp[i + 1][j]);
      dp[i + 1][j + 1] = max(dp[i + 1][j + 1], dp[i][j + 1]);
    }
  }

  // dpテーブルからLCSを復元
  string ans = "";
  int left_chars = dp[n][m]; // あと何文字残っているか
  for (int i = n; i > 0; i--) {
    for (int j = m; j > 0; j--) {
      if (dp[i - 1][j] == dp[i][j - 1] && dp[i][j - 1] == dp[i - 1][j - 1] &&
          dp[i - 1][j - 1] != dp[i][j] && dp[i][j] == left_chars) {
        ans = (char)S[i - 1] + ans;
        left_chars--;
        break; // 同じ箇所の文字は1回だけ使用可能
      }
    }
  }
  cout << ans << endl;

  return 0;
}

Fixed submission:
#include <algorithm>
#include <cstdio>
#include <iostream>
using namespace std;

string S, T;
int dp[3010][3010];

int main() {

  cin >> S >> T;
  int n = (int)S.size();
  int m = (int)T.size();

  // LCSの長さを求める
  dp[0][0] = 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] = max(dp[i + 1][j + 1], dp[i][j] + 1);
      }
      dp[i + 1][j + 1] = max(dp[i + 1][j + 1], dp[i + 1][j]);
      dp[i + 1][j + 1] = max(dp[i + 1][j + 1], dp[i][j + 1]);
    }
  }

  // dpテーブルからLCSを復元
  string ans = "";
  int left_chars = dp[n][m]; // あと何文字残っているか
  for (int i = n; i > 0; i--) {
    for (int j = m; j > 0; j--) {
      if (dp[i - 1][j] == dp[i][j - 1] && dp[i][j - 1] == dp[i - 1][j - 1] &&
          dp[i - 1][j - 1] != dp[i][j] && dp[i][j] == left_chars) {
        ans = (char)S[i - 1] + ans;
        left_chars--;
        break; // 同じ箇所の文字は1回だけ使用可能
      }
    }
  }
  cout << ans << endl;

  return 0;
}
Problem p03165 (C++)

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

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

Buggy submission (status: Runtime Error):
//
// Created by 13783 on 2020/4/2.
//
#include <iostream>
using namespace std;
string s, t;
const int len = 3e3;
int dp[len][len];
int n;
void solve() {
  for (int i = 0; i < s.length(); i++) {
    for (int j = 0; j < t.length(); 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 ans;
  int i = s.length(), j = t.length();
  while (i > 0 && j > 0) {
    if (dp[i][j] == dp[i - 1][j]) {
      i--;
    } else if (dp[i][j - 1] == dp[i][j]) {
      j--;
    } else {
      i--;
      j--;
      ans = s[i] + ans;
    }
  }
  cout << ans;
}
int main() {
  cin >> s >> t;
  solve();
  return 0;
}

Fixed submission:
//
// Created by 13783 on 2020/4/2.
//
#include <iostream>
using namespace std;
string s, t;
const int len = 3e3 + 2;
int dp[len][len];
int n;
void solve() {
  for (int i = 0; i < s.length(); i++) {
    for (int j = 0; j < t.length(); 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 ans;
  int i = s.length(), j = t.length();
  while (i > 0 && j > 0) {
    if (dp[i][j] == dp[i - 1][j]) {
      i--;
    } else if (dp[i][j - 1] == dp[i][j]) {
      j--;
    } else {
      i--;
      j--;
      ans = s[i] + ans;
    }
  }
  cout << ans;
}
int main() {
  cin >> s >> t;
  solve();
  return 0;
}
Problem p03165 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#include <vector>
using namespace std;
int main(void) {
  // Your code here!
  // int dp[3001][3001] = {{}};
  vector<vector<int>> dp(3001, vector<int>(3001, 0));
  string S, T;
  cin >> S;
  cin >> T;

  for (int i = 1; i < S.size() + 1; i++) {
    for (int j = 1; j < T.size() + 1; j++) {
      dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]);
      if (S[i - 1] == T[j - 1])
        dp[i][j] = max(dp[i][j], dp[i - 1][j - 1] + 1);
    }
  }

  if (dp[S.size()][T.size()] > 0) {
    int i = S.size();
    int j = T.size();
    char output[3001];
    long long count_output = 0;
    while (i > 0) {
      while (j > 0) {
        if (dp[i][j] == dp[i - 1][j - 1] + 1 && dp[i][j] == dp[i - 1][j] + 1 &&
            dp[i][j] == dp[i][j - 1] + 1) {
          output[dp[S.size()][T.size()] - count_output - 1] = S[i - 1];
          count_output++;
          i--;
          j--;
        } else if (dp[i][j] == dp[i - 1][j])
          i--;
        else if (dp[i][j] == dp[i][j - 1])
          j--;
      }
    }

    for (int i = 0; i < count_output; i++)
      cout << output[i];
  } else
    cout << ' ';
}


Fixed submission:
#include <iostream>
#include <vector>
using namespace std;
int main(void) {
  // Your code here!
  // int dp[3001][3001] = {{}};
  vector<vector<int>> dp(3001, vector<int>(3001, 0));
  string S, T;
  cin >> S;
  cin >> T;

  for (int i = 1; i < S.size() + 1; i++) {
    for (int j = 1; j < T.size() + 1; j++) {
      dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]);
      if (S[i - 1] == T[j - 1])
        dp[i][j] = max(dp[i][j], dp[i - 1][j - 1] + 1);
    }
  }

  if (dp[S.size()][T.size()] > 0) {
    int i = S.size();
    int j = T.size();
    char output[3001];
    long long count_output = 0;
    long long count = 0;
    while (true) {
      if (i == 0 and j == 0)
        break;

      if (i > 0 && j > 0 && dp[i][j] == dp[i - 1][j - 1]) {
        i--;
        j--;
      } else if (i > 0 && dp[i][j] == dp[i - 1][j])
        i--;
      else if (j > 0 && dp[i][j] == dp[i][j - 1])
        j--;
      else {
        output[dp[S.size()][T.size()] - count_output - 1] = S[i - 1];
        count_output++;
        i--;
        j--;
      }
    }

    for (int i = 0; i < count_output; i++)
      cout << output[i];
  } else
    cout << ' ';
}
Problem p03165 (C++)

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

using namespace std;

const int MAXN = 3005;

int dp[MAXN][MAXN];

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

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

  int len_s = (int)s.size();
  int len_t = (int)t.size();

  memset(dp, 0, sizeof dp);

  for (int i = 1; i <= len_s; i++) {
    for (int j = 1; j <= len_t; j++) {
      if (s[i - 1] == t[j - 1])
        dp[i][j] = 1 + dp[i - 1][j - 1];
      else
        dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]);
    }
  }

  int i = len_s, j = len_t;
  string ans = "";

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

  reverse(ans.begin(), ans.end());
  cout << ans << endl;
  return 0;
}


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

using namespace std;

const int MAXN = 3005;

int dp[MAXN][MAXN];

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

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

  int len_s = (int)s.size();
  int len_t = (int)t.size();

  memset(dp, 0, sizeof dp);

  for (int i = 1; i <= len_s; i++) {
    for (int j = 1; j <= len_t; j++) {
      if (s[i - 1] == t[j - 1])
        dp[i][j] = 1 + dp[i - 1][j - 1];
      else
        dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]);
    }
  }

  int i = len_s, j = len_t;
  string ans = "";

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

  reverse(ans.begin(), ans.end());
  cout << ans << endl;
  return 0;
}
Problem p03165 (C++)

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

using namespace std;

#define ll long long

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  string s, t;
  int slen, tlen, dp[305][305] = {0};
  cin >> s;
  cin >> t;
  slen = s.length();
  tlen = t.length();
  s = " " + s;
  t = " " + t;
  for (int i = 1; i <= slen; i++) {
    for (int j = 1; j <= tlen; 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]);
      }
    }
  }
  // cout<<"  ";
  // for(auto x:t){
  // 	cout<<x<<" ";
  // }
  // cout<<endl;
  // for(int i=0;i<=slen; i++){
  // 	cout<<s[i]<<" ";
  // 	for(int j=0; j<=tlen; j++){
  // 		cout<<dp[i][j]<<" ";
  // 	}
  // 	cout<<endl;
  // }
  vector<char> ans;
  int i = slen, j = tlen;
  while (i > 0 && j > 0) {
    while (i > 0 && dp[i - 1][j] == dp[i][j]) {
      i--;
    }
    while (j > 0 && dp[i][j - 1] == dp[i][j]) {
      j--;
    }

    ans.push_back(s[i]);
    i--;
    j--;
  }
  reverse(ans.begin(), ans.end());
  for (auto x : ans) {
    cout << x;
  }
}

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

using namespace std;

#define ll long long

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  string s, t;
  int slen, tlen, dp[3005][3005] = {0};
  cin >> s;
  cin >> t;
  slen = s.length();
  tlen = t.length();
  s = " " + s;
  t = " " + t;
  for (int i = 1; i <= slen; i++) {
    for (int j = 1; j <= tlen; 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]);
      }
    }
  }
  // cout<<"  ";
  // for(auto x:t){
  // 	cout<<x<<" ";
  // }
  // cout<<endl;
  // for(int i=0;i<=slen; i++){
  // 	cout<<s[i]<<" ";
  // 	for(int j=0; j<=tlen; j++){
  // 		cout<<dp[i][j]<<" ";
  // 	}
  // 	cout<<endl;
  // }
  vector<char> ans;
  int i = slen, j = tlen;
  while (i > 0 && j > 0) {
    while (i > 0 && dp[i - 1][j] == dp[i][j]) {
      i--;
    }
    while (j > 0 && dp[i][j - 1] == dp[i][j]) {
      j--;
    }

    ans.push_back(s[i]);
    i--;
    j--;
  }
  reverse(ans.begin(), ans.end());
  for (auto x : ans) {
    cout << x;
  }
}
Problem p03165 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
int dp[3001][3001], i, j;
string s, t, ans;
int main() {
  cin >> s >> t;
  s += '$';
  t += '%';
  for (; i < s.size(); i++) {
    for (j = 0; j < t.size(); 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 (s[i] == t[j])
        dp[i + 1][j + 1] = max(dp[i + 1][j + 1], dp[i][j] + 1);
    }
  }
  for (i = s.size() - 1, j = t.size() - 1; i + j;) {
    if (i && dp[i - 1][j] == dp[i][j])
      i--;
    else if (j && dp[i][j - 1] == dp[i][j])
      j--;
    else
      i--, j--, ans += s[i];
  }
  reverse(ans.begin(), ans.end());
  cout << ans << endl;
  return 0;
}

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

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

using namespace std;
using ll = long long;

string s;
string t;

pair<int, int> nexty[3005][3005];
int dp[3005][3005];

int solve(int i, int j) {
  if (i == s.size() || j == t.size()) {
    return 0;
  }
  int &res = dp[i][j];
  if (res != -1) {
    return res;
  }
  if (s[i] == t[j]) {
    nexty[i][j] = {i + 1, j + 1};
    return res = 1 + solve(i + 1, j + 1);
  }
  int ss = solve(i + 1, j);
  int tt = solve(i, j + 1);
  if (ss > tt) {
    nexty[i][j] = {i + 1, j};
  } else {
    nexty[i][j] = {i, j + 1};
  }
  return max(ss, tt);
}

int main() {
  cin >> s >> t;
  fill(&dp[0][0], &dp[0][0] + 3005 * 3005, -1);
  int len = solve(0, 0);
  vector<char> res;
  auto pp = make_pair(0, 0);
  set<pair<int, int>> seen;
  while (pp.first != s.size() && pp.second != t.size()) {
    assert(!seen.count(pp));
    seen.insert(pp);
    if (s[pp.first] == t[pp.second]) {
      res.push_back(s[pp.first]);
    }
    pp = nexty[pp.first][pp.second];
  }
  for (char c : res) {
    cout << c;
  }
  cout << '\n';
  return 0;
}


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

using namespace std;
using ll = long long;

string s;
string t;

pair<int, int> nexty[3005][3005];
int dp[3005][3005];

int solve(int i, int j) {
  if (i == s.size() || j == t.size()) {
    return 0;
  }
  int &res = dp[i][j];
  if (res != -1) {
    return res;
  }
  if (s[i] == t[j]) {
    nexty[i][j] = {i + 1, j + 1};
    return res = 1 + solve(i + 1, j + 1);
  }
  int ss = solve(i + 1, j);
  int tt = solve(i, j + 1);
  if (ss > tt) {
    nexty[i][j] = {i + 1, j};
  } else {
    nexty[i][j] = {i, j + 1};
  }
  return res = max(ss, tt);
}

int main() {
  cin >> s >> t;
  fill(&dp[0][0], &dp[0][0] + 3005 * 3005, -1);
  int len = solve(0, 0);
  vector<char> res;
  auto pp = make_pair(0, 0);
  set<pair<int, int>> seen;
  while (pp.first != s.size() && pp.second != t.size()) {
    assert(!seen.count(pp));
    seen.insert(pp);
    if (s[pp.first] == t[pp.second]) {
      res.push_back(s[pp.first]);
    }
    pp = nexty[pp.first][pp.second];
  }
  for (char c : res) {
    cout << c;
  }
  cout << '\n';
  return 0;
}
Problem p03165 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define nl "\n"
#define pb push_back
#define E esit(0)
#define all(v) v.begin(), v.end()
using namespace std;
using ll = long long;
const int N = 1e5 + 5;
const int INF = 1e9 + 7;
int dp[1001][1001];
string s, s1, ans;
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cin >> s >> s1;
  ll n = s.size(), m = s1.size();
  s = " " + s;
  s1 = " " + s1;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      if (s[i] == s1[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) {
    if (s[n] == s1[m]) {
      ans += s[n];
      n--;
      m--;
    } else {
      if (dp[n - 1][m] > dp[n][m - 1])
        n--;
      else
        m--;
    }
  }
  for (int i = ans.size() - 1; i >= 0; i--) {
    cout << ans[i];
  }
}

Fixed submission:
#include <bits/stdc++.h>
#define nl "\n"
#define pb push_back
#define E esit(0)
#define all(v) v.begin(), v.end()
using namespace std;
using ll = long long;
const int N = 1e5 + 5;
const int INF = 1e9 + 7;
int dp[3001][3001];
string s, s1, ans;
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cin >> s >> s1;
  ll n = s.size(), m = s1.size();
  s = " " + s;
  s1 = " " + s1;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      if (s[i] == s1[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) {
    if (s[n] == s1[m]) {
      ans += s[n];
      n--;
      m--;
    } else {
      if (dp[n - 1][m] > dp[n][m - 1])
        n--;
      else
        m--;
    }
  }
  for (int i = ans.size() - 1; i >= 0; i--) {
    cout << ans[i];
  }
}
Problem p03165 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define ll long long
#define ld long double
#define vect vector<ll>
#define unmap unordered_map
#define pb push_back
#define pi 3.1415926536
#define mod int(1e9 + 7)
#define mp make_pair
#define testcases                                                              \
  ll t;                                                                        \
  cin >> t;                                                                    \
  while (t--)

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

  string s, t;
  cin >> s >> t;
  int n = s.length(), m = t.length();
  s = "*" + s;
  t = "*" + t;
  vector<vector<int>> dp(1001, vector<int>(1001, 0));
  vector<vector<int>> v(1001, vector<int>(1001, 0));

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


Fixed submission:
#include <bits/stdc++.h>
#define ll long long
#define ld long double
#define vect vector<ll>
#define unmap unordered_map
#define pb push_back
#define pi 3.1415926536
#define mod int(1e9 + 7)
#define mp make_pair
#define testcases                                                              \
  ll t;                                                                        \
  cin >> t;                                                                    \
  while (t--)

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

  string s, t;
  cin >> s >> t;
  int n = s.length(), m = t.length();
  s = "*" + s;
  t = "*" + t;
  vector<vector<int>> dp(3001, vector<int>(3001, 0));
  vector<vector<int>> v(3001, vector<int>(3001, 0));

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

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

using namespace std;

const int MSIZE = 3003;

int dp[MSIZE][MSIZE];

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  string s, t;
  cin >> s >> t;
  memset(dp, 0, sizeof dp);

  int n = s.size();
  int m = t.size();

  s = " " + s;
  t = " " + t;

  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      dp[i][j] =
          max({dp[i - 1][j], dp[i][j - 1], dp[i - 1][j - 1] + (s[i] == t[j])});
    }
  }

  int x = n;
  int y = m;

  string answer = "";

  if (dp[n][m] == 0) {
    return 0;
  }

  while (x && y) {
    while (x && dp[x - 1][y] == dp[x][y])
      x--;
    while (y && dp[x][y - 1] == dp[x][y])
      y--;
    answer.push_back(s[x]);
    x--, y--;
  }
  reverse(answer.begin(), answer.end());
  cout << answer << endl;
}

Fixed submission:
#include "bits/stdc++.h"

using namespace std;

const int MSIZE = 3003;

int dp[MSIZE][MSIZE];

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  string s, t;
  cin >> s >> t;
  memset(dp, 0, sizeof dp);

  int n = s.size();
  int m = t.size();

  s = " " + s;
  t = " " + t;

  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      dp[i][j] =
          max({dp[i - 1][j], dp[i][j - 1], dp[i - 1][j - 1] + (s[i] == t[j])});
    }
  }

  int x = n;
  int y = m;

  string answer = "";

  while (dp[x][y]) {
    while (x && dp[x - 1][y] == dp[x][y])
      x--;
    while (y && dp[x][y - 1] == dp[x][y])
      y--;
    answer.push_back(s[x]);
    x--, y--;
  }
  reverse(answer.begin(), answer.end());
  cout << answer << endl;
}
Problem p03165 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define FOR(i, o, n) for (long long i = o; i < n; i++)
#define oneforall                                                              \
  ios::sync_with_stdio(false);                                                 \
  cin.tie(0);
typedef vector<int> vi;
typedef vector<long long> vl;
typedef long long ll;
typedef vector<pair<long, long>> vpll;
typedef vector<pair<int, int>> vpii;
int main() {
  oneforall char a[10000], b[10000];
  cin >> a + 1 >> b + 1;
  ll asize = 0;
  ll bsize = 0;
  while (a[asize + 1]) {
    asize++;
  }
  while (b[bsize + 1]) {
    bsize++;
  }
  ll dp[10000010][10000010];
  FOR(i, 0, 10000010) FOR(j, 0, 10000010) { dp[i][j] = 0; }
  FOR(i, 1, asize + 1) {
    FOR(j, 1, bsize + 1) {
      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]);
      }
    }
  }
  string x;
  while (asize and bsize) {
    if (a[asize] == b[bsize]) {
      x += a[asize];
      asize--;
      bsize--;
    } else if (dp[asize - 1][bsize] > dp[asize][bsize - 1])
      asize--;
    else
      bsize--;
  }
  reverse(x.begin(), x.end());
  cout << x;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define FOR(i, o, n) for (long long i = o; i < n; i++)
#define oneforall                                                              \
  ios::sync_with_stdio(false);                                                 \
  cin.tie(0);
typedef vector<int> vi;
typedef vector<long long> vl;
typedef long long ll;
typedef vector<pair<long, long>> vpll;
typedef vector<pair<int, int>> vpii;
int main() {
  oneforall char a[10000], b[10000];
  cin >> a + 1 >> b + 1;
  ll asize = 0;
  ll bsize = 0;
  while (a[asize + 1]) {
    asize++;
  }
  while (b[bsize + 1]) {
    bsize++;
  }
  ll dp[3001][3001];
  FOR(i, 0, 3001) FOR(j, 0, 3001) { dp[i][j] = 0; }
  FOR(i, 1, asize + 1) {
    FOR(j, 1, bsize + 1) {
      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]);
      }
    }
  }
  string x;
  while (asize and bsize) {
    if (a[asize] == b[bsize]) {
      x += a[asize];
      asize--;
      bsize--;
    } else if (dp[asize - 1][bsize] > dp[asize][bsize - 1])
      asize--;
    else
      bsize--;
  }
  reverse(x.begin(), x.end());
  cout << x;
  return 0;
}
Problem p03165 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
const int MAX = 1e3 + 10;
string A, B;
ll N, M;
ll dp[MAX][MAX];
int solve(int i, int j) {
  if (i >= N || j >= M)
    return 0;
  if (dp[i][j] != -1)
    return dp[i][j];
  int ans = 0;
  if (A[i] == B[j]) {
    ans = 1 + solve(i + 1, j + 1);
  } else {
    ans = max(solve(i + 1, j), solve(i, j + 1));
  }
  return dp[i][j] = ans;
}
void path(int i, int j) {
  if (i >= N || j >= M)
    return;
  if (A[i] == B[j]) {
    cout << A[i];
    path(i + 1, j + 1);
  } else {
    if (solve(i + 1, j) == dp[i][j]) {
      path(i + 1, j);
    } else if (solve(i, j + 1) == dp[i][j]) {
      path(i, j + 1);
    }
  }
}
int main() {
  memset(dp, -1, sizeof dp);
  cin >> A >> B;
  N = A.size();
  M = B.size();
  solve(0, 0);
  path(0, 0);
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
const int MAX = 3e3 + 10;
string A, B;
ll N, M;
ll dp[MAX][MAX];
int solve(int i, int j) {
  if (i >= N || j >= M)
    return 0;
  if (dp[i][j] != -1)
    return dp[i][j];
  int ans = 0;
  if (A[i] == B[j]) {
    ans = 1 + solve(i + 1, j + 1);
  } else {
    ans = max(solve(i + 1, j), solve(i, j + 1));
  }
  return dp[i][j] = ans;
}
void path(int i, int j) {
  if (i >= N || j >= M)
    return;
  if (A[i] == B[j]) {
    cout << A[i];
    path(i + 1, j + 1);
  } else {
    if (solve(i + 1, j) == dp[i][j]) {
      path(i + 1, j);
    } else if (solve(i, j + 1) == dp[i][j]) {
      path(i, j + 1);
    }
  }
}
int main() {
  memset(dp, -1, sizeof dp);
  cin >> A >> B;
  N = A.size();
  M = B.size();
  solve(0, 0);
  path(0, 0);
  return 0;
}
Problem p03165 (C++)

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

using namespace std;

const int MAX_N = 1e3 + 5;

int n, m;

int dp[MAX_N][MAX_N];

char s[MAX_N], t[MAX_N], ans[MAX_N];

int main() {
  // dp[i][j] = maximum lcs using first i chars from s and first j chars from t
  // if s[i] == t[j], then there is a match, so, dp[i][j] = 1 + dp[i - 1][j - 1]
  // (we take the two chars) Else, there is no match, so we take the best option
  // from dp[i - 1][j], dp[i][j - 1];
  int l;
  cin >> (s + 1) >> (t + 1);
  n = strlen(s + 1);
  m = strlen(t + 1);
  for (int i = 1; i <= n; ++i) {
    for (int j = 1; j <= m; ++j) {
      if (s[i] == t[j]) {
        dp[i][j] = 1 + dp[i - 1][j - 1];
      } else {
        dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]);
      }
    }
  }
  l = 0;
  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[l++] = s[1 + n];
    }
  }
  reverse(ans, ans + l);
  cout << ans;
  return 0;
}


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

using namespace std;

const int MAX_N = 3e3 + 5;

int n, m;

int dp[MAX_N][MAX_N];

char s[MAX_N], t[MAX_N], ans[MAX_N];

int main() {
  // dp[i][j] = maximum lcs using first i chars from s and first j chars from t
  // if s[i] == t[j], then there is a match, so, dp[i][j] = 1 + dp[i - 1][j - 1]
  // (we take the two chars) Else, there is no match, so we take the best option
  // from dp[i - 1][j], dp[i][j - 1];
  int l;
  cin >> (s + 1) >> (t + 1);
  n = strlen(s + 1);
  m = strlen(t + 1);
  for (int i = 1; i <= n; ++i) {
    for (int j = 1; j <= m; ++j) {
      if (s[i] == t[j]) {
        dp[i][j] = 1 + dp[i - 1][j - 1];
      } else {
        dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]);
      }
    }
  }
  l = 0;
  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[l++] = s[1 + n];
    }
  }
  reverse(ans, ans + l);
  cout << ans;
  return 0;
}
Problem p03165 (C++)

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

using namespace std;

const int MAX_LENGTH = 3012;
string s, t, dp[MAX_LENGTH][MAX_LENGTH];

int main() {

  cin >> s >> t;

  int s_length = s.size(), t_length = t.size();

  if (s[0] == t[0])
    dp[0][0] = s[0];

  for (int i = 1; i < t_length; ++i) {

    if (t[i] == s[0] && dp[0][i - 1].empty())
      dp[0][i] = s[0];

    else
      dp[0][i] = dp[0][i - 1];
  }

  int tst = 0;

  for (int i = 1; i < s_length; ++i) {

    if (tst % 2 == 0) {
      if (s[i] == t[0] && dp[0][0].size() == 0)
        dp[1][0] = s[i];

      else
        dp[1][0] = dp[0][0];
    }

    else {
      if (s[i] == t[0] && dp[1][0].size() == 0)
        dp[0][0] = s[i];

      else
        dp[0][0] = dp[1][0];
    }

    for (int j = 1; j < t_length; ++j) {

      if (tst % 2 == 0) {

        if (s[i] == t[j])
          dp[1][j] = dp[0][j - 1] + s[i];

        else if (dp[1][j - 1].size() > dp[0][j].size())
          dp[1][j] = dp[1][j - 1];

        else
          dp[1][j] = dp[0][j];
      } else {

        if (s[i] == t[j])
          dp[0][j] = dp[1][j - 1] + s[i];

        else if (dp[0][j - 1].size() > dp[1][j].size())
          dp[0][j] = dp[0][j - 1];

        else
          dp[0][j] = dp[1][j];
      }
    }
    tst++;
  }

  if (dp[0][t_length - 1].size() > dp[1][t_length - 1].size())
    cout << dp[0][t_length - 1] << endl;

  else
    cout << dp[1][t_length - 1] << endl;
  return 0;
}

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

using namespace std;

const int MAX_LENGTH = 3012;
string s, t, dp[2][MAX_LENGTH];

int main() {

  cin >> s >> t;

  int s_length = s.size(), t_length = t.size();

  if (s[0] == t[0])
    dp[0][0] = s[0];

  for (int i = 1; i < t_length; ++i) {

    if (t[i] == s[0] && dp[0][i - 1].empty())
      dp[0][i] = s[0];

    else
      dp[0][i] = dp[0][i - 1];
  }

  int tst = 0;

  for (int i = 1; i < s_length; ++i) {

    if (tst % 2 == 0) {
      if (s[i] == t[0] && dp[0][0].size() == 0)
        dp[1][0] = s[i];

      else
        dp[1][0] = dp[0][0];
    }

    else {
      if (s[i] == t[0] && dp[1][0].size() == 0)
        dp[0][0] = s[i];

      else
        dp[0][0] = dp[1][0];
    }

    for (int j = 1; j < t_length; ++j) {

      if (tst % 2 == 0) {

        if (s[i] == t[j])
          dp[1][j] = dp[0][j - 1] + s[i];

        else if (dp[1][j - 1].size() > dp[0][j].size())
          dp[1][j] = dp[1][j - 1];

        else
          dp[1][j] = dp[0][j];
      } else {

        if (s[i] == t[j])
          dp[0][j] = dp[1][j - 1] + s[i];

        else if (dp[0][j - 1].size() > dp[1][j].size())
          dp[0][j] = dp[0][j - 1];

        else
          dp[0][j] = dp[1][j];
      }
    }
    tst++;
  }

  if (dp[0][t_length - 1].size() > dp[1][t_length - 1].size())
    cout << dp[0][t_length - 1] << endl;

  else
    cout << dp[1][t_length - 1] << endl;
  return 0;
}
Problem p03165 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define fast                                                                   \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);                                                               \
  cout.tie(NULL);
#define ll long long
using namespace std;
string a, b;
const int MAXN = 3005;
ll dp[MAXN][MAXN];
int n, m;
string ans;
ll solve(int i, int j) {
  if (i == n)
    return 0;
  if (j == m)
    return 0;
  ll r = dp[i][j];
  if (r != -1)
    return r;
  if (a[i] == b[j])
    r = solve(i + 1, j + 1) + 1;
  else
    r = max(solve(i + 1, j), solve(i, j + 1));
  return r;
}
void build(int i, int j) {
  if (i == n)
    return;
  if (j == m)
    return;
  ll answer = solve(i, j);
  if (a[i] == b[j] && answer == solve(i + 1, j + 1) + 1) {
    ans.push_back(a[i]);
    build(i + 1, j + 1);
  } else if (answer == solve(i + 1, j))
    build(i + 1, j);
  else if (answer == solve(i, j + 1))
    build(i, j + 1);
  return;
}
int main() {
  fast;
  cin >> a >> b;
  n = a.size();
  m = b.size();
  memset(dp, -1, sizeof dp);
  solve(0, 0);
  build(0, 0);
  cout << ans << "\n";
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define fast                                                                   \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);                                                               \
  cout.tie(NULL);
#define ll long long
using namespace std;
string a, b;
const int MAXN = 3005;
ll dp[MAXN][MAXN];
int n, m;
string ans;
ll solve(int i, int j) {
  if (i == n)
    return 0;
  if (j == m)
    return 0;
  ll r = dp[i][j];
  if (r != -1)
    return r;
  if (a[i] == b[j])
    r = solve(i + 1, j + 1) + 1;
  else
    r = max(solve(i + 1, j), solve(i, j + 1));
  return dp[i][j] = r;
}
void build(int i, int j) {
  if (i == n)
    return;
  if (j == m)
    return;
  ll answer = solve(i, j);
  if (a[i] == b[j] && answer == solve(i + 1, j + 1) + 1) {
    ans.push_back(a[i]);
    build(i + 1, j + 1);
  } else if (answer == solve(i + 1, j))
    build(i + 1, j);
  else if (answer == solve(i, j + 1))
    build(i, j + 1);
  return;
}
int main() {
  fast;
  cin >> a >> b;
  n = a.size();
  m = b.size();
  memset(dp, -1, sizeof dp);
  solve(0, 0);
  build(0, 0);
  cout << ans << "\n";
  return 0;
}
Problem p03165 (C++)

Buggy submission (status: Runtime Error):
//
// Created by Pulak on 18-11-2019.
//
#include <bits/stdc++.h>
#include <iostream>
using namespace std;

int main() {
  string a, b;
  cin >> a >> b;
  int s1 = a.size();
  int s2 = b.size();
  vector<vector<int>> dp(5000, vector<int>(5000));
  for (int i = 0; i <= s1; ++i) {
    for (int j = 0; j <= s2; ++j) {
      if (i == 0 || j == 0)
        dp[i][j] = 0;
      else if (a[i - 1] == b[j - 1]) {
        dp[i][j] = 1 + dp[i - 1][j - 1];
      } else {
        dp[i][j] = max(dp[i][j - 1], dp[i - 1][j]);
      }
    }
  }
  string ans;
  int k = s1, j = s2;
  while (k > 0 || j > 0) {
    if (a[k - 1] == b[j - 1]) {
      ans += a[k - 1];
      k--;
      j--;
    } else {
      if (dp[k - 1][j] > dp[k][j - 1])
        k--;
      else
        j--;
    }
  }

  reverse(ans.begin(), ans.end());
  cout << ans;
}


Fixed submission:
//
// Created by Pulak on 18-11-2019.
//
#include <bits/stdc++.h>
#include <iostream>
using namespace std;

int main() {
  string a, b;
  cin >> a >> b;
  int s1 = a.size();
  int s2 = b.size();
  vector<vector<int>> dp(5000, vector<int>(5000));
  for (int i = 0; i <= s1; ++i) {
    for (int j = 0; j <= s2; ++j) {
      if (i == 0 || j == 0)
        dp[i][j] = 0;
      else if (a[i - 1] == b[j - 1]) {
        dp[i][j] = 1 + dp[i - 1][j - 1];
      } else {
        dp[i][j] = max(dp[i][j - 1], dp[i - 1][j]);
      }
    }
  }
  string ans;
  int k = s1, j = s2;
  while (k > 0 && j > 0) {
    if (a[k - 1] == b[j - 1]) {
      ans += a[k - 1];
      k--;
      j--;
    } else {
      if (dp[k - 1][j] > dp[k][j - 1])
        k--;
      else
        j--;
    }
  }

  reverse(ans.begin(), ans.end());
  cout << ans;
}
Problem p03165 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll mod = 1000000007;
const ll INF = mod * mod;
#define stop                                                                   \
  char nyaa;                                                                   \
  cin >> nyaa;
#define rep(i, n) for (int i = 0; i < n; i++)
#define per(i, n) for (int i = n - 1; i >= 0; i--)
#define Rep(i, sta, n) for (int i = sta; i < n; i++)
#define rep1(i, n) for (int i = 1; i <= n; i++)
#define per1(i, n) for (int i = n; i >= 1; i--)
#define Rep1(i, sta, n) for (int i = sta; i <= n; i++)

int main() {
  string s, t;
  cin >> s >> t;
  int n = s.size(), m = t.size();
  int dp[1010][1010];

  rep(i, n + 1) {
    rep(j, m + 1) {
      if (s[i] == t[j])
        dp[i + 1][j + 1] = max(dp[i + 1][j + 1], dp[i][j] + 1);
      dp[i + 1][j + 1] = max(dp[i + 1][j + 1], dp[i + 1][j]);
      dp[i + 1][j + 1] = max(dp[i + 1][j + 1], dp[i][j + 1]);
    }
  }

  string res = "";
  int i = n, j = m;
  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 {
      res = s[i - 1] + res;
      i--;
      j--;
    }
  }

  cout << res << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll mod = 1000000007;
const ll INF = mod * mod;
#define stop                                                                   \
  char nyaa;                                                                   \
  cin >> nyaa;
#define rep(i, n) for (int i = 0; i < n; i++)
#define per(i, n) for (int i = n - 1; i >= 0; i--)
#define Rep(i, sta, n) for (int i = sta; i < n; i++)
#define rep1(i, n) for (int i = 1; i <= n; i++)
#define per1(i, n) for (int i = n; i >= 1; i--)
#define Rep1(i, sta, n) for (int i = sta; i <= n; i++)

int main() {
  string s, t;
  cin >> s >> t;
  int n = s.size(), m = t.size();
  int dp[3010][3010];

  rep(i, n + 1) {
    rep(j, m + 1) {
      if (s[i] == t[j])
        dp[i + 1][j + 1] = max(dp[i + 1][j + 1], dp[i][j] + 1);
      dp[i + 1][j + 1] = max(dp[i + 1][j + 1], dp[i + 1][j]);
      dp[i + 1][j + 1] = max(dp[i + 1][j + 1], dp[i][j + 1]);
    }
  }

  string res = "";
  int i = n, j = m;
  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 {
      res = s[i - 1] + res;
      i--;
      j--;
    }
  }

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

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

using namespace std;

typedef long long ll;

int dp[3000][3000];
string li, ne;

int f(int i, int j) {
  if (i == li.size() || j == ne.size())
    return 0;
  if (dp[i][j] != -1)
    return dp[i][j];
  if (li[i] == ne[j])
    return dp[i][j] = 1 + f(i + 1, j + 1);
  return dp[i][j] = max(f(i + 1, j), f(i, j + 1));
}

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

  memset(dp, -1, sizeof dp);
  cin >> li >> ne;
  f(0, 0);
  int i = 0, j = 0;
  while (dp[i][j] > 0) {
    if (li[i] == ne[j])
      cout << li[i], ++i, ++j;
    else if (dp[i][j + 1] < dp[i + 1][j])
      ++i;
    else
      ++j;
  }

  return 0;
}

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

using namespace std;

typedef long long ll;

int dp[3001][3001];
string li, ne;

int f(int i, int j) {
  if (i == li.size() || j == ne.size())
    return 0;
  if (dp[i][j] != -1)
    return dp[i][j];
  if (li[i] == ne[j])
    return dp[i][j] = 1 + f(i + 1, j + 1);
  return dp[i][j] = max(f(i + 1, j), f(i, j + 1));
}

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

  memset(dp, -1, sizeof dp);
  cin >> li >> ne;
  f(0, 0);
  int i = 0, j = 0;
  while (dp[i][j] > 0) {
    if (li[i] == ne[j])
      cout << li[i], ++i, ++j;
    else if (dp[i][j + 1] < dp[i + 1][j])
      ++i;
    else
      ++j;
  }

  return 0;
}
Problem p03165 (C++)

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

#define ll long long
#define ios                                                                    \
  ios::sync_with_stdio(false);                                                 \
  cin.tie(0);                                                                  \
  cout.tie(0);
const ll mod = 1000000007;

using namespace std;

string a, b;
int d[1010][1010];

void lcs(int n, int m) {
  for (int i = 0; i <= n; i++) {
    for (int j = 0; j <= m; j++) {
      if (j == 0 or i == 0)
        d[i][j] = 0;
      else if (a[i - 1] == b[j - 1])
        d[i][j] = d[i - 1][j - 1] + 1;
      else
        d[i][j] = max(d[i - 1][j], d[i][j - 1]);
    }
  }
  string ans;
  int i = n, j = m;
  while (i > 0 and j > 0) {
    if (a[i - 1] == b[j - 1]) {
      i--, j--;
      ans += a[i];
      continue;
    }
    if (d[i - 1][j] > d[i][j - 1])
      i--;
    else
      j--;
  }
  reverse(ans.begin(), ans.end());
  cout << ans;
}

int main() {
  cin >> a >> b;
  lcs(a.size(), b.size());
  return 0;
}
/*
AGGTAB
GXTXAYB
*/


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

#define ll long long
#define ios                                                                    \
  ios::sync_with_stdio(false);                                                 \
  cin.tie(0);                                                                  \
  cout.tie(0);
const ll mod = 1000000007;

using namespace std;

string a, b;
int d[3010][3010];

void lcs(int n, int m) {
  for (int i = 0; i <= n; i++) {
    for (int j = 0; j <= m; j++) {
      if (j == 0 or i == 0)
        d[i][j] = 0;
      else if (a[i - 1] == b[j - 1])
        d[i][j] = d[i - 1][j - 1] + 1;
      else
        d[i][j] = max(d[i - 1][j], d[i][j - 1]);
    }
  }
  string ans;
  int i = n, j = m;
  while (i > 0 and j > 0) {
    if (a[i - 1] == b[j - 1]) {
      i--, j--;
      ans += a[i];
      continue;
    }
    if (d[i - 1][j] > d[i][j - 1])
      i--;
    else
      j--;
  }
  reverse(ans.begin(), ans.end());
  cout << ans;
}

int main() {
  cin >> a >> b;
  lcs(a.size(), b.size());
  return 0;
}
/*
AGGTAB
GXTXAYB
*/
Problem p03165 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define Ds 1
#define Dt 2
#define Db 3

#define rep(i, a, b) for (int i = a; i < (b); ++i)
#define trav(a, x) for (auto &a : x)
#define all(x) x.begin(), x.end()
#define sz(x) (int)(x).size()
#define ff first
#define ss second
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
const int S = int(1e3) + 10;
const ll MOD = ll(1e9) + 7;

string s, t;
int dp[S][S], mv[S][S];

int main() {
  cin.sync_with_stdio(0);
  cin.tie(0);
  cin >> s >> t;
  int n = sz(s);
  int m = sz(t);
  rep(i, 1, n + 1) rep(j, 1, m + 1) {
    mv[i][j] = Ds;
    if (dp[i][j] < dp[i - 1][j]) {
      dp[i][j] = dp[i - 1][j];
      mv[i][j] = Ds;
    }
    if (dp[i][j] < dp[i][j - 1]) {
      dp[i][j] = dp[i][j - 1];
      mv[i][j] = Dt;
    }
    if (s[i - 1] == t[j - 1] && dp[i][j] < dp[i - 1][j - 1] + 1) {
      dp[i][j] = dp[i - 1][j - 1] + 1;
      mv[i][j] = Db;
    }
  }
  int ci = n, cj = m;
  string res;
  while (ci > 0 && cj > 0) {
    if (mv[ci][cj] == Ds)
      ci--;
    else if (mv[ci][cj] == Dt)
      cj--;
    else {
      res.push_back(s[ci - 1]);
      ci--;
      cj--;
    }
  }
  reverse(all(res));
  cout << res << endl;

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define Ds 1
#define Dt 2
#define Db 3

#define rep(i, a, b) for (int i = a; i < (b); ++i)
#define trav(a, x) for (auto &a : x)
#define all(x) x.begin(), x.end()
#define sz(x) (int)(x).size()
#define ff first
#define ss second
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
const int S = int(3e3) + 10;
const ll MOD = ll(1e9) + 7;

string s, t;
int dp[S][S], mv[S][S];

int main() {
  cin.sync_with_stdio(0);
  cin.tie(0);
  cin >> s >> t;
  int n = sz(s);
  int m = sz(t);
  rep(i, 1, n + 1) rep(j, 1, m + 1) {
    mv[i][j] = Ds;
    if (dp[i][j] < dp[i - 1][j]) {
      dp[i][j] = dp[i - 1][j];
      mv[i][j] = Ds;
    }
    if (dp[i][j] < dp[i][j - 1]) {
      dp[i][j] = dp[i][j - 1];
      mv[i][j] = Dt;
    }
    if (s[i - 1] == t[j - 1] && dp[i][j] < dp[i - 1][j - 1] + 1) {
      dp[i][j] = dp[i - 1][j - 1] + 1;
      mv[i][j] = Db;
    }
  }
  int ci = n, cj = m;
  string res;
  while (ci > 0 && cj > 0) {
    if (mv[ci][cj] == Ds)
      ci--;
    else if (mv[ci][cj] == Dt)
      cj--;
    else {
      res.push_back(s[ci - 1]);
      ci--;
      cj--;
    }
  }
  reverse(all(res));
  cout << res << endl;

  return 0;
}
Problem p03165 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define inf 1000000000
#define INF 1000000000000000
#define ll long long
#define ull unsigned long long
#define M (int)(1e9 + 7)
#define P pair<int, int>
#define PLL pair<ll, ll>
#define FOR(i, m, n) for (int i = (int)m; i < (int)n; i++)
#define RFOR(i, m, n) for (int i = (int)m; i >= (int)n; i--)
#define rep(i, n) FOR(i, 0, n)
#define rrep(i, n) RFOR(i, n, 0)
#define all(a) a.begin(), a.end()
const int vx[4] = {0, 1, -1, 0};
const int vy[4] = {1, 0, 0, -1};
#define PI 3.14159265
#define F first
#define S second
#define PB push_back
#define int ll

string s, t;
string dp[2][3001];
int size[2][3001];

signed main() {
  cin >> s >> t;

  rep(i, s.size()) {
    rep(j, t.size()) {
      if (s[i] == t[j]) {
        if (i == 0 || j == 0) {
          dp[i % 2][j] = s[i];
          size[i % 2][j] = 1;
        } else {
          dp[i % 2][j] = dp[(i + 1) % 2][j - 1] + s[i];
          size[i % 2][j] = size[(i + 1) % 2][j - 1] + 1;
        }
      } else {
        if (i == 0 && j == 0)
          continue;
        else if (i == 0 ||
                 j != 0 && size[(i + 1) % 2][j] < size[i % 2][j - 1]) {
          dp[i % 2][j] = dp[i % 2][j - 1];
          size[i % 2][j] = size[i % 2][j - 1];
        } else {
          dp[i % 2][j] = dp[(i + 1) % 2][j];
          size[i % 2][j] = size[(i + 1) % 2][j];
        }
      }
    }
  }

  cout << dp[(s.size() + 1) % 2][t.size() - 1] << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define inf 1000000000
#define INF 1000000000000000
#define ll long long
#define ull unsigned long long
#define M (int)(1e9 + 7)
#define P pair<int, int>
#define PLL pair<ll, ll>
#define FOR(i, m, n) for (int i = (int)m; i < (int)n; i++)
#define RFOR(i, m, n) for (int i = (int)m; i >= (int)n; i--)
#define rep(i, n) FOR(i, 0, n)
#define rrep(i, n) RFOR(i, n, 0)
#define all(a) a.begin(), a.end()
const int vx[4] = {0, 1, -1, 0};
const int vy[4] = {1, 0, 0, -1};
#define PI 3.14159265
#define F first
#define S second
#define PB push_back
#define int ll

string s, t;
string dp[2][3001];
int size[2][3001];

signed main() {
  cin >> s >> t;

  rep(i, s.size()) {
    rep(j, t.size()) {
      if (s[i] == t[j]) {
        if (i == 0 || j == 0) {
          dp[i % 2][j] = s[i];
          size[i % 2][j] = 1;
        } else if (size[i % 2][j] < size[(i + 1) % 2][j - 1] + 1) {
          dp[i % 2][j] = dp[(i + 1) % 2][j - 1] + s[i];
          size[i % 2][j] = size[(i + 1) % 2][j - 1] + 1;
        }
      } else {
        if (i == 0 && j == 0)
          continue;
        else if (i == 0 ||
                 j != 0 && size[(i + 1) % 2][j] < size[i % 2][j - 1]) {
          dp[i % 2][j] = dp[i % 2][j - 1];
          size[i % 2][j] = size[i % 2][j - 1];
        } else {
          dp[i % 2][j] = dp[(i + 1) % 2][j];
          size[i % 2][j] = size[(i + 1) % 2][j];
        }
      }
    }
  }

  cout << dp[(s.size() + 1) % 2][t.size() - 1] << endl;
}