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

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
int main() {
  string i1;
  string i2;
  cin >> i1 >> i2;
  long int Arr[i1.length() + 1][i2.length() + 1];
  for (long int i = 0; i <= i2.length(); i++) {
    for (long int j = 0; j <= i1.length(); j++) {
      if (i == 0 || j == 0) {
        Arr[i][j] = 0;
      } else if (i2[i - 1] == i1[j - 1]) {
        Arr[i][j] = Arr[i - 1][j - 1] + 1;
      } else {
        Arr[i][j] = max(Arr[i - 1][j], Arr[i][j - 1]);
      }
    }
  }
  long int a = i1.length(), b = i2.length();
  if (Arr[b][a] > 0) {
    int e = Arr[b][a] - 1;
    int d = Arr[b][a];
    char Brr[d];
    while (true) {
      if (a == 0 || b == 0)
        break;
      if (Arr[b][a] == Arr[b][a - 1]) {
        a--;
      } else if (Arr[b][a] == Arr[b - 1][a]) {
        b--;
      } else {
        Brr[e] = i1[a - 1];
        b--;
        a--;
        e--;
      }
    }
    for (int h = 0; h < d; h++) {
      cout << Brr[h];
    }
  }
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int main() {
  string i1;
  string i2;
  cin >> i1 >> i2;
  long int Arr[i2.length() + 1][i1.length() + 1];
  for (long int i = 0; i <= i2.length(); i++) {
    for (long int j = 0; j <= i1.length(); j++) {
      if (i == 0 || j == 0) {
        Arr[i][j] = 0;
      } else if (i2[i - 1] == i1[j - 1]) {
        Arr[i][j] = Arr[i - 1][j - 1] + 1;
      } else {
        Arr[i][j] = max(Arr[i - 1][j], Arr[i][j - 1]);
      }
    }
  }
  long int a = i1.length(), b = i2.length();
  if (Arr[b][a] > 0) {
    int e = Arr[b][a] - 1;
    int d = Arr[b][a];
    char Brr[d];
    while (true) {
      if (a == 0 || b == 0)
        break;
      if (Arr[b][a] == Arr[b][a - 1]) {
        a--;
      } else if (Arr[b][a] == Arr[b - 1][a]) {
        b--;
      } else {
        Brr[e] = i1[a - 1];
        b--;
        a--;
        e--;
      }
    }
    for (int h = 0; h < d; h++) {
      cout << Brr[h];
    }
  }
}
Problem p03165 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
int main() {
  string s, t, res = "";
  cin >> s >> t;
  int n = s.size(), m = t.size(), i, j;
  int b[n + 2][m + 2];
  for (i = 0; i <= n; i++) {
    b[i][0] = 0;
  }
  for (i = 0; i <= m; i++) {
    b[0][i] = 0;
  }
  for (i = 1; i <= n; i++) {
    for (j = 1; j <= m; j++) {
      if (s[i - 1] == t[j - 1])
        b[i][j] = b[i - 1][j - 1] + 1;
      else
        b[i][j] = max(b[i - 1][j], b[i][j - 1]);
      // cout<<b[i][j]<<" ";
    }
    // cout<<"\n";
  }
  // cout<<b[n][m];
  i = n;
  j = m;
  while (i >= 0 || j >= 0) {
    if (b[i][j] > max(b[i - 1][j], b[i][j - 1])) {
      res += t[j - 1];
      i = i - 1;
      j = j - 1;
    } else {
      if (b[i - 1][j] > b[i][j - 1]) {
        i = i - 1;
        j = j;
      } else {
        i = i;
        j = j - 1;
      }
    }
  }
  reverse(res.begin(), res.end());
  cout << res << "\n";
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int main() {
  string s, t, res = "";
  cin >> s >> t;
  int n = s.size(), m = t.size(), i, j;
  int b[n + 2][m + 2];
  for (i = 0; i <= n; i++) {
    b[i][0] = 0;
  }
  for (i = 0; i <= m; i++) {
    b[0][i] = 0;
  }
  for (i = 1; i <= n; i++) {
    for (j = 1; j <= m; j++) {
      if (s[i - 1] == t[j - 1])
        b[i][j] = b[i - 1][j - 1] + 1;
      else
        b[i][j] = max(b[i - 1][j], b[i][j - 1]);
      // cout<<b[i][j]<<" ";
    }
    // cout<<"\n";
  }
  // cout<<b[n][m];
  i = n;
  j = m;
  while (i >= 0 && j >= 0) {
    if (b[i][j] > max(b[i - 1][j], b[i][j - 1])) {
      res += t[j - 1];
      i = i - 1;
      j = j - 1;
    } else {
      if (b[i - 1][j] > b[i][j - 1]) {
        i = i - 1;
        j = j;
      } else {
        i = i;
        j = j - 1;
      }
    }
  }
  reverse(res.begin(), res.end());
  cout << res << "\n";
  return 0;
}
Problem p03165 (C++)

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

const int MAX = 1010;

string a, b;
int dp[MAX][MAX];

int sol(int m, int n) {
  if (!m or !n)
    return 0;
  int &ans = dp[m][n];
  if (ans != -1)
    return ans;
  if (a[m - 1] == b[n - 1])
    ans = 1 + sol(m - 1, n - 1);
  else
    ans = max(sol(m, n - 1), sol(m - 1, n));
  return ans;
}

int main() {
  cin >> a >> b;
  memset(dp, -1, sizeof dp);
  sol(a.size(), b.size());
  string ans = "";
  int i = a.size(), j = b.size();
  while (i > 0 and j > 0) {
    if (a[i - 1] == b[j - 1]) {
      i--, j--;
      ans.push_back(a[i]);
    } else if (dp[i - 1][j] > dp[i][j - 1])
      i--;
    else
      j--;
  }
  reverse(ans.begin(), ans.end());
  cout << ans << endl;
}

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

const int MAX = 3010;

string a, b;
int dp[MAX][MAX];

int sol(int m, int n) {
  if (!m or !n)
    return 0;
  int &ans = dp[m][n];
  if (ans != -1)
    return ans;
  if (a[m - 1] == b[n - 1])
    ans = 1 + sol(m - 1, n - 1);
  else
    ans = max(sol(m, n - 1), sol(m - 1, n));
  return ans;
}

int main() {
  cin >> a >> b;
  memset(dp, -1, sizeof dp);
  sol(a.size(), b.size());
  string ans = "";
  int i = a.size(), j = b.size();
  while (i > 0 and j > 0) {
    if (a[i - 1] == b[j - 1]) {
      i--, j--;
      ans.push_back(a[i]);
    } else if (dp[i - 1][j] > dp[i][j - 1])
      i--;
    else
      j--;
  }
  reverse(ans.begin(), ans.end());
  cout << ans << endl;
}
Problem p03165 (C++)

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

using namespace std;
#define MAX 1010
string a, b;
int dp[MAX][MAX];
int get(int i = 0, int j = 0) {
  if (i == a.size() || j == b.size())
    return 0;
  int &ret = dp[i][j];
  if (ret + 1)
    return ret;

  ret = 0;
  if (a[i] == b[j])
    return ret = 1 + get(i + 1, j + 1);
  return ret = max(get(i + 1, j), get(i, j + 1));
}
void getPath(int i = 0, int j = 0) {
  if (i == a.size() || j == b.size()) {
    cout << '\n';
    return;
  }

  int ret = get(i, j);

  if (a[i] == b[j]) {
    cout << a[i];
    getPath(i + 1, j + 1);
    return;
  }
  if (ret == get(i + 1, j))
    getPath(i + 1, j);
  else
    getPath(i, j + 1);
}
int main() {
  cin >> a >> b;
  memset(dp, -1, sizeof dp);

  getPath(0, 0);
  return 0;
}


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

using namespace std;
#define MAX 3010
string a, b;
int dp[MAX][MAX];
int get(int i = 0, int j = 0) {
  if (i == a.size() || j == b.size())
    return 0;
  int &ret = dp[i][j];
  if (ret + 1)
    return ret;

  ret = 0;
  if (a[i] == b[j])
    return ret = 1 + get(i + 1, j + 1);
  return ret = max(get(i + 1, j), get(i, j + 1));
}
void getPath(int i = 0, int j = 0) {
  if (i == a.size() || j == b.size()) {
    cout << '\n';
    return;
  }

  int ret = get(i, j);

  if (a[i] == b[j]) {
    cout << a[i];
    getPath(i + 1, j + 1);
    return;
  }
  if (ret == get(i + 1, j))
    getPath(i + 1, j);
  else
    getPath(i, j + 1);
}
int main() {
  cin >> a >> b;
  memset(dp, -1, sizeof dp);

  getPath(0, 0);
  return 0;
}
Problem p03165 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
#define all(x) (x).begin(), (x).end()
#define pll pair<ll, ll>
#define rep(i, n) for (int i = 0; i < n; i++)
const ll INF = 1LL << 60;
const ll mod = (int)1e9 + 7;

int main() {
  // ll N; cin >> N;
  // ll N,M; cin >> N >> M;
  // string S; cin >> S;
  // ll H,W; cin >> H >> W;
  int dp[3100][3100] = {0};
  string s, t;
  cin >> s >> t;
  rep(i, s.length()) {
    rep(j, t.length()) {
      if (s[i] == t[j])
        chmax(dp[i + 1][j + 1], dp[i][j] + 1);
      chmax(dp[i + 1][j + 1], dp[i + 1][j]);
      chmax(dp[i + 1][j + 1], dp[i][j + 1]);
    }
  }
  string res = "";
  int i = (int)s.size();
  int j = (int)t.size();
  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 if (dp[i][j] == dp[i - 1][j - 1]) {
      res = s[i - 1] + res;
      i--;
      j--;
    }
  }
  cout << res << endl;
}

/*



 */


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
#define all(x) (x).begin(), (x).end()
#define pll pair<ll, ll>
#define rep(i, n) for (int i = 0; i < n; i++)
const ll INF = 1LL << 60;
const ll mod = (int)1e9 + 7;

int main() {
  // ll N; cin >> N;
  // ll N,M; cin >> N >> M;
  // string S; cin >> S;
  // ll H,W; cin >> H >> W;
  int dp[3100][3100] = {0};
  string s, t;
  cin >> s >> t;
  rep(i, s.length()) {
    rep(j, t.length()) {
      if (s[i] == t[j])
        chmax(dp[i + 1][j + 1], dp[i][j] + 1);
      chmax(dp[i + 1][j + 1], dp[i + 1][j]);
      chmax(dp[i + 1][j + 1], dp[i][j + 1]);
    }
  }
  string res = "";
  int i = (int)s.size();
  int j = (int)t.size();
  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 <cstdio> // 回溯输出
#include <cstring>

const int maxn = 3000;

char s[maxn], t[maxn];

int dp[maxn][maxn];

int max(int a, int b) { return a > b ? a : b; }

void print_string(int, int);

int main() {

  scanf("%s %s", s + 1, t + 1);

  memset(dp, 0, sizeof(dp));

  int len1 = strlen(s + 1);

  int len2 = strlen(t + 1);

  int ans = 0;

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

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

      dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]);

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

      if (i == len1 && j == len2)
        ans = max(ans, dp[i][j]);
    }
  }

  print_string(len1, len2);
  printf("\n");

  return 0;
}

void print_string(int i, int j) {

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

  if (s[i] == t[j] && dp[i][j] == dp[i - 1][j - 1] + 1) {

    print_string(i - 1, j - 1);

    printf("%c", s[i]);
  }

  else {

    if (dp[i - 1][j] == dp[i][j])
      print_string(i - 1, j);

    else
      print_string(i, j - 1);
  }
}

Fixed submission:
#include <cstdio> // 回溯输出
#include <cstring>

const int maxn = 3000 + 5;

char s[maxn], t[maxn];

int dp[maxn][maxn];

int max(int a, int b) { return a > b ? a : b; }

void print_string(int, int);

int main() {

  scanf("%s %s", s + 1, t + 1);

  memset(dp, 0, sizeof(dp));

  int len1 = strlen(s + 1);

  int len2 = strlen(t + 1);

  int ans = 0;

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

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

      dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]);

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

      if (i == len1 && j == len2)
        ans = max(ans, dp[i][j]);
    }
  }

  print_string(len1, len2);
  printf("\n");

  return 0;
}

void print_string(int i, int j) {

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

  if (s[i] == t[j] && dp[i][j] == dp[i - 1][j - 1] + 1) {

    print_string(i - 1, j - 1);

    printf("%c", s[i]);
  }

  else {

    if (dp[i - 1][j] == dp[i][j])
      print_string(i - 1, j);

    else
      print_string(i, j - 1);
  }
}
Problem p03165 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
int main() {
  string text1, text2, str;
  getline(cin, text1);
  getline(cin, text2);
  int m = text1.size() + 1;
  int n = text2.size() + 1;

  text1 = '*' + text1;
  text2 = '*' + text2;

  int lcs[m][n];
  for (int i = 0; i < n; i++)
    lcs[0][i] = 0;
  for (int i = 0; i < m; i++)
    lcs[i][0] = 0;
  for (int i = 1; i < m; i++) {
    for (int j = 1; j < n; j++) {
      if (text1[i] == text2[j])
        lcs[i][j] = 1 + lcs[i - 1][j - 1];

      else
        lcs[i][j] = max(lcs[i - 1][j], lcs[i][j - 1]);
    }
  }
  int i = m - 1;
  int j = n - 1;
  for (; i >= 1;) {
    for (; j >= 1 && i >= 1;) {
      if (lcs[i][j] == 1 + lcs[i - 1][j - 1] && text1[i] == text2[j]) {
        str = text1[i] + str;
        i = i - 1;
        j = j - 1;
      } else if (lcs[i][j] == lcs[i - 1][j])
        i = i - 1;
      else if (lcs[i][j] == lcs[i][j - 1])
        j = j - 1;
      else
        break;
    }
  }
  cout << str << "\n";
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int main() {
  string text1, text2, str;
  getline(cin, text1);
  getline(cin, text2);
  int m = text1.size() + 1;
  int n = text2.size() + 1;

  text1 = '*' + text1;
  text2 = '*' + text2;

  int lcs[m][n];
  for (int i = 0; i < n; i++)
    lcs[0][i] = 0;
  for (int i = 0; i < m; i++)
    lcs[i][0] = 0;
  for (int i = 1; i < m; i++) {
    for (int j = 1; j < n; j++) {
      if (text1[i] == text2[j])
        lcs[i][j] = 1 + lcs[i - 1][j - 1];

      else
        lcs[i][j] = max(lcs[i - 1][j], lcs[i][j - 1]);
    }
  }
  int i = m - 1;
  int j = n - 1;

  while (j - 1 >= 0 && i - 1 >= 0) {
    if (lcs[i][j] == 1 + lcs[i - 1][j - 1] && text1[i] == text2[j]) {
      str = text1[i] + str;
      i = i - 1;
      j = j - 1;
    } else if (lcs[i][j] == lcs[i - 1][j])
      i = i - 1;
    else if (lcs[i][j] == lcs[i][j - 1])
      j = j - 1;
  }
  cout << str << "\n";
}
Problem p03165 (C++)

Buggy submission (status: Runtime Error):
//
// Created by Ciafrino on 1/6/19.
//
#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <deque>
#include <functional>
#include <iostream>
#include <map>
#include <memory>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <unordered_map>
#include <utility>
#include <vector>

using namespace std;

typedef long long lint;

int dp[100][100];

string get_lcs(string s, string t, int size_s, int size_t) {
  for (int i = 0; i <= size_s; ++i) {
    for (int j = 0; j <= size_t; ++j) {
      if (i == 0 || j == 0)
        dp[i][j] = 0;
      else 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 idx = dp[size_s][size_t];
  char lcs[idx + 1];
  lcs[idx] = '\0';
  int i = size_s, j = size_t;
  while (i > 0 && j > 0) {
    if (s[i - 1] == t[j - 1]) {
      lcs[idx - 1] = s[i - 1];
      --i;
      --j;
      --idx;
    } else if (dp[i - 1][j] > dp[i][j - 1])
      --i;
    else
      --j;
  }
  return lcs;
}

string read_string() {
  char *str;
  scanf("%ms", &str);
  string result(str);
  free(str);
  return result;
}

int main() {
  string s, t;
  s = read_string();
  t = read_string();
  int size_s = s.size(), size_t = t.size();
  string result = get_lcs(s, t, size_s, size_t);
  cout << result;
  return 0;
}

Fixed submission:
//
// Created by Ciafrino on 1/6/19.
//
#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <deque>
#include <functional>
#include <iostream>
#include <map>
#include <memory>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <unordered_map>
#include <utility>
#include <vector>

using namespace std;

typedef long long lint;

int dp[3010][3010];

string get_lcs(string s, string t, int size_s, int size_t) {
  for (int i = 0; i <= size_s; ++i) {
    for (int j = 0; j <= size_t; ++j) {
      if (i == 0 || j == 0)
        dp[i][j] = 0;
      else 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 idx = dp[size_s][size_t];
  char lcs[idx + 1];
  lcs[idx] = '\0';
  int i = size_s, j = size_t;
  while (i > 0 && j > 0) {
    if (s[i - 1] == t[j - 1]) {
      lcs[idx - 1] = s[i - 1];
      --i;
      --j;
      --idx;
    } else if (dp[i - 1][j] > dp[i][j - 1])
      --i;
    else
      --j;
  }
  return lcs;
}

string read_string() {
  char *str;
  scanf("%ms", &str);
  string result(str);
  free(str);
  return result;
}

int main() {
  string s, t;
  s = read_string();
  t = read_string();
  int size_s = s.size(), size_t = t.size();
  string result = get_lcs(s, t, size_s, size_t);
  cout << result;
  return 0;
}
Problem p03165 (C++)

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

int main() {
  string s, t;
  cin >> s;
  cin >> t;
  int n = s.length();
  int 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;
        continue;
      }
      if (s[i - 1] == t[j - 1]) {
        dp[i][j] = dp[i - 1][j - 1] + 1;
      } else {
        dp[i][j] = max(dp[i][j - 1], dp[i - 1][j]);
      }
    }
  }
  string ss = "";
  /*
  for(int i=0;i<=n;i++)
  {
          for(int j=0;j<=m;j++)
          {

          }

  }*/
  int i = n, j = m;
  int len = dp[n][m];
  while (i > 0 && j > 0) {

    if (s[i - 1] == t[j - 1]) {
      ss += t[j - 1];
      i--;
      j--;

    } else {
      if (dp[i][j - 1] < dp[i - 1][j]) {
        i--;
      } else
        j--;
    }
  }
  reverse(ss.begin(), ss.end());
  cout << ss << endl;
  // cout<<dp[n][m]<<endl;

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int dp[3005][3005];

int main() {
  string s, t;
  cin >> s;
  cin >> t;
  int n = s.length();
  int 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;
        continue;
      }
      if (s[i - 1] == t[j - 1]) {
        dp[i][j] = dp[i - 1][j - 1] + 1;
      } else {
        dp[i][j] = max(dp[i][j - 1], dp[i - 1][j]);
      }
    }
  }
  string ss = "";
  /*
  for(int i=0;i<=n;i++)
  {
          for(int j=0;j<=m;j++)
          {

          }

  }*/
  int i = n, j = m;
  int len = dp[n][m];
  while (i > 0 && j > 0) {

    if (s[i - 1] == t[j - 1]) {
      ss += t[j - 1];
      i--;
      j--;

    } else {
      if (dp[i][j - 1] < dp[i - 1][j]) {
        i--;
      } else
        j--;
    }
  }
  reverse(ss.begin(), ss.end());
  cout << ss << endl;
  // cout<<dp[n][m]<<endl;

  return 0;
}
Problem p03165 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
int DP[3001][3001];
string S, T, LCS;
void Print() {
  int i = S.length(), j = T.length();
  while (i && j) {
    if (S[i - 1] == T[j - 1]) {
      LCS += S[i - 1];
      i--;
      j--;
    } else {
      if (DP[i - 1][j] > DP[i][j - 1])
        i--;
      else
        j--;
    }
  }
  reverse(LCS.begin(), LCS.end());
  cout << LCS << endl;
  return;
}
int main() {
  cin >> S >> T;
  if (S.length() == 1) {
    for (int i = 0; i < T.length(); i++)
      if (S[0] == T[i]) {
        cout << S[0] << endl;
        return 0;
      }
    puts("");
    return 0;
  }
  if (T.length() == 1) {
    for (int i = 0; i < S.length(); i++)
      if (T[0] == S[i]) {
        cout << T[0] << endl;
        return 0;
      }
    puts("");
    return 0;
  }
  S += ' ';
  T += ' ';
  for (int i = 0; i < S.length(); i++)
    if (T[0] == S[i])
      DP[i + 1][1] = 1;
  for (int i = 0; i < T.length(); i++)
    if (S[0] == T[i])
      DP[1][i + 1] = 1;
  for (int i = 1; i < S.length(); i++)
    for (int j = 1; j < T.length(); 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] = max(DP[i + 1][j], DP[i][j]);
      DP[i][j + 1] = max(DP[i][j + 1], DP[i][j]);
    }
  Print();
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int DP[3002][3002];
string S, T, LCS;
void Print() {
  int i = S.length(), j = T.length();
  while (i && j) {
    if (S[i - 1] == T[j - 1]) {
      LCS += S[i - 1];
      i--;
      j--;
    } else {
      if (DP[i - 1][j] > DP[i][j - 1])
        i--;
      else
        j--;
    }
  }
  reverse(LCS.begin(), LCS.end());
  cout << LCS << endl;
  return;
}
int main() {
  cin >> S >> T;
  if (S.length() == 1) {
    for (int i = 0; i < T.length(); i++)
      if (S[0] == T[i]) {
        cout << S[0] << endl;
        return 0;
      }
    puts("");
    return 0;
  }
  if (T.length() == 1) {
    for (int i = 0; i < S.length(); i++)
      if (T[0] == S[i]) {
        cout << T[0] << endl;
        return 0;
      }
    puts("");
    return 0;
  }
  S += ' ';
  T += ' ';
  for (int i = 0; i < S.length(); i++)
    if (T[0] == S[i])
      DP[i + 1][1] = 1;
  for (int i = 0; i < T.length(); i++)
    if (S[0] == T[i])
      DP[1][i + 1] = 1;
  for (int i = 1; i < S.length(); i++)
    for (int j = 1; j < T.length(); 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] = max(DP[i + 1][j], DP[i][j]);
      DP[i][j + 1] = max(DP[i][j + 1], DP[i][j]);
    }
  Print();
  return 0;
}
Problem p03165 (C++)

Buggy submission (status: Runtime Error):
using namespace std;
#include <bits/stdc++.h>
#define rep(i, s) for (int i = 0; i < s; ++i)
#define repr(i, n) for (int i = n; i >= 0; i--)
#define FOR(i, m, n) for (int i = m; i < n; i++)
#define all(v) (v.begin(), v.end())
#define EACH(i, s)                                                             \
  for (__typeof__((s).begin()) i = (s).begin(); i != (s).end(); ++i)
#define VEC(a, n) vector<int> a(n)
#define PQ(a) priority_queue<int> a
#define PQmin(a) priority_queue<int, : vector<int>, greater<int>> a
#define PAIR pair<int, int>
typedef long long ll;
typedef pair<ll, ll> l_l;
typedef pair<int, int> i_i;
#define EPS (1e-7)
#define INF (1e10)
#define PI (acos(-1))
const ll mod = 1000000007;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  string s, t;
  cin >> s >> t;
  int dp[s.size()][t.size()];
  rep(i, s.size()) {
    rep(j, t.size()) { dp[i][j] = 0; }
  }
  string res;
  int n = s.size();
  int m = t.size();
  FOR(i, 1, n + 1) {
    FOR(j, 1, m + 1) {
      if (s[i - 1] == t[j - 1]) {
        dp[i][j] = dp[i - 1][j - 1] + 1;
      } else {
        dp[i][j] = max(dp[i][j - 1], dp[i - 1][j]);
      }
    }
  }
  int x = n, y = m;
  int ndp = dp[x][y];
  while (x > 0 & y > 0) {
    if (ndp - 1 == dp[x - 1][y - 1] && s[x - 1] == t[y - 1]) {
      x -= 1;
      y -= 1;
      ndp -= 1;
      res = s[x] + res;
    } else if (ndp == dp[x - 1][y]) {
      x -= 1;
    } else {
      y -= 1;
    }
  }
  cout << res << endl;
}


Fixed submission:
using namespace std;
#include <bits/stdc++.h>
#define rep(i, s) for (int i = 0; i < s; ++i)
#define repr(i, n) for (int i = n; i >= 0; i--)
#define FOR(i, m, n) for (int i = m; i < n; i++)
#define all(v) (v.begin(), v.end())
#define EACH(i, s)                                                             \
  for (__typeof__((s).begin()) i = (s).begin(); i != (s).end(); ++i)
#define VEC(a, n) vector<int> a(n)
#define PQ(a) priority_queue<int> a
#define PQmin(a) priority_queue<int, : vector<int>, greater<int>> a
#define PAIR pair<int, int>
typedef long long ll;
typedef pair<ll, ll> l_l;
typedef pair<int, int> i_i;
#define EPS (1e-7)
#define INF (1e10)
#define PI (acos(-1))
const ll mod = 1000000007;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  string s, t;
  cin >> s >> t;
  int dp[s.size() + 1][t.size() + 1];
  rep(i, s.size() + 1) {
    rep(j, t.size() + 1) { dp[i][j] = 0; }
  }
  string res;
  int n = s.size();
  int m = t.size();
  FOR(i, 1, n + 1) {
    FOR(j, 1, m + 1) {
      if (s[i - 1] == t[j - 1]) {
        dp[i][j] = dp[i - 1][j - 1] + 1;
      } else {
        dp[i][j] = max(dp[i][j - 1], dp[i - 1][j]);
      }
    }
  }
  int x = n, y = m;
  int ndp = dp[x][y];
  while (x > 0 & y > 0) {
    if (ndp - 1 == dp[x - 1][y - 1] && s[x - 1] == t[y - 1]) {
      x -= 1;
      y -= 1;
      ndp -= 1;
      res = s[x] + res;
    } else if (ndp == dp[x - 1][y]) {
      x -= 1;
    } else {
      y -= 1;
    }
  }
  cout << res << endl;
}
Problem p03165 (C++)

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

using namespace std;

int dp[3003][3003] = {};

int main() {

  string s, t;
  cin >> s >> t;
  int n = (int)s.size();
  int m = (int)t.size();

  for (int i = 0; i <= n; i++) {
    for (int j = 0; j <= m; j++) {
      if (i * 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 = n;
  int j = m;
  string ans;
  while (i * j > 0) {
    while (dp[i][j] == dp[i - 1][j])
      i--;
    while (dp[i][j] == dp[i][j - 1])
      j--;
    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;

int dp[3003][3003] = {};

int main() {

  string s, t;
  cin >> s >> t;
  int n = (int)s.size();
  int m = (int)t.size();

  for (int i = 0; i <= n; i++) {
    for (int j = 0; j <= m; j++) {
      if (i * 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 = n;
  int j = m;
  string ans;
  while (i * j > 0 && dp[i][j] > 0) {
    while (dp[i][j] == dp[i - 1][j])
      i--;
    while (dp[i][j] == dp[i][j - 1])
      j--;
    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;
typedef long long ll;

const int N = 1e3 + 7;
string s, t;

int dp[N][N];
pair<int, int> construct[N][N];

int solve(int i, int j) {
  if (i == s.size() || j == t.size()) {
    return 0;
  }
  if (dp[i][j] != -1)
    return dp[i][j];

  if (s[i] == t[j]) {
    construct[i][j] = {i + 1, j + 1};
    return dp[i][j] = 1 + solve(i + 1, j + 1);
  } else {
    int moveI = solve(i + 1, j);
    int moveJ = solve(i, j + 1);
    if (moveI > moveJ) {
      construct[i][j] = {i + 1, j};
    } else {
      construct[i][j] = {i, j + 1};
    }
    return dp[i][j] = max(moveI, moveJ);
  }
}

int main() {
  cin >> s >> t;
  memset(dp, -1, sizeof dp);
  solve(0, 0);
  pair<int, int> curr = {0, 0};
  string ans = "";
  while (curr.first < s.size() && curr.second < t.size()) {
    if (s[curr.first] == t[curr.second]) {
      ans += s[curr.first];
    }
    curr = construct[curr.first][curr.second];
  }

  cout << ans;
}

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

const int N = 3e3 + 7;
string s, t;

int dp[N][N];
pair<int, int> construct[N][N];

int solve(int i, int j) {
  if (i == s.size() || j == t.size()) {
    return 0;
  }
  if (dp[i][j] != -1)
    return dp[i][j];

  if (s[i] == t[j]) {
    construct[i][j] = {i + 1, j + 1};
    return dp[i][j] = 1 + solve(i + 1, j + 1);
  } else {
    int moveI = solve(i + 1, j);
    int moveJ = solve(i, j + 1);
    if (moveI > moveJ) {
      construct[i][j] = {i + 1, j};
    } else {
      construct[i][j] = {i, j + 1};
    }
    return dp[i][j] = max(moveI, moveJ);
  }
}

int main() {
  cin >> s >> t;
  memset(dp, -1, sizeof dp);
  solve(0, 0);
  pair<int, int> curr = {0, 0};
  string ans = "";
  while (curr.first < s.size() && curr.second < t.size()) {
    if (s[curr.first] == t[curr.second]) {
      ans += s[curr.first];
    }
    curr = construct[curr.first][curr.second];
  }

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

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

typedef long long ll;
const int INF = 1e+9;

int main() {
  static int dp[3010][3010], ma = 0, me[3010][3010];
  string s, t, ans;
  cin >> s >> t;
  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;
        me[i + 1][j + 1] = 1;
      } else if (dp[i + 1][j] > dp[i][j + 1]) {
        dp[i + 1][j + 1] = dp[i + 1][j];
        me[i + 1][j + 1] = 2;
      } else {
        dp[i + 1][j + 1] = dp[i][j + 1];
        me[i + 1][j + 1] = 3;
      }
    }
  }
  for (int i = s.length(); i >= 0;) {
    for (int j = t.length(); j >= 0;) {
      if (me[i][j] == 3)
        i--;
      else if (me[i][j] == 2)
        j--;
      else if (me[i][j] == 1) {
        i--;
        j--;
        ans.push_back(s[i]);
      } else {
        i--;
        j--;
      }
    }
  }
  reverse(ans.begin(), ans.end());
  cout << ans << endl;
  return 0;
}

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

typedef long long ll;
const int INF = 1e+9;

int main() {
  static int dp[3010][3010], ma = 0, me[3010][3010];
  string s, t, ans;
  cin >> s >> t;
  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;
        me[i + 1][j + 1] = 1;
      } else if (dp[i + 1][j] > dp[i][j + 1]) {
        dp[i + 1][j + 1] = dp[i + 1][j];
        me[i + 1][j + 1] = 2;
      } else {
        dp[i + 1][j + 1] = dp[i][j + 1];
        me[i + 1][j + 1] = 3;
      }
    }
  }
  for (int i = s.length(); i >= 0;) {
    for (int j = t.length(); j >= 0;) {
      if (me[i][j] == 3)
        i--;
      else if (me[i][j] == 2)
        j--;
      else if (me[i][j] == 1) {
        i--;
        j--;
        ans.push_back(s[i]);
      } else {
        i = -1;
        j = -1;
        break;
      }
    }
  }
  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;

void doc() {
  freopen("OB.inp", "r", stdin);
  freopen("OB.out", "w", stdout);
}

const int MaxN = 3 * 1e3 + 5;
int f[MaxN][MaxN];
string s, t;

void Trace(int n, int m) {
  if (n <= 0 || m <= 0)
    return;
  if (f[n][m] == f[n - 1][m])
    Trace(n - 1, m);
  else if (f[n][m] == f[n][m - 1])
    Trace(n, m - 1);
  else {
    Trace(n - 1, m - 1);
    cout << s[n - 1];
  }
}

void solve() {

  cin >> s >> t;
  int n = s.length();
  int m = t.length();
  // int f[n + 1][m + 1];
  for (int i = 0; i < n; i++)
    for (int j = 0; j < m; j++) {
      int x = i + 1;
      int y = j + 1;
      if (s[i] == t[j])
        f[x][y] = f[x - 1][y - 1] + 1;
      f[x][y] = max(f[x][y], max(f[x - 1][y], f[x][y - 1]));
    }
  //    for(int i = 1; i <= n; i++){
  //        for(int j = 1; j <= m; j++)
  //            cout << f[i][j] << " ";
  //        cout << '\n';
  //    }
  //    cout << f[n][m];
  Trace(n, m);
}

int main() {
  //    ios_base::sync_with_stdio(0); cin.tie(nullptr); cout.tie(nullptr);
  ios_base::sync_with_stdio(0);
  cin.tie(nullptr);
  cout.tie(nullptr);
  doc();
  solve();
}


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

using namespace std;

void doc() {
  freopen("OB.inp", "r", stdin);
  freopen("OB.out", "w", stdout);
}

const int MaxN = 3 * 1e3 + 5;
int f[MaxN][MaxN];
string s, t;

void Trace(int n, int m) {
  if (n <= 0 || m <= 0)
    return;
  if (f[n][m] == f[n - 1][m])
    Trace(n - 1, m);
  else if (f[n][m] == f[n][m - 1])
    Trace(n, m - 1);
  else {
    Trace(n - 1, m - 1);
    cout << s[n - 1];
  }
}

void solve() {

  cin >> s >> t;
  int n = s.length();
  int m = t.length();
  // int f[n + 1][m + 1];
  for (int i = 0; i < n; i++)
    for (int j = 0; j < m; j++) {
      int x = i + 1;
      int y = j + 1;
      if (s[i] == t[j])
        f[x][y] = f[x - 1][y - 1] + 1;
      f[x][y] = max(f[x][y], max(f[x - 1][y], f[x][y - 1]));
    }
  //    for(int i = 1; i <= n; i++){
  //        for(int j = 1; j <= m; j++)
  //            cout << f[i][j] << " ";
  //        cout << '\n';
  //    }
  //    cout << f[n][m];
  Trace(n, m);
}

int main() {
  //    ios_base::sync_with_stdio(0); cin.tie(nullptr); cout.tie(nullptr);
  ios_base::sync_with_stdio(0);
  cin.tie(nullptr);
  cout.tie(nullptr);
  // doc();
  solve();
}
Problem p03165 (C++)

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

using namespace std;

const int MAXN = 4096;
string s, t;
int dp[MAXN][MAXN];
int ls, lt;
int pre[MAXN][MAXN];

void dfs(int x, int y) {
  if (!x && !y)
    return;
  if (pre[x][y] == 1) {
    dfs(x - 1, y - 1);
    cout << s[x];
  } else if (pre[x][y] == 2) {
    dfs(x, y - 1);
  } else
    dfs(x - 1, y);
}

int main() {
  cin >> s >> t;
  ls = s.size();
  lt = t.size();
  s = '$' + s;
  t = '$' + t;
  for (int i = 1; i <= ls; i++) {
    for (int j = 1; j <= lt; j++) {
      if (s[i] == t[j])
        dp[i][j] = dp[i - 1][j - 1] + 1, pre[i][j] = 1;
      if (dp[i][j] < dp[i][j - 1]) {
        dp[i][j] = dp[i][j - 1];
        pre[i][j] = 2;
      }
      if (dp[i][j] < dp[i - 1][j]) {
        dp[i][j] = dp[i - 1][j];
        pre[i][j] = 3;
      }
    }
  }
  if (dp[ls][lt])
    dfs(ls, lt);
  else
    cout << ' ';
  return 0;
}


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

using namespace std;

const int MAXN = 4096;
string s, t;
int dp[MAXN][MAXN];
int ls, lt;
int pre[MAXN][MAXN];

void dfs(int x, int y) {
  if (!x || !y)
    return;
  if (pre[x][y] == 1) {
    dfs(x - 1, y - 1);
    cout << s[x];
  } else if (pre[x][y] == 2) {
    dfs(x, y - 1);
  } else
    dfs(x - 1, y);
}

int main() {
  cin >> s >> t;
  ls = s.size();
  lt = t.size();
  s = '$' + s;
  t = '$' + t;
  for (int i = 1; i <= ls; i++) {
    for (int j = 1; j <= lt; j++) {
      if (s[i] == t[j])
        dp[i][j] = dp[i - 1][j - 1] + 1, pre[i][j] = 1;
      if (dp[i][j] < dp[i][j - 1]) {
        dp[i][j] = dp[i][j - 1];
        pre[i][j] = 2;
      }
      if (dp[i][j] < dp[i - 1][j]) {
        dp[i][j] = dp[i - 1][j];
        pre[i][j] = 3;
      }
    }
  }
  if (dp[ls][lt])
    dfs(ls, lt);
  else
    cout << ' ';
  return 0;
}
Problem p03165 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#include <vector>
using namespace std;
int n;
char s[3000], t[3000];
vector<char> rs;
int slen, tlen;
void read() {
  cin >> s;
  cin >> t;
  slen = strlen(s);
  tlen = strlen(t);
}
int dp[3005][3005];
int solve() {
  int i, j;
  for (i = 0; i <= strlen(s); i++) {
    dp[i][0] = 0;
  }
  for (i = 0; i <= strlen(t); i++) {
    dp[0][i] = 0;
  }
  for (i = 1; i <= strlen(s); i++) {
    for (j = 1; j <= strlen(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 k = dp[strlen(s)][strlen(t)];
  int cnt = k;
  i = slen;
  j = tlen;
  while (cnt > 0) {
    if (dp[i][j] == dp[i - 1][j]) {
      i--;
    } else if (dp[i][j] == dp[i][j - 1]) {
      j--;
    } else if (dp[i][j] != dp[i - 1][j - 1]) {
      rs.push_back(s[i - 1]);
      i--;
      j--;
    }
  }
  for (i = k - 1; i >= 0; i--)
    cout << rs[i];
  return 0;
}
int main() {
  read();
  solve();

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#include <vector>
using namespace std;
int n;
char s[3000], t[3000];
vector<char> rs;
int slen, tlen;
void read() {
  cin >> s;
  cin >> t;
  slen = strlen(s);
  tlen = strlen(t);
}
int dp[3005][3005];
int solve() {
  int i, j;
  for (i = 0; i <= strlen(s); i++) {
    dp[i][0] = 0;
  }
  for (i = 0; i <= strlen(t); i++) {
    dp[0][i] = 0;
  }
  for (i = 1; i <= strlen(s); i++) {
    for (j = 1; j <= strlen(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 k = dp[strlen(s)][strlen(t)];
  int cnt = k;
  i = slen;
  j = tlen;
  while (cnt > 0) {
    if (dp[i][j] == dp[i - 1][j]) {
      i--;
    } else if (dp[i][j] == dp[i][j - 1]) {
      j--;
    } else if (dp[i][j] != dp[i - 1][j - 1]) {
      rs.push_back(s[i - 1]);
      i--;
      j--;
      cnt--;
    }
  }
  for (i = k - 1; i >= 0; i--)
    cout << rs[i];
  return 0;
}
int main() {
  read();
  solve();

  return 0;
}
Problem p03165 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define pb push_back
#define mp make_pair
#define deb(x) cout << #x << " " << x << "\n";
#define MAX 9223372036854775807
#define MIN -9223372036854775807
#define PI 3.141592653589
#define setbits(n) __builtin_popcountll(n)
const ll mod = 1e9 + 7;

const int N = 3001;
int dp[N][N];

int main() {

  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  ll T = clock();

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

  cerr << "\n\nTIME: " << (double)(clock() - T) / CLOCKS_PER_SEC << " sec\n";
  T = clock();
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define pb push_back
#define mp make_pair
#define deb(x) cout << #x << " " << x << "\n";
#define MAX 9223372036854775807
#define MIN -9223372036854775807
#define PI 3.141592653589
#define setbits(n) __builtin_popcountll(n)
const ll mod = 1e9 + 7;

const int N = 3001;
int dp[N][N];

int main() {

  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  ll T = clock();

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

  cerr << "\n\nTIME: " << (double)(clock() - T) / CLOCKS_PER_SEC << " sec\n";
  T = clock();
  return 0;
}
Problem p03165 (C++)

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

string S, T;
pair<int, int> dp[3009][3009];

int main() {
  cin >> S >> T;
  for (int i = 0; i <= 3000; i++) {
    for (int j = 0; j <= 3000; j++)
      dp[i][j] = make_pair(-1, -1);
  }
  dp[0][0] = make_pair(0, -1);
  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] =
            max(dp[i + 1][j + 1], make_pair(dp[i][j].first + 1, 0));
      }
      dp[i + 1][j] = max(dp[i + 1][j], make_pair(dp[i][j].first, 1));
      dp[i][j + 1] = max(dp[i][j + 1], make_pair(dp[i][j].first, 2));
    }
  }
  int maxn = 0;
  pair<int, int> maxid = make_pair(-1, -1);
  for (int i = 0; i <= S.size(); i++) {
    for (int j = 0; j <= T.size(); j++) {
      if (dp[i][j].first > maxn) {
        maxn = dp[i][j].first;
        maxid = make_pair(i, j);
      }
    }
  }
  string V;
  while (maxid != make_pair(0, 0)) {
    if (dp[maxid.first][maxid.second].second == 0) {
      maxid.first--;
      maxid.second--;
      V += S[maxid.first];
    }
    if (dp[maxid.first][maxid.second].second == 1)
      maxid.first--;
    if (dp[maxid.first][maxid.second].second == 2)
      maxid.second--;
  }
  reverse(V.begin(), V.end());
  cout << V << endl;
  return 0;
}

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

string S, T;
pair<int, int> dp[3009][3009];

int main() {
  cin >> S >> T;
  for (int i = 0; i <= 3000; i++) {
    for (int j = 0; j <= 3000; j++)
      dp[i][j] = make_pair(-1, -1);
  }
  dp[0][0] = make_pair(0, -1);
  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] =
            max(dp[i + 1][j + 1], make_pair(dp[i][j].first + 1, 0));
      }
      dp[i + 1][j] = max(dp[i + 1][j], make_pair(dp[i][j].first, 1));
      dp[i][j + 1] = max(dp[i][j + 1], make_pair(dp[i][j].first, 2));
    }
  }
  int maxn = -1;
  pair<int, int> maxid = make_pair(-1, -1);
  for (int i = 0; i <= S.size(); i++) {
    for (int j = 0; j <= T.size(); j++) {
      if (dp[i][j].first > maxn) {
        maxn = dp[i][j].first;
        maxid = make_pair(i, j);
      }
    }
  }
  string V;
  while (maxid != make_pair(0, 0)) {
    if (dp[maxid.first][maxid.second].second == 0) {
      maxid.first--;
      maxid.second--;
      V += S[maxid.first];
    }
    if (dp[maxid.first][maxid.second].second == 1)
      maxid.first--;
    if (dp[maxid.first][maxid.second].second == 2)
      maxid.second--;
  }
  reverse(V.begin(), V.end());
  cout << V << endl;
  return 0;
}
Problem p03165 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <assert.h>
#include <bitset>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <math.h>
#include <memory.h>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define ll long long
#define ppi pair<int, int>
#define mp make_pair
#define inf 2e16
const int N = 2e5 + 11, K = 20, S = 1e3 + 11;
const int mod = 1e9 + 7;
int dp[111][111], n, m;
int main() {
#ifndef ONLINE_JUDGE
  // freopen("inp.txt", "r", stdin);
#endif
  string a, b;
  cin >> a >> b;
  n = a.size(), m = b.size();
  for (int i = n - 1; i >= 0; i--) {
    for (int j = m - 1; j >= 0; 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 j = 0;
  for (int i = 0; i < n; i++) {
    while (j < m) {
      if (dp[i][j] == dp[i + 1][j])
        break;
      else if (a[i] == b[j] && dp[i][j] == dp[i + 1][j + 1] + 1) {
        printf("%c", b[j++]);
        break;
      } else
        j++;
    }
  }
  return 0;
}

Fixed submission:
#include <algorithm>
#include <assert.h>
#include <bitset>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <math.h>
#include <memory.h>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define ll long long
#define ppi pair<int, int>
#define mp make_pair
#define inf 2e16
const int N = 2e5 + 11, K = 20, S = 1e3 + 11;
const int mod = 1e9 + 7;
int dp[3011][3011], n, m;
int main() {
#ifndef ONLINE_JUDGE
  // freopen("inp.txt", "r", stdin);
#endif
  string a, b;
  cin >> a >> b;
  n = a.size(), m = b.size();
  for (int i = n - 1; i >= 0; i--) {
    for (int j = m - 1; j >= 0; 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 j = 0;
  for (int i = 0; i < n; i++) {
    while (j < m) {
      if (dp[i][j] == dp[i + 1][j])
        break;
      else if (a[i] == b[j] && dp[i][j] == dp[i + 1][j + 1] + 1) {
        printf("%c", b[j++]);
        break;
      } else
        j++;
    }
  }
  return 0;
}
Problem p03165 (C++)

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

/*
code credits : ਜ਼ੈਲਦਾਰ
dashan jot singh
*/
using namespace std;
#define ll long long int
#define fast ios_base::sync_with_stdio(0), cin.tie(NULL), cout.tie(NULL)
#define pb push_back
#define mp make_pair
#define inf LLONG_MAX
#define p pair<ll, ll>
string s, t;
ll a, b;
ll dp[3010][3010];
string ans;
ll solve(ll i, ll j) {
  if (i == 0 || j == 0) {
    return 0;
  }
  // ll ans=0;
  if (s[i - 1] == t[j - 1]) {
    dp[i][j] = 1 + solve(i - 1, j - 1);
    return dp[i][j];
  } else {
    dp[i][j] = max(solve(i - 1, j), solve(i, j - 1));
    return dp[i][j];
  }
  // dp[i][j]=ans;
  // return dp[i][j];
}
int main() {
  fast;
  cin >> s >> t;
  a = s.length();
  b = t.length();
  memset(dp, -1, sizeof(dp));
  ll axr = solve(a, b);
  ll i = a;
  ll j = b;
  while (i > 0 && j > 0) {
    if (s[i - 1] == t[j - 1]) {
      ans += t[j - 1];
      i--;
      j--;
    } else if (dp[i - 1][j] > dp[i][j - 1]) {
      i--;
    } else {
      j--;
    }
  }
  reverse(ans.begin(), ans.end());
  cout << ans << endl;
  return 0;
}

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

/*
code credits : ਜ਼ੈਲਦਾਰ
dashan jot singh
*/
using namespace std;
#define ll long long int
#define fast ios_base::sync_with_stdio(0), cin.tie(NULL), cout.tie(NULL)
#define pb push_back
#define mp make_pair
#define inf LLONG_MAX
#define p pair<ll, ll>
string s, t;
ll a, b;
ll dp[3010][3010];
string ans;
ll solve(ll i, ll j) {
  if (i == 0 || j == 0) {
    return 0;
  }
  // ll ans=0;
  if (dp[i][j] != -1) {
    return dp[i][j];
  }
  if (s[i - 1] == t[j - 1]) {
    dp[i][j] = 1 + solve(i - 1, j - 1);
    return dp[i][j];
  } else {
    dp[i][j] = max(solve(i - 1, j), solve(i, j - 1));
    return dp[i][j];
  }
  // dp[i][j]=ans;
  // return dp[i][j];
}
int main() {
  fast;
  cin >> s >> t;
  a = s.length();
  b = t.length();
  memset(dp, -1, sizeof(dp));
  ll axr = solve(a, b);
  ll i = a;
  ll j = b;
  while (i > 0 && j > 0) {
    if (s[i - 1] == t[j - 1]) {
      ans += t[j - 1];
      i--;
      j--;
    } else if (dp[i - 1][j] > dp[i][j - 1]) {
      i--;
    } else {
      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;
int main() {
  string a, b;
  cin >> a >> b;
  vector<vector<int>> dp(a.length() + 1, vector<int>(b.length() + 1, 0));
  for (int i = 1; i <= a.length(); i++) {
    for (int j = 1; j <= b.length(); j++) {
      if (a[i - 1] == b[j - 1]) {
        dp[i][j] = dp[i - 1][j - 1] + 1;
      } else {
        dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]);
      }
    }
  }
  int i = a.length();
  int j = b.length();
  string ans;
  while (i > 0 || j > 0) {
    if (a[i - 1] == b[j - 1]) {
      ans += a[i - 1];
      i--;
      j--;
    } else if (dp[i - 1][j] > dp[i][j - 1]) {
      i--;
    } else
      j--;
  }
  reverse(ans.begin(), ans.end());
  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int main() {
  string a, b;
  cin >> a >> b;
  vector<vector<int>> dp(a.length() + 1, vector<int>(b.length() + 1, 0));
  for (int i = 1; i <= a.length(); i++) {
    for (int j = 1; j <= b.length(); j++) {
      if (a[i - 1] == b[j - 1]) {
        dp[i][j] = dp[i - 1][j - 1] + 1;
      } else {
        dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]);
      }
    }
  }
  int i = a.length();
  int j = b.length();
  string ans;
  while (i > 0 && j > 0) {
    if (a[i - 1] == b[j - 1]) {
      ans += a[i - 1];
      i--;
      j--;
    } else if (dp[i - 1][j] > dp[i][j - 1]) {
      i--;
    } else
      j--;
  }
  reverse(ans.begin(), ans.end());
  cout << ans << endl;
  return 0;
}