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

Buggy submission (status: Time Limit Exceeded):
/***********************************
*******AUTHOR SHASHI KANT **********
****GMAIL Shshiksnt140895@gmail.com*
************************************
***********************************/
#include <bits/stdc++.h>
#define ll long long int
using namespace std;
/*class graph
{
        map<int,list<int>>adj;
        public :
               void addedge(int a, int b)
               {
                   adj[a].push_back(b);
                   adj[b].push_back(a);
               }

}*/
// vector<vector<int>>dp(1000000);
int dp[3005][3005];
string lcsString(string &str1, string &str2, int len) {

  string LCS;
  int i = 0;
  int j = 0;
  while (len > 0) {
    if (str1[i] == str2[j]) {
      LCS.push_back(str1[i]);
      i++;
      j++;
      len--;
    } else {
      if (dp[i + 1][j] > dp[i][j + 1])
        i++;
      else
        j++;
    }
  }
  return LCS;
}

int lcs(string str1, string str2, int i, int j) {

  if (i >= str1.size() || j >= str2.size())
    return 0;

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

  if (str1[i] == str2[j]) {
    dp[i][j] = 1 + lcs(str1, str2, i + 1, j + 1);
    return dp[i][j];
  } else {
    int l1 = lcs(str1, str2, i + 1, j);
    int l2 = lcs(str1, str2, i, j + 1);
    dp[i][j] = max(l1, l2);
    return dp[i][j];
  }
}
int main() {

  string str1, str2;
  cin >> str1 >> str2;
  int n = str1.size();
  int m = str2.size();

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

  int len = lcs(str1, str2, 0, 0);
  cout << lcsString(str1, str2, len);

  return 0;
}

Fixed submission:
/***********************************
*******AUTHOR SHASHI KANT **********
****GMAIL Shshiksnt140895@gmail.com*
************************************
***********************************/
#include <bits/stdc++.h>
#define ll long long int
using namespace std;
/*class graph
{
        map<int,list<int>>adj;
        public :
               void addedge(int a, int b)
               {
                   adj[a].push_back(b);
                   adj[b].push_back(a);
               }

}*/
// vector<vector<int>>dp(1000000);
int dp[3005][3005];
string lcsString(string &str1, string &str2, int len) {

  string LCS;
  int i = 0;
  int j = 0;
  while (len > 0) {
    if (str1[i] == str2[j]) {
      LCS.push_back(str1[i]);
      i++;
      j++;
      len--;
    } else {
      if (dp[i + 1][j] > dp[i][j + 1])
        i++;
      else
        j++;
    }
  }
  return LCS;
}

int lcs(string &str1, string &str2, int i, int j) {

  if (i >= str1.size() || j >= str2.size())
    return 0;

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

  if (str1[i] == str2[j]) {
    dp[i][j] = 1 + lcs(str1, str2, i + 1, j + 1);
    return dp[i][j];
  } else {
    int l1 = lcs(str1, str2, i + 1, j);
    int l2 = lcs(str1, str2, i, j + 1);
    dp[i][j] = max(l1, l2);
    return dp[i][j];
  }
}
int main() {

  string str1, str2;
  cin >> str1 >> str2;
  int n = str1.size();
  int m = str2.size();

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

  int len = lcs(str1, str2, 0, 0);
  cout << lcsString(str1, str2, len);

  return 0;
}
Problem p03165 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <assert.h>
#include <bitset>
#include <chrono>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string.h>
#include <time.h>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#define rnd mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
#define ll long long
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define FIO                                                                    \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);
#define all(x) x.begin(), x.end()
#define PII pair<ll, ll>
#define N 100007
#define MOD 1000000007
#define INF 1000000000000000000
using namespace std;

int dp[3001][3001];

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

Fixed submission:
#include <algorithm>
#include <assert.h>
#include <bitset>
#include <chrono>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string.h>
#include <time.h>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#define rnd mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
#define ll long long
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define FIO                                                                    \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);
#define all(x) x.begin(), x.end()
#define PII pair<ll, ll>
#define N 100007
#define MOD 1000000007
#define INF 1000000000000000000
using namespace std;

int dp[3001][3001];

int main() {
  string s, t, r = "";
  cin >> s >> t;
  for (int i = 0; i <= s.size(); i++) {
    for (int j = 0; j <= t.size(); j++) {
      if (i == 0 || j == 0) {
        dp[i][j] = 0;
      } else if (s[i - 1] == t[j - 1]) {
        dp[i][j] = 1 + dp[i - 1][j - 1];
      } else {
        dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]);
      }
    }
  }
  int i = s.size(), j = t.size();
  while (i > 0 && j > 0) {
    if (s[i - 1] == t[j - 1] && dp[i][j] == 1 + dp[i - 1][j - 1]) {
      r += s[i - 1];
      i -= 1;
      j -= 1;
    } else if (dp[i][j] == dp[i - 1][j]) {
      i -= 1;
    } else {
      j -= 1;
    }
  }
  reverse(all(r));
  cout << r;
  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 < (int)(n); i++)
typedef long long ll;

ll memo[33][33];

int main() {
  string a, b;
  cin >> a >> b;
  for (int i = 0; i <= a.size(); i++)
    memo[i][0] = 0;
  for (int j = 0; j <= b.size(); j++)
    memo[0][j] = 0;
  for (int i = 1; i <= a.size(); i++) {
    for (int j = 1; j <= b.size(); j++) {
      if (a[i - 1] == b[j - 1])
        memo[i][j] = memo[i - 1][j - 1] + 1;
      else
        memo[i][j] = max(memo[i - 1][j], memo[i][j - 1]);
    }
  }
  string res;
  int i = a.size(), j = b.size();
  while (i > 0 && j > 0) {
    if (a[i - 1] == b[j - 1]) {
      res += a[i - 1];
      i--;
      j--;
    } else if (memo[i - 1][j] > memo[i][j - 1])
      i--;
    else
      j--;
  }
  reverse(res.begin(), res.end());
  cout << res << endl;
  return 0;
}

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

ll memo[3003][3003];

int main() {
  string a, b;
  cin >> a >> b;
  for (int i = 0; i <= a.size(); i++)
    memo[i][0] = 0;
  for (int j = 0; j <= b.size(); j++)
    memo[0][j] = 0;
  for (int i = 1; i <= a.size(); i++) {
    for (int j = 1; j <= b.size(); j++) {
      if (a[i - 1] == b[j - 1])
        memo[i][j] = memo[i - 1][j - 1] + 1;
      else
        memo[i][j] = max(memo[i - 1][j], memo[i][j - 1]);
    }
  }
  string res;
  int i = a.size(), j = b.size();
  while (i > 0 && j > 0) {
    if (a[i - 1] == b[j - 1]) {
      res += a[i - 1];
      i--;
      j--;
    } else if (memo[i - 1][j] > memo[i][j - 1])
      i--;
    else
      j--;
  }
  reverse(res.begin(), res.end());
  cout << res << endl;
  return 0;
}
Problem p03165 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <string>
using namespace std;
string s, t, ans;
int dp[3001][3001];
int main() {
  cin >> s >> t;
  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;
      else
        dp[i + 1][j + 1] = max(dp[i + 1][j], dp[i][j + 1]);
    }
  }
  for (int i = s.size(); i > 0;) {
    for (int j = t.size(); 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] + ans;
        i--;
        j--;
      }
    }
  }
  cout << ans;
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <string>
using namespace std;
string s, t, ans;
int dp[3001][3001];
int main() {
  cin >> s >> t;
  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;
      else
        dp[i + 1][j + 1] = max(dp[i + 1][j], dp[i][j + 1]);
    }
  }
  for (int i = s.size(), j = t.size(); 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] + ans;
      i--;
      j--;
    }
  }
  cout << ans;
}
Problem p03165 (C++)

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

using namespace std;
#define chmax(a, b) a = max(a, b)
#define N 310
ll f[N][N];
string s, t;
int main() {
  cin >> s >> t;
  for (int i = 0; i < N; i++)
    for (int j = 0; j < N; j++)
      f[i][j] = 0;
  for (int i = 1; i <= s.size(); i++)
    for (int j = 1; j <= t.size(); j++) {
      if (s[i - 1] == t[j - 1])
        chmax(f[i][j], f[i - 1][j - 1] + 1);
      chmax(f[i][j], f[i - 1][j]);
      chmax(f[i][j], f[i][j - 1]);
    }
  string ans;
  ll x = s.size(), y = t.size();
  // cout<<"#"<<f[x][y]<<endl;
  while (x > 0 && y > 0) {
    if (f[x - 1][y - 1] == f[x][y] - 1 && s[x - 1] == t[y - 1]) {
      ans += s[x - 1];
      x--, y--;
      continue;
    }
    if (f[x - 1][y] == f[x][y]) {
      x--;
    } else
      y--;
  }
  reverse(ans.begin(), ans.end());
  cout << ans << endl;
  return 0;
}

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

using namespace std;
#define chmax(a, b) a = max(a, b)
#define N 3010
ll f[N][N];
string s, t;
int main() {
  cin >> s >> t;
  for (int i = 0; i < N; i++)
    for (int j = 0; j < N; j++)
      f[i][j] = 0;
  for (int i = 1; i <= s.size(); i++)
    for (int j = 1; j <= t.size(); j++) {
      if (s[i - 1] == t[j - 1])
        chmax(f[i][j], f[i - 1][j - 1] + 1);
      chmax(f[i][j], f[i - 1][j]);
      chmax(f[i][j], f[i][j - 1]);
    }
  string ans;
  ll x = s.size(), y = t.size();
  // cout<<"#"<<f[x][y]<<endl;
  while (x > 0 && y > 0) {
    if (f[x - 1][y - 1] == f[x][y] - 1 && s[x - 1] == t[y - 1]) {
      ans += s[x - 1];
      x--, y--;
      continue;
    }
    if (f[x - 1][y] == f[x][y]) {
      x--;
    } else
      y--;
  }
  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;

using ll = long long;
using pii = pair<int, int>;
#define dbg(n) cout << '\n' << #n << " is " << n << '\n';
#define pb push_back
#define all(v) v.begin(), v.end()
#define cs(i) cout << "Case " << i + 1 << ": "

const int sz = 1e3 + 5;
const int INF = 1e9 + 5;
string a, b, ans;
int dp[sz][sz];
void printLCS(int i, int j) {
  if (i == 0 || j == 0)
    return;
  else if (a[i - 1] == b[j - 1]) {
    ans.pb(a[i - 1]);
    printLCS(i - 1, j - 1);
  } else if (dp[i - 1][j] > dp[i][j - 1]) {
    printLCS(i - 1, j);
  } else if (dp[i - 1][j] < dp[i][j - 1]) {
    printLCS(i, j - 1);
  } else {
    printLCS(i, j - 1);
  }
}

int main() {

  cin >> a >> b;

  for (int i = 1; i <= a.size(); i++) {
    for (int j = 1; j <= b.size(); j++) {
      if (a[i - 1] == b[j - 1])
        dp[i][j] = dp[i - 1][j - 1] + 1;
      else
        dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]);
    }
  }
  printLCS(a.size(), b.size());
  reverse(all(ans));
  cout << ans << endl;

  return 0;
}


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

using ll = long long;
using pii = pair<int, int>;
#define dbg(n) cout << '\n' << #n << " is " << n << '\n';
#define pb push_back
#define all(v) v.begin(), v.end()
#define cs(i) cout << "Case " << i + 1 << ": "

const int sz = 3e3 + 5;
const int INF = 1e9 + 5;
string a, b, ans;
int dp[sz][sz];
void printLCS(int i, int j) {
  if (i == 0 || j == 0)
    return;
  else if (a[i - 1] == b[j - 1]) {
    ans.pb(a[i - 1]);
    printLCS(i - 1, j - 1);
  } else if (dp[i - 1][j] > dp[i][j - 1]) {
    printLCS(i - 1, j);
  } else if (dp[i - 1][j] < dp[i][j - 1]) {
    printLCS(i, j - 1);
  } else {
    printLCS(i, j - 1);
  }
}

int main() {

  cin >> a >> b;

  for (int i = 1; i <= a.size(); i++) {
    for (int j = 1; j <= b.size(); j++) {
      if (a[i - 1] == b[j - 1])
        dp[i][j] = dp[i - 1][j - 1] + 1;
      else
        dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]);
    }
  }
  printLCS(a.size(), b.size());
  reverse(all(ans));
  cout << ans << endl;

  return 0;
}
Problem p03165 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
const int inf = 1e4 + 5;
typedef long long ll;
string a, b, c;
int len1, len2;
int dp[inf][inf];
bool vis[inf][inf];
void str(int i, int j) {
  if (a[i] == '\0' || b[i] == '\0') {
    cout << c << endl;
    return;
  }
  if (a[i] == b[j]) {
    c += a[i];
    str(i + 1, j + 1);
  } else {
    if (dp[i + 1][j] > dp[i][j + 1]) {
      str(i + 1, j);
    } else {
      str(i, j + 1);
    }
  }
}
int solve(int i, int j) {
  if (a[i] == '\0' || b[j] == '\0') {
    return 0;
  }
  if (vis[i][j])
    return dp[i][j];
  int ans = 0;
  if (a[i] == b[j]) {
    ans = 1 + solve(i + 1, j + 1);
  } else {
    int val1 = solve(i + 1, j);
    int val2 = solve(i, j + 1);
    ans = max(val1, val2);
  }
  vis[i][j] = 1;
  dp[i][j] = ans;
  return dp[i][j];
  // return c;
}
int main() {
  cin >> a >> b;

  int k = solve(0, 0);
  str(0, 0);
  // cout<<c<<endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
const int inf = 1e4 + 5;
typedef long long ll;
string a, b, c;
int len1, len2;
int dp[inf][inf];
bool vis[inf][inf];
void str(int i, int j) {
  if (a[i] == '\0' || b[j] == '\0') {
    cout << c << endl;
    return;
  }
  if (a[i] == b[j]) {
    c += a[i];
    str(i + 1, j + 1);
  } else {
    if (dp[i + 1][j] > dp[i][j + 1]) {
      str(i + 1, j);
    } else {
      str(i, j + 1);
    }
  }
}
int solve(int i, int j) {
  if (a[i] == '\0' || b[j] == '\0') {
    return 0;
  }
  if (vis[i][j])
    return dp[i][j];
  int ans = 0;
  if (a[i] == b[j]) {
    ans = 1 + solve(i + 1, j + 1);
  } else {
    int val1 = solve(i + 1, j);
    int val2 = solve(i, j + 1);
    ans = max(val1, val2);
  }
  vis[i][j] = 1;
  dp[i][j] = ans;
  return dp[i][j];
  // return c;
}
int main() {
  cin >> a >> b;

  int k = solve(0, 0);
  str(0, 0);
  // cout<<c<<endl;
}
Problem p03165 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
const double pi = acos(-1);
const double EPS = 1e-7;
const long long mod = 1e6 + 3;
#define Hello                                                                  \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define ll long long
#define ld long double
#define X real()
#define Y imag()
#define cross(a, b) (conj(a) * (b)).imag()
#define dot(a, b) (conj(a) * (b)).real()
#define dist(a, b) (pow(a.X - b.X, 2) + pow(a.Y - b.Y, 2))
typedef tuple<int, int, int> line;
typedef complex<double> point;
typedef vector<point> polygon;
string s, t;
ll dp[1000][1000], ans = 0;
ll solve(int si, int ti) {
  if (si >= s.size() || ti >= t.size())
    return 0;
  if (dp[si][ti] != -1)
    return dp[si][ti];
  if (s[si] == t[ti])
    ans = 1 + solve(si + 1, ti + 1);
  else
    ans = max(solve(si, ti + 1), solve(si + 1, ti));
  dp[si][ti] = ans;
  return ans;
}
void print(int si, int ti) {
  if (si >= s.size() || ti >= t.size())
    return;
  if (s[si] == t[ti]) {
    cout << s[si];
    print(si + 1, ti + 1);
  } else {
    if (solve(si, ti + 1) >= solve(si + 1, ti))
      print(si, ti + 1);
    else
      print(si + 1, ti);
  }
  return;
}
int main() {
  Hello memset(dp, -1, sizeof dp);
  cin >> s >> t;
  solve(0, 0);
  print(0, 0);
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
const double pi = acos(-1);
const double EPS = 1e-7;
const long long mod = 1e6 + 3;
#define Hello                                                                  \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define ll long long
#define ld long double
#define X real()
#define Y imag()
#define cross(a, b) (conj(a) * (b)).imag()
#define dot(a, b) (conj(a) * (b)).real()
#define dist(a, b) (pow(a.X - b.X, 2) + pow(a.Y - b.Y, 2))
typedef tuple<int, int, int> line;
typedef complex<double> point;
typedef vector<point> polygon;
string s, t;
ll dp[3005][3005], ans = 0;
ll solve(int si, int ti) {
  if (si >= s.size() || ti >= t.size())
    return 0;
  if (dp[si][ti] != -1)
    return dp[si][ti];
  if (s[si] == t[ti])
    ans = 1 + solve(si + 1, ti + 1);
  else
    ans = max(solve(si, ti + 1), solve(si + 1, ti));
  dp[si][ti] = ans;
  return ans;
}
void print(int si, int ti) {
  if (si >= s.size() || ti >= t.size())
    return;
  if (s[si] == t[ti]) {
    cout << s[si];
    print(si + 1, ti + 1);
  } else {
    if (solve(si, ti + 1) >= solve(si + 1, ti))
      print(si, ti + 1);
    else
      print(si + 1, ti);
  }
  return;
}
int main() {
  Hello memset(dp, -1, sizeof dp);
  cin >> s >> t;
  solve(0, 0);
  print(0, 0);
  return 0;
}
Problem p03165 (Python)

Buggy submission (status: Runtime Error):
s = input()
t = input()
dp = [0]
lcs = [""]
for b in t:
    search_idx = 0
    ncs = lcs.copy()
    for i, curr_idx in enumerate(dp):
        char_idx = s.find(b, search_idx)
        if char_idx == -1:
            break
        next_idx = char_idx + 1
        if next_idx < curr_idx:
            dp[i] = next_idx
            ncs[i] = lcs[i - 1] + b
        search_idx = curr_idx
    else:
        char_idx = s.find(b, search_idx)
        if char_idx > -1:
            dp.append(char_idx + 1)
            ncs.append(lcs[-1] + b)
    lcs = ncs
print(lcs[-1])


Fixed submission:
s = input()
t = input()
dp = [0]
lcs = [""]
for b in t:
    search_idx = 0
    ncs = lcs[:]
    for i, curr_idx in enumerate(dp):
        char_idx = s.find(b, search_idx)
        if char_idx == -1:
            break
        next_idx = char_idx + 1
        if next_idx < curr_idx:
            dp[i] = next_idx
            ncs[i] = lcs[i - 1] + b
        search_idx = curr_idx
    else:
        char_idx = s.find(b, search_idx)
        if char_idx > -1:
            dp.append(char_idx + 1)
            ncs.append(lcs[-1] + b)
    lcs = ncs
print(lcs[-1])
Problem p03165 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <stdlib.h>
#include <unordered_map>
#include <vector>

using namespace std;

void solve() {
  string s, t;

  cin >> s >> t;

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

  vector<vector<int>> dp(m, vector<int>(n, 0));
  vector<vector<vector<int>>> sdp(m, vector<vector<int>>(n, vector<int>(0)));

  for (int i = 1; i < m + 1; ++i) {
    for (int j = 1; j < n + 1; ++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 = m;
  int j = n;

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

  cout << ans;
}

int main() {
  cin.sync_with_stdio(false);
  cin.tie(nullptr);

  solve();
}


Fixed submission:
#include <algorithm>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <stdlib.h>
#include <unordered_map>
#include <vector>

using namespace std;

void solve() {
  string s, t;

  cin >> s >> t;

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

  vector<vector<int>> dp(m + 1, vector<int>(n + 1, 0));
  // vector<vector<vector<int>>> sdp(m, vector<vector<int>>(n, vector<int>(0)));

  for (int i = 1; i < m + 1; ++i) {
    for (int j = 1; j < n + 1; ++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 = m;
  int j = n;

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

  cout << ans;
}

int main() {
  cin.sync_with_stdio(false);
  cin.tie(nullptr);

  solve();
}
Problem p03165 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define pb push_back
#define pob pop_back
#define pf push_front
#define pof pop_front
#define nd second
#define st first
using namespace std;
using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using vi = vector<int>;
using vl = vector<ll>;
using mii = map<int, int>;
using si = set<int, int>;
using vpi = vector<pii>;
using vpl = vector<pll>;

const int N = (int)1e3 + 5;

int n, m, a, b, c, q, dp[N][N];
vi V;
string A, B, wyn;

int R(int l, int p) { return rand() % (p - l + 1) + l; }

void wczytaj() {
  cin >> A >> B;
  A = '#' + A;
  B = '#' + B;
}

void solve() {
  for (int i = 1; i < A.size(); ++i) {
    for (int j = 1; j < B.size(); j++) {
      dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]);
      if (A[i] == B[j]) {
        dp[i][j] = max(dp[i][j], dp[i - 1][j - 1] + 1);
      }
    }
  }
  int i = A.size() - 1, j = B.size() - 1;
  while (i and j) {
    if (A[i] == B[j]) {
      wyn += A[i];
      i--;
      j--;
    } else if (dp[i - 1][j] == dp[i][j]) {
      i--;
    } else
      j--;
  }
  reverse(wyn.begin(), wyn.end());
  if (wyn.size() == 0) {
    cout << ' ';
    return;
  }
  cout << wyn << '\n';
}

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int ilosc_przypadkow_testowych = 1;
  //	cin>>ilosc_przypadkow_testowych
  for (int i = 1; i <= ilosc_przypadkow_testowych; ++i) {
    wczytaj();
    solve();
  }
}

Fixed submission:
#include <bits/stdc++.h>
#define pb push_back
#define pob pop_back
#define pf push_front
#define pof pop_front
#define nd second
#define st first
using namespace std;
using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using vi = vector<int>;
using vl = vector<ll>;
using mii = map<int, int>;
using si = set<int, int>;
using vpi = vector<pii>;
using vpl = vector<pll>;

const int N = (int)3e3 + 5;

int n, m, a, b, c, q, dp[N][N];
vi V;
string A, B, wyn;

int R(int l, int p) { return rand() % (p - l + 1) + l; }

void wczytaj() {
  cin >> A >> B;
  A = '#' + A;
  B = '#' + B;
}

void solve() {
  for (int i = 1; i < A.size(); ++i) {
    for (int j = 1; j < B.size(); j++) {
      dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]);
      if (A[i] == B[j]) {
        dp[i][j] = max(dp[i][j], dp[i - 1][j - 1] + 1);
      }
    }
  }
  int i = A.size() - 1, j = B.size() - 1;
  while (i and j) {
    if (A[i] == B[j]) {
      wyn += A[i];
      i--;
      j--;
    } else if (dp[i - 1][j] == dp[i][j]) {
      i--;
    } else
      j--;
  }
  reverse(wyn.begin(), wyn.end());
  if (wyn.size() == 0) {
    cout << ' ';
    return;
  }
  cout << wyn << '\n';
}

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int ilosc_przypadkow_testowych = 1;
  //	cin>>ilosc_przypadkow_testowych
  for (int i = 1; i <= ilosc_przypadkow_testowych; ++i) {
    wczytaj();
    solve();
  }
}
Problem p03165 (C++)

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

string lcs(string a, string b) {
  int n = a.size(), m = b.size();
  vector<vector<int>> X(3000, vector<int>(3000));
  vector<vector<int>> Y(3000, vector<int>(3000));
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      if (a.at(i) == b.at(j)) {
        X.at(i + 1).at(j + 1) = X.at(i).at(j) + 1;
        Y.at(i + 1).at(j + 1) = 0;
      } else if (X.at(i + 1).at(j) < X.at(i).at(j + 1)) {
        X.at(i + 1).at(j + 1) = X.at(i).at(j + 1);
        Y.at(i + 1).at(j + 1) = 1;
      } else {
        X.at(i + 1).at(j + 1) = X.at(i + 1).at(j);
        Y.at(i + 1).at(j + 1) = -1;
      }
    }
  }
  string c;
  for (int i = n, j = m; i > 0 && j > 0;) {
    if (Y.at(i).at(j) > 0) {
      i--;
    } else if (Y.at(i).at(j) < 0) {
      j--;
    } else {
      c.push_back(a.at(i - 1));
      i--;
      j--;
    }
  }
  reverse(c.begin(), c.end());
  return c;
}

int main() {
  string S, T;
  cin >> S >> T;
  cout << lcs(S, T) << "\n";
}

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

string lcs(string a, string b) {
  int n = a.size(), m = b.size();
  vector<vector<int>> X(3001, vector<int>(3001));
  vector<vector<int>> Y(3001, vector<int>(3001));
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      if (a.at(i) == b.at(j)) {
        X.at(i + 1).at(j + 1) = X.at(i).at(j) + 1;
        Y.at(i + 1).at(j + 1) = 0;
      } else if (X.at(i + 1).at(j) < X.at(i).at(j + 1)) {
        X.at(i + 1).at(j + 1) = X.at(i).at(j + 1);
        Y.at(i + 1).at(j + 1) = 1;
      } else {
        X.at(i + 1).at(j + 1) = X.at(i + 1).at(j);
        Y.at(i + 1).at(j + 1) = -1;
      }
    }
  }
  string c;
  for (int i = n, j = m; i > 0 && j > 0;) {
    if (Y.at(i).at(j) > 0) {
      i--;
    } else if (Y.at(i).at(j) < 0) {
      j--;
    } else {
      c.push_back(a.at(i - 1));
      i--;
      j--;
    }
  }
  reverse(c.begin(), c.end());
  return c;
}

int main() {
  string S, T;
  cin >> S >> T;
  cout << lcs(S, T) << "\n";
}
Problem p03165 (C++)

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

  for (int i = 0; i <= lens; i++) {
    for (int j = 0; j <= lent; j++) {
      if (i == 0 || j == 0) {
        dp[i][j] = 0;
        continue;
      }

      if (s[i - 1] == t[j - 1])
        dp[i][j] = dp[i - 1][j - 1] + 1;
      else
        dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]);
    }
  }

  int i = lens;
  int j = lent;
  string res = "";
  while (i > 0 && j > 0) {
    if (s[i - 1] == t[j - 1]) {
      res = res + s[i - 1];
      i--;
      j--;
    } else {
      if (dp[i - 1][j] == dp[i][j])
        i--;
      else
        j--;
    }
  }
  reverse(res.begin(), res.end());
  cout << res << endl;
}

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

  for (int i = 0; i <= lens; i++) {
    for (int j = 0; j <= lent; j++) {
      if (i == 0 || j == 0) {
        dp[i][j] = 0;
        continue;
      }

      if (s[i - 1] == t[j - 1])
        dp[i][j] = dp[i - 1][j - 1] + 1;
      else
        dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]);
    }
  }

  int i = lens;
  int j = lent;
  string res = "";
  while (i > 0 && j > 0) {
    if (s[i - 1] == t[j - 1]) {
      res = res + s[i - 1];
      i--;
      j--;
    } else {
      if (dp[i - 1][j] == dp[i][j])
        i--;
      else
        j--;
    }
  }
  reverse(res.begin(), res.end());
  cout << res << endl;
}
Problem p03165 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define ll long long int
#define mk make_pair
#define pb push_back
#define ppb pop_back
using namespace std;
int dp[3005][3005];
string ans = "";
int main() {
  ios::sync_with_stdio(false);
  string s, t;
  cin >> s >> t;
  int n = s.size(), m = t.size();
  for (int i = 0; i < 3005; i++)
    dp[i][0] = dp[0][i] = 0;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      if (s[i - 1] == t[j - 1])
        dp[i][j] = dp[i - 1][j - 1] + 1;
      else
        dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]);
    }
  }
  int i = n, j = m;
  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 - 1][j])
      i--;
    else
      j--;
  }
  reverse(ans.begin(), ans.end());
  cout << ans;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define ll long long int
#define mk make_pair
#define pb push_back
#define ppb pop_back
using namespace std;
int dp[3005][3005];
string ans = "";
int main() {
  ios::sync_with_stdio(false);
  string s, t;
  cin >> s >> t;
  int n = s.size(), m = t.size();
  for (int i = 0; i < 3005; i++)
    dp[i][0] = dp[0][i] = 0;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      if (s[i - 1] == t[j - 1])
        dp[i][j] = dp[i - 1][j - 1] + 1;
      else
        dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]);
    }
  }
  int i = n, j = m;
  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 - 1][j])
      i--;
    else
      j--;
  }
  reverse(ans.begin(), ans.end());
  cout << ans;
  return 0;
}
Problem p03165 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define fl(i, a, b) for (int i = a; i < b; i++)
#define ll long long
#define endl '\n'
#define pb push_back
#define ff first
#define ss second
#define mod 1000000007
#define mx 200005
#define inf 2e9
#define fast                                                                   \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(NULL);                                                               \
  cout.tie(NULL);
ll dp[3001][3001] = {0};
int main() {
  fast;
  string s, t;
  cin >> s >> t;
  int n = s.length(), m = t.length();
  for (int i = 0; i <= n; i++) {
    for (int j = 0; j <= m; j++) {
      if (i == 0 || j == 0)
        dp[i][j] = 0;
      else if (s[i - 1] == t[j - 1])
        dp[i][j] = 1 + dp[i - 1][j - 1];
      else
        dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]);
    }
  }
  // cout<<dp[n][m];
  ll index = dp[n][m];
  ll i = n, j = m;
  char lcs[index + 1];
  lcs[index] = '\0';
  while (i >= 0 && j >= 0) {
    if (s[i - 1] == t[j - 1]) {
      lcs[index - 1] = s[i - 1];
      index--;
      i--, j--;
    } else if (dp[i - 1][j] > dp[i][j - 1])
      i--;
    else
      j--;
  }
  cout << lcs;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define fl(i, a, b) for (int i = a; i < b; i++)
#define ll long long
#define endl '\n'
#define pb push_back
#define ff first
#define ss second
#define mod 1000000007
#define mx 200005
#define inf 2e9
#define fast                                                                   \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(NULL);                                                               \
  cout.tie(NULL);
ll dp[3001][3001] = {0};
int main() {
  fast;
  string s, t;
  cin >> s >> t;
  int n = s.length(), m = t.length();
  for (int i = 0; i <= n; i++) {
    for (int j = 0; j <= m; j++) {
      if (i == 0 || j == 0)
        dp[i][j] = 0;
      else if (s[i - 1] == t[j - 1])
        dp[i][j] = 1 + dp[i - 1][j - 1];
      else
        dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]);
    }
  }
  // cout<<dp[n][m];
  ll index = dp[n][m];
  ll i = n, j = m;
  char lcs[index + 1];
  lcs[index] = '\0';
  while (i > 0 && j > 0) {
    if (s[i - 1] == t[j - 1]) {
      lcs[index - 1] = s[i - 1];
      index--;
      i--, j--;
    } else if (dp[i - 1][j] > dp[i][j - 1])
      i--;
    else
      j--;
  }
  cout << lcs;
}
Problem p03165 (C++)

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

#define int long long int
#define mp make_pair
using namespace std;

string s, t;
int dp[3002][3002];
int l1, l2;
int fun(int pos1, int pos2) {
  if (pos1 == l1) {
    return 0;
  }
  if (pos2 == l2) {
    return 0;
  }
  if (s[pos1] == t[pos2]) {
    dp[pos1][pos2] = 1 + fun(pos1 + 1, pos2 + 1);
  } else {
    dp[pos1][pos2] = max(fun(pos1 + 1, pos2), fun(pos1, pos2 + 1));
  }
  return dp[pos1][pos2];
}

void path(int i, int j) {
  if (i == l1) {
    return;
  }
  if (j == l2) {
    return;
  }
  if (s[i] == t[j]) {
    cout << s[i];
    path(i + 1, j + 1);
  } else {
    if (dp[i + 1][j] > dp[i][j + 1]) {
      path(i + 1, j);
    } else {
      path(i, j + 1);
    }
  }
  return;
}

int32_t main() {
  cin >> s >> t;
  l1 = s.length();
  l2 = t.length();
  for (int i = 0; i < 3002; ++i) {
    for (int j = 0; j < 3002; ++j) {
      dp[i][j] = -1;
    }
  }
  int ans1 = fun(0, 0);
  path(0, 0);
  return 0;
}

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

#define int long long int
#define mp make_pair
using namespace std;

string s, t;
int dp[3002][3002];
int l1, l2;
int fun(int pos1, int pos2) {
  if (pos1 == l1) {
    return 0;
  }
  if (pos2 == l2) {
    return 0;
  }
  if (dp[pos1][pos2] != -1) {
    return dp[pos1][pos2];
  }
  if (s[pos1] == t[pos2]) {
    dp[pos1][pos2] = 1 + fun(pos1 + 1, pos2 + 1);
  } else {
    dp[pos1][pos2] = max(fun(pos1 + 1, pos2), fun(pos1, pos2 + 1));
  }
  return dp[pos1][pos2];
}

void path(int i, int j) {
  if (i == l1) {
    return;
  }
  if (j == l2) {
    return;
  }
  if (s[i] == t[j]) {
    cout << s[i];
    path(i + 1, j + 1);
  } else {
    if (dp[i + 1][j] > dp[i][j + 1]) {
      path(i + 1, j);
    } else {
      path(i, j + 1);
    }
  }
  return;
}

int32_t main() {
  cin >> s >> t;
  l1 = s.length();
  l2 = t.length();
  for (int i = 0; i < 3002; ++i) {
    for (int j = 0; j < 3002; ++j) {
      dp[i][j] = -1;
    }
  }
  int ans1 = fun(0, 0);
  path(0, 0);
  return 0;
}
Problem p03165 (C++)

Buggy submission (status: Runtime Error):
// shan61916
#include <bits/stdc++.h>
using namespace std;
#define IOS                                                                    \
  ios::sync_with_stdio(false);                                                 \
  cin.tie(NULL);                                                               \
  cout.tie(NULL);
#define endl "\n"
typedef long long ll;
typedef unsigned long long ull;
typedef double dll;
ll dp[1010][1010];
string s1, s2, an = "";
ll len;
ll recurse(ll id1, ll id2) {
  if (id1 < 0 or id2 < 0)
    return 0;
  if (dp[id1][id2] != -1)
    return dp[id1][id2];
  if (s1[id1] == s2[id2]) {
    return dp[id1][id2] = 1 + recurse(id1 - 1, id2 - 1);
  }
  return dp[id1][id2] = max(recurse(id1 - 1, id2), recurse(id1, id2 - 1));
}
void print(ll id1, ll id2) {
  if (id1 < 0 or id2 < 0)
    return;
  ll x = recurse(id1, id2);
  if (s1[id1] == s2[id2]) {
    an += s1[id1];
    print(id1 - 1, id2 - 1);
  } else {
    if (recurse(id1 - 1, id2) == x)
      print(id1 - 1, id2);
    else {
      print(id1, id2 - 1);
    }
  }
}
int main() {
  IOS
#ifdef SHAN
      freopen("input.txt", "r", stdin);
#endif
  memset(dp, -1, sizeof(dp));
  cin >> s1 >> s2;
  len = recurse((ll)s1.length() - 1, (ll)s2.length() - 1);
  print((ll)s1.length() - 1, (ll)s2.length() - 1);
  reverse(an.begin(), an.end());
  cout << an;
  return 0;
} // good night.


Fixed submission:
// shan61916
#include <bits/stdc++.h>
using namespace std;
#define IOS                                                                    \
  ios::sync_with_stdio(false);                                                 \
  cin.tie(NULL);                                                               \
  cout.tie(NULL);
#define endl "\n"
typedef long long ll;
typedef unsigned long long ull;
typedef double dll;
ll dp[3010][3010];
string s1, s2, an = "";
ll len;
ll recurse(ll id1, ll id2) {
  if (id1 < 0 or id2 < 0)
    return 0;
  if (dp[id1][id2] != -1)
    return dp[id1][id2];
  if (s1[id1] == s2[id2]) {
    return dp[id1][id2] = 1 + recurse(id1 - 1, id2 - 1);
  }
  return dp[id1][id2] = max(recurse(id1 - 1, id2), recurse(id1, id2 - 1));
}
void print(ll id1, ll id2) {
  if (id1 < 0 or id2 < 0)
    return;
  ll x = recurse(id1, id2);
  if (s1[id1] == s2[id2]) {
    an += s1[id1];
    print(id1 - 1, id2 - 1);
  } else {
    if (recurse(id1 - 1, id2) == x)
      print(id1 - 1, id2);
    else {
      print(id1, id2 - 1);
    }
  }
}
int main() {
  IOS
#ifdef SHAN
      freopen("input.txt", "r", stdin);
#endif
  memset(dp, -1, sizeof(dp));
  cin >> s1 >> s2;
  len = recurse((ll)s1.length() - 1, (ll)s2.length() - 1);
  print((ll)s1.length() - 1, (ll)s2.length() - 1);
  reverse(an.begin(), an.end());
  cout << an;
  return 0;
} // good night.
Problem p03165 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define ALL(a) (a).begin(), (a).end()
#define rALL(a) (a).rbegin(), (a).rend()
typedef pair<int, int> Pint;
typedef pair<int64_t, int64_t> Pll;

int main() {
  string s, t;
  cin >> s >> t;
  vector<vector<int>> dp(s.size() + 1, vector<int>(t.size() + 1));
  for (int i = 0; i < s.size(); i++) {
    for (int j = 0; j < t.size(); j++) {
      if (s.at(i) == t.at(j)) {
        dp.at(i + 1).at(j + 1) = dp.at(i).at(j) + 1;
      } else {
        if (dp.at(i + 1).at(j) > dp.at(i).at(j + 1)) {
          dp.at(i + 1).at(j + 1) = dp.at(i + 1).at(j);
        } else {
          dp.at(i + 1).at(j + 1) = dp.at(i).at(j + 1);
        }
      }
    }
  }
  string ans = "";
  int i = s.size(), j = t.size();
  while (i != 0 || j != 0) {
    if (i - 1 >= 0 && j - 1 >= 0 &&
        dp.at(i).at(j) == dp.at(i - 1).at(j - 1) + 1 && s.at(i) == t.at(j)) {
      i--;
      j--;
      ans += s.at(i);
    } else if (i - 1 >= 0 && dp.at(i).at(j) == dp.at(i - 1).at(j)) {
      i--;
    } else if (j - 1 >= 0 && dp.at(i).at(j) == dp.at(i).at(j - 1)) {
      j--;
    }
  }
  reverse(ALL(ans));
  cout << ans << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define ALL(a) (a).begin(), (a).end()
#define rALL(a) (a).rbegin(), (a).rend()
typedef pair<int, int> Pint;
typedef pair<int64_t, int64_t> Pll;

int main() {
  string s, t;
  cin >> s >> t;
  vector<vector<int>> dp(s.size() + 1, vector<int>(t.size() + 1));
  for (int i = 0; i < s.size(); i++) {
    for (int j = 0; j < t.size(); j++) {
      if (s.at(i) == t.at(j)) {
        dp.at(i + 1).at(j + 1) = dp.at(i).at(j) + 1;
      } else {
        if (dp.at(i + 1).at(j) > dp.at(i).at(j + 1)) {
          dp.at(i + 1).at(j + 1) = dp.at(i + 1).at(j);
        } else {
          dp.at(i + 1).at(j + 1) = dp.at(i).at(j + 1);
        }
      }
    }
  }
  string ans = "";
  int i = s.size(), j = t.size();
  while (i != 0 || j != 0) {
    if (i - 1 >= 0 && j - 1 >= 0 &&
        dp.at(i).at(j) == dp.at(i - 1).at(j - 1) + 1 &&
        s.at(i - 1) == t.at(j - 1)) {
      i--;
      j--;
      ans += s.at(i);
    } else if (i - 1 >= 0 && dp.at(i).at(j) == dp.at(i - 1).at(j)) {
      i--;
    } else if (j - 1 >= 0 && dp.at(i).at(j) == dp.at(i).at(j - 1)) {
      j--;
    }
  }
  reverse(ALL(ans));
  cout << ans << endl;
}
Problem p03165 (C++)

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

typedef long long ll;
#define pb push_back

string s, t;

ll n, m;

ll dp[4000][4000], vis[4000][4000];
string ans;
ll lcs(ll i, ll j) {
  if (i >= n or j >= m)
    return 0;
  if (vis[i][j])
    return dp[i][j];

  if (s[i] == t[j]) {
    dp[i][j] = 1 + lcs(i + 1, j + 1);
  } else {
    ll on = lcs(i + 1, j);
    ll to = lcs(i, j + 1);
    dp[i][j] = max(on, to);
  }
  return dp[i][j];
}

void print(ll i, ll j) {
  if (i >= n or j >= m) {
    cout << ans << endl;
    return;
  }
  if (s[i] == t[j]) {
    ans.pb(s[i]);
    print(i + 1, j + 1);
  } else {
    if (dp[i + 1][j] > dp[i][j + 1])
      print(i + 1, j);
    else
      print(i, j + 1);
  }
}

int main() {

  cin >> s >> t;
  n = s.size(), m = t.size();
  lcs(0, 0);
  print(0, 0);
  return 0;
}


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

typedef long long ll;
#define pb push_back

string s, t;

ll n, m;

ll dp[4000][4000], vis[4000][4000];
string ans;
ll lcs(ll i, ll j) {
  if (i >= n or j >= m)
    return 0;
  if (vis[i][j])
    return dp[i][j];

  if (s[i] == t[j]) {
    dp[i][j] = 1 + lcs(i + 1, j + 1);
  } else {
    ll on = lcs(i + 1, j);
    ll to = lcs(i, j + 1);
    dp[i][j] = max(on, to);
  }
  vis[i][j] = 1;
  return dp[i][j];
}

void print(ll i, ll j) {
  if (i >= n or j >= m) {
    cout << ans << endl;
    return;
  }
  if (s[i] == t[j]) {
    ans.pb(s[i]);
    print(i + 1, j + 1);
  } else {
    if (dp[i + 1][j] > dp[i][j + 1])
      print(i + 1, j);
    else
      print(i, j + 1);
  }
}

int main() {

  cin >> s >> t;
  n = s.size(), m = t.size();
  lcs(0, 0);
  print(0, 0);
  return 0;
}
Problem p03165 (C++)

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

#define ll long long
#define ld long double
#define pb push_back
#define pii pair<int, int>
#define vi vector<int>
#define vii vector<pii>
#define mi map<int, int>
#define mii map<pii, int>
#define all(a) (a).begin(), (a).end()
#define F first
#define S second
#define sz(x) (int)x.size()
#define endl '\n'
#define hell 1000000007
#define mem(x) memset(x, 0, sizeof(x));
#define what_is(x) cerr << #x << " is " << x << endl;
#define rep(i, a, b) for (int i = a; i < b; i++)

using namespace std;

void solve() {
  string s, t;
  cin >> s >> t;
  int n = sz(s);
  int m = sz(t);

  int dp[n + 1][m + 1];

  rep(i, 0, n + 1) {
    rep(j, 0, m + 1) {
      if (i == 0 || j == 0)
        dp[i][j] = 0;

      else
        dp[i][j] = max(dp[i - 1][j - 1] + (int)(s[i - 1] == t[j - 1]),
                       max(dp[i - 1][j], dp[i][j - 1]));
    }
  }

  string ans = "";

  int i = n, j = m;

  while (j > 0 && i > 0) {
    if (dp[i][j] == dp[i][j - 1]) {

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

  reverse(all(ans));
  cout << ans << endl;
}

int main() {
#ifndef ONLINE_JUDGE
  // for getting input from input.txt
  freopen("input.txt", "r", stdin);
  // for writing output to output.txt
  freopen("output.txt", "w", stdout);
#endif
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);

  int t = 1, i = 1;
  // cin>>t;

  while (t--) {
    // cout<<"Case #"<<i++<<": ";
    solve();
  }

  return 0;
}

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

#define ll long long
#define ld long double
#define pb push_back
#define pii pair<int, int>
#define vi vector<int>
#define vii vector<pii>
#define mi map<int, int>
#define mii map<pii, int>
#define all(a) (a).begin(), (a).end()
#define F first
#define S second
#define sz(x) (int)x.size()
#define endl '\n'
#define hell 1000000007
#define mem(x) memset(x, 0, sizeof(x));
#define what_is(x) cerr << #x << " is " << x << endl;
#define rep(i, a, b) for (int i = a; i < b; i++)

using namespace std;

void solve() {
  string s, t;
  cin >> s >> t;
  int n = sz(s);
  int m = sz(t);

  int dp[n + 1][m + 1];

  rep(i, 0, n + 1) {
    rep(j, 0, m + 1) {
      if (i == 0 || j == 0)
        dp[i][j] = 0;

      else
        dp[i][j] = max(dp[i - 1][j - 1] + (int)(s[i - 1] == t[j - 1]),
                       max(dp[i - 1][j], dp[i][j - 1]));
    }
  }

  string ans = "";

  int i = n, j = m;

  while (j > 0 && i > 0) {
    if (dp[i][j] == dp[i][j - 1]) {

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

  reverse(all(ans));
  cout << ans << endl;
}

int main() {
  // #ifndef ONLINE_JUDGE
  // // for getting input from input.txt
  // freopen("input.txt", "r", stdin);
  // // for writing output to output.txt
  // freopen("output.txt", "w", stdout);
  // #endif
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);

  int t = 1, i = 1;
  // cin>>t;

  while (t--) {
    // cout<<"Case #"<<i++<<": ";
    solve();
  }

  return 0;
}
Problem p03165 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define debug(a) cout << #a << ":" << a << endl;
const int INF = 0x3f3f3f3f;
const int N = 1e6 + 7;
const int mod = 1e9 + 7;
int maxn, minn;
int T, n;
string s1, s2;
int len1, len2;
int dp[4010][4010];

int main() {
  cin >> s1 >> s2;
  len1 = s1.size();
  len2 = s2.size();
  for (int i = 1; i <= len1; i++) {
    for (int j = 1; j <= len2; j++) {
      if (s1[i - 1] == s2[j - 1]) {
        dp[i][j] = dp[i - 1][j - 1] + 1;
      } else {
        dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]);
      }
    }
  }
  string s = "";
  int i = len1, j = len2;
  while (i + j > 2) {
    if (dp[i][j] == dp[i - 1][j - 1] + 1) {
      s += s1[i - 1];
      i--, j--;
    } else {
      if (dp[i][j] == dp[i - 1][j]) {
        i--;
      } else {
        j--;
      }
    }
  }
  reverse(s.begin(), s.end());
  cout << s << endl;

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define debug(a) cout << #a << ":" << a << endl;
const int INF = 0x3f3f3f3f;
const int N = 1e6 + 7;
const int mod = 1e9 + 7;
int maxn, minn;
int T, n;
string s1, s2;
int len1, len2;
int dp[4010][4010];

int main() {
  cin >> s1 >> s2;
  len1 = s1.size();
  len2 = s2.size();
  for (int i = 1; i <= len1; i++) {
    for (int j = 1; j <= len2; j++) {
      if (s1[i - 1] == s2[j - 1]) {
        dp[i][j] = dp[i - 1][j - 1] + 1;
      } else {
        dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]);
      }
    }
  }
  string s = "";
  int i = len1, j = len2;
  while (i != 0 && j != 0) {
    if (s1[i - 1] == s2[j - 1]) {
      s += s1[i - 1];
      i--, j--;
    } else {
      if (dp[i][j] == dp[i - 1][j]) {
        i--;
      } else {
        j--;
      }
    }
  }
  reverse(s.begin(), s.end());
  cout << s << endl;

  return 0;
}
Problem p03165 (C++)

Buggy submission (status: Runtime Error):
#include <cstring>
#include <iostream>
using namespace std;
char s[3001], t[3001];
int n, m, i, j, a[3001][3001];
int main() {
  cin >> s >> t;
  n = strlen(s);
  m = strlen(t);
  for (i = n; i > 0; i--) {
    for (j = m; j > 0; j--) {
      if (s[i - 1] == t[j - 1]) {
        a[i][j] = a[i + 1][j + 1] + 1;
      } else
        a[i][j] = max(a[i][j + 1], a[i + 1][j]);
    }
  }
  i = 1, j = 1;
  while (i <= n && j <= m) {
    if (s[i - 1] == t[j - 1]) {
      cout << s[i - 1];
      i++, j++;

    } else {
      if (a[i + 1][j] > a[i][j + 1]) {
        i++;
      } else
        j++;
    }
  }
  return 0;
}


Fixed submission:
#include <cstring>
#include <iostream>
using namespace std;
char s[3003], t[3003];
int n, m, i, j, a[3003][3003];
int main() {
  cin >> s >> t;
  n = strlen(s);
  m = strlen(t);
  for (i = n; i > 0; i--) {
    for (j = m; j > 0; j--) {
      if (s[i - 1] == t[j - 1]) {
        a[i][j] = a[i + 1][j + 1] + 1;
      } else
        a[i][j] = max(a[i][j + 1], a[i + 1][j]);
    }
  }
  i = 1, j = 1;
  while (i <= n && j <= m) {
    if (s[i - 1] == t[j - 1]) {
      cout << s[i - 1];
      i++, j++;

    } else {
      if (a[i + 1][j] > a[i][j + 1]) {
        i++;
      } else
        j++;
    }
  }
  return 0;
}