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

Buggy submission (status: Runtime Error):


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

string lcs(string s1, string s2) {

  ll int n = s1.length();
  ll int m = s2.length();

  if (n == 0 || m == 0) {
    return "";
  }

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

  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {

      if (s1[i - 1] == s2[j - 1]) {
        dp[i][j] = 1 + dp[i - 1][j - 1];

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

  int s = n, e = m;

  string ans = "";

  while (s >= 0 && e >= 0) {

    if (s1[s - 1] == s2[e - 1]) {
      ans += s1[s - 1];
      s--;
      e--;

    } else {
      if (dp[s][e - 1] > dp[s - 1][e]) {
        e--;

      } else {

        s--;
      }
    }
  }

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

  return ans;
}

int main() {
  string s1, s2;
  cin >> s1 >> s2;
  cout << lcs(s1, s2);
}


Fixed submission:


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

string lcs(string s1, string s2) {

  ll int n = s1.length();
  ll int m = s2.length();

  if (n == 0 || m == 0) {
    return "";
  }

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

  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {

      if (s1[i - 1] == s2[j - 1]) {
        dp[i][j] = 1 + dp[i - 1][j - 1];

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

  int s = n, e = m;

  string ans = "";

  while (s > 0 && e > 0) {

    if (s1[s - 1] == s2[e - 1]) {
      ans += s1[s - 1];
      s--;
      e--;

    } else {
      if (dp[s][e - 1] > dp[s - 1][e]) {
        e--;

      } else {

        s--;
      }
    }
  }

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

  return ans;
}

int main() {
  string s1, s2;
  cin >> s1 >> s2;
  cout << lcs(s1, s2);
}
Problem p03165 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define ll long long int
using namespace std;
ll dp[3000][3000];
int main() {
  string p, q;
  cin >> p >> q;

  for (int i = 0; i <= p.length(); i++) {
    for (int j = 0; j <= q.length(); j++) {
      if (i == 0 || j == 0) {
        dp[i][j] = 0;
      } else if (p[i - 1] == q[j - 1]) {

        dp[i][j] = dp[i - 1][j - 1] + 1;
      } else {
        dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]);
      }
    }
  }
  // cout<<dp[p.length()][q.length()];
  char ans[dp[p.length()][q.length()] + 1];
  ans[dp[p.length()][q.length()]] = '\0';
  int i = p.length();
  int j = q.length();
  int k = dp[p.length()][q.length()] - 1;
  while (i > 0 && j > 0) {
    if (p[i - 1] == q[j - 1]) {
      ans[k] = p[i - 1];
      k--;
      i--;
      j--;
    } else if (dp[i - 1][j] > dp[i][j - 1]) {
      i--;
    } else {
      j--;
    }
  }
  cout << ans;
}


Fixed submission:
#include <bits/stdc++.h>
#define ll long long int
using namespace std;
ll dp[3001][3001];
int main() {
  string p, q;
  cin >> p >> q;

  for (int i = 0; i <= p.length(); i++) {
    for (int j = 0; j <= q.length(); j++) {
      if (i == 0 || j == 0) {
        dp[i][j] = 0;
      } else if (p[i - 1] == q[j - 1]) {

        dp[i][j] = dp[i - 1][j - 1] + 1;
      } else {
        dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]);
      }
    }
  }
  // cout<<dp[p.length()][q.length()];
  char ans[dp[p.length()][q.length()] + 1];
  ans[dp[p.length()][q.length()]] = '\0';
  int i = p.length();
  int j = q.length();
  int k = dp[p.length()][q.length()] - 1;
  while (i > 0 && j > 0) {
    if (p[i - 1] == q[j - 1]) {
      ans[k] = p[i - 1];
      k--;
      i--;
      j--;
    } else if (dp[i - 1][j] > dp[i][j - 1]) {
      i--;
    } else {
      j--;
    }
  }
  cout << ans;
}
Problem p03165 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define int long long int
int mod = INT_MAX;
int memo[3005][3005];
int n;
string LCSstr(string &s1, string &s2, int i, int j, int len) {
  // base cases
  if ((i >= s1.size()) or (j >= s2.size()) or (len == 0))
    return "";
  // selfwork
  string ans;
  while (len > 0) {
    if (s1[i] == s2[j]) {
      ans.push_back(s1[i]);
      i++;
      j++;
      len--;
    } else {
      if (memo[i + 1][j] > memo[i][j + 1])
        i++;
      else
        j++;
    }
  }
  return ans;
}
int LCSlen(string &s1, string &s2, int i, int j) // pass string by reference
{
  // base cases
  if ((i >= s1.size()) or (j >= s2.size()))
    return 0;
  int &ans = memo[i][j];
  // memoizatiion
  if (ans != -1)
    return ans;
  // self work
  if (s1[i] == s2[j])
    return ans = 1 + LCSlen(s1, s2, i + 1, j + 1);
  else
    return ans = max(LCSlen(s1, s2, i + 1, j), LCSlen(s1, s2, i, j + 1));
}

int32_t main() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  memset(memo, -1, sizeof(memo));
  string s1, s2;
  cin >> s1 >> s2;
  int len = LCSlen(s1, s2, 0, 0);
  // cout << len << endl;
  cout << LCSstr(s1, s2, 0, 0, len);
  return 0;
}
// 3 8
// 3 30
// 4 50
// 5 60

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define int long long int
int mod = INT_MAX;
int memo[3005][3005];
int n;
string LCSstr(string &s1, string &s2, int i, int j, int len) {
  // base cases
  if ((i >= s1.size()) or (j >= s2.size()) or (len == 0))
    return "";
  // selfwork
  string ans;
  while (len > 0) {
    if (s1[i] == s2[j]) {
      ans.push_back(s1[i]);
      i++;
      j++;
      len--;
    } else {
      if (memo[i + 1][j] > memo[i][j + 1])
        i++;
      else
        j++;
    }
  }
  return ans;
}
int LCSlen(string &s1, string &s2, int i, int j) // pass string by reference
{
  // base cases
  if ((i >= s1.size()) or (j >= s2.size()))
    return 0;
  int &ans = memo[i][j];
  // memoizatiion
  if (ans != -1)
    return ans;
  // self work
  if (s1[i] == s2[j])
    return ans = 1 + LCSlen(s1, s2, i + 1, j + 1);
  else
    return ans = max(LCSlen(s1, s2, i + 1, j), LCSlen(s1, s2, i, j + 1));
}

int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  memset(memo, -1, sizeof(memo));
  string s1, s2;
  cin >> s1 >> s2;
  int len = LCSlen(s1, s2, 0, 0);
  // cout << len << endl;
  cout << LCSstr(s1, s2, 0, 0, len);
  return 0;
}
// 3 8
// 3 30
// 4 50
// 5 60
Problem p03165 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 3007;
int dp[MAXN][MAXN];
char s[MAXN], t[MAXN];
int main() {
  scanf(" %s", s + 1);
  scanf(" %s", t + 1);
  int n = strlen(s + 1), m = strlen(t + 1);
  for (int i = 1; i <= n / 2; i++)
    swap(s[i], s[n + 1 - i]);
  for (int j = 1; j <= m / 2; j++)
    swap(t[j], t[m + 1 - j]);
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++)
      dp[i][j] =
          s[i] == t[j] ? dp[i - 1][j - 1] + 1 : max(dp[i][j - 1], dp[i - 1][j]);
  }
  for (int i = n, lst = m; i >= 1; --i)
    for (int j = lst; j >= 1; --j) {
      if (dp[i][j] == dp[i - 1][j - 1] + 1) {
        assert(s[i] == t[j]);
        putchar(t[j]);
        lst = j - 1;
        break;
      }
    }
  puts("");
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 3007;
int dp[MAXN][MAXN];
char s[MAXN], t[MAXN];
int main() {
  scanf(" %s", s + 1);
  scanf(" %s", t + 1);
  int n = strlen(s + 1), m = strlen(t + 1);
  for (int i = 1; i <= n / 2; i++)
    swap(s[i], s[n + 1 - i]);
  for (int j = 1; j <= m / 2; j++)
    swap(t[j], t[m + 1 - j]);
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++)
      dp[i][j] =
          s[i] == t[j] ? dp[i - 1][j - 1] + 1 : max(dp[i][j - 1], dp[i - 1][j]);
  }
  int i = n, j = m;
  while (i && j) {
    if (s[i] == t[j])
      putchar(t[j]), i--, j--;
    else
      dp[i - 1][j] > dp[i][j - 1] ? i-- : j--;
  }
  puts("");
  return 0;
}
Problem p03165 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
// #include <ext/pb_ds/assoc_container.hpp>
// using namespace __gnu_pbds;
// typedef tree<int, null_type, less<int>, rb_tree_tag,
// tree_order_statistics_node_update> pbds;
#define int long long
#define pb push_back
#define inf 1e9
#define mod 1000000007

const int maxn = 3010;

int dp[maxn][maxn];

string getLCS(string &s, string &t, int len) {
  string ans;
  int i = 0, j = 0;
  while (len > 0) {
    if (s[i] == t[j]) {
      ans.push_back(s[i]);
      i++;
      j++;
      len--;
    } else {
      if (dp[i][j + 1] > dp[i + 1][j])
        j++;
      else
        i++;
    }
  }
  return ans;
}
int lenLCS(string s, string t, int i, int j) {
  if (i >= s.length() or j >= t.length()) {
    return 0; // one of the string is empty
  }
  if (dp[i][j] != -1)
    return dp[i][j];

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

string fun(string s, string t) {
  memset(dp, -1, sizeof(dp));
  int len = lenLCS(s, t, 0, 0);
  return getLCS(s, t, len);
}

void solve() {
  string s, t;
  cin >> s >> t;
  cout << fun(s, t);
}

int32_t main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  solve();
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
// #include <ext/pb_ds/assoc_container.hpp>
// using namespace __gnu_pbds;
// typedef tree<int, null_type, less<int>, rb_tree_tag,
// tree_order_statistics_node_update> pbds;
#define int long long
#define pb push_back
#define inf 1e9
#define mod 1000000007

const int maxn = 3010;

int dp[maxn][maxn];

string getLCS(string &s, string &t, int len) {
  string ans;
  int i = 0, j = 0;
  while (len > 0) {
    if (s[i] == t[j]) {
      ans.push_back(s[i]);
      i++;
      j++;
      len--;
    } else {
      if (dp[i][j + 1] > dp[i + 1][j])
        j++;
      else
        i++;
    }
  }
  return ans;
}
int lenLCS(string &s, string &t, int i, int j) {
  if (i >= s.length() or j >= t.length()) {
    return 0; // one of the string is empty
  }
  if (dp[i][j] != -1)
    return dp[i][j];

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

string fun(string s, string t) {
  memset(dp, -1, sizeof(dp));
  int len = lenLCS(s, t, 0, 0);
  return getLCS(s, t, len);
}

void solve() {
  string s, t;
  cin >> s >> t;
  cout << fun(s, t);
}

int32_t main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  solve();
  return 0;
}
Problem p03165 (C++)

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

#define pb push_back

using namespace std;

int32_t main() {
  string a, b;
  cin >> a >> b;

  int n = a.length();
  int m = b.length();
  vector<vector<int>> d(n + 1, vector<int>(m + 1));
  for (int i = 0; i <= n; i++) {
    for (int j = 0; j <= m; j++) {
      d[i][j] = 0;
    }
  }
  for (int i = 0; i <= n; i++) {
    for (int j = 0; j <= m; j++) {
      d[i][j] = 0;
      if (i == 0 || j == 0)
        d[i][j] = 0;

      else if (a[i - 1] == b[j - 1]) {
        d[i][j] += d[i - 1][j - 1] + 1;
      } else {
        if (d[i - 1][j] >= d[i][j - 1])
          d[i][j] += d[i - 1][j];
        else
          d[i][j] += d[i][j - 1];
      }
    }
  }
  int ind = d[n][m];
  char ans[ind + 1];
  int i = n;
  int j = m;
  ans[ind] = '\0';
  while (i >= 0 && j >= 0) {
    if (a[i - 1] == b[j - 1]) {
      ans[ind - 1] = a[i - 1];
      ind--;
      i--;
      j--;
    } else if (d[i - 1][j] > d[i][j - 1])
      i--;
    else
      j--;
  }
  cout << ans;
}


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

#define pb push_back

using namespace std;

int32_t main() {
  string a, b;
  cin >> a >> b;

  int n = a.length();
  int m = b.length();
  vector<vector<int>> d(n + 1, vector<int>(m + 1));
  for (int i = 0; i <= n; i++) {
    for (int j = 0; j <= m; j++) {
      d[i][j] = 0;
    }
  }
  for (int i = 0; i <= n; i++) {
    for (int j = 0; j <= m; j++) {
      d[i][j] = 0;
      if (i == 0 || j == 0)
        d[i][j] = 0;

      else if (a[i - 1] == b[j - 1]) {
        d[i][j] += d[i - 1][j - 1] + 1;
      } else {
        if (d[i - 1][j] >= d[i][j - 1])
          d[i][j] += d[i - 1][j];
        else
          d[i][j] += d[i][j - 1];
      }
    }
  }
  int ind = d[n][m];
  char ans[ind + 1];
  int i = n;
  int j = m;
  ans[ind] = '\0';
  while (i > 0 && j > 0) {
    if (a[i - 1] == b[j - 1]) {
      ans[ind - 1] = a[i - 1];
      ind--;
      i--;
      j--;
    } else if (d[i - 1][j] > d[i][j - 1])
      i--;
    else
      j--;
  }
  cout << ans;
}
Problem p03165 (C++)

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

std::string s, t;

int dp[3001][3001];

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

  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      if (s[i - 1] == t[j - 1])
        dp[i][j] = dp[i - 1][j - 1] + 1;
      else
        dp[i][j] = std::max(dp[i - 1][j], dp[i][j - 1]);
    }
  }
  std::string ans;

  while (n | m) {
    if (s[n - 1] == t[m - 1])
      ans.push_back(s[n - 1]), n--, m--;
    else if (dp[n - 1][m] > dp[n][m - 1])
      n--;
    else
      m--;
  }
  std::reverse(ans.begin(), ans.end());

  std::cout << ans << std::endl;

  return 0;
}


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

std::string s, t;

int dp[3001][3001];

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

  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      if (s[i - 1] == t[j - 1])
        dp[i][j] = dp[i - 1][j - 1] + 1;
      else
        dp[i][j] = std::max(dp[i - 1][j], dp[i][j - 1]);
    }
  }
  std::string ans;

  while (n && m) {
    if (s[n - 1] == t[m - 1])
      ans.push_back(s[n - 1]), n--, m--;
    else if (dp[n - 1][m] > dp[n][m - 1])
      n--;
    else
      m--;
  }
  std::reverse(ans.begin(), ans.end());

  std::cout << ans << std::endl;

  return 0;
}
Problem p03165 (C++)

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

#define ll long long
#define pb push_back
#define mp make_pair

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

#ifndef ONLINE_JUDGE
  // for getting input from input.txt
  freopen("input.txt", "r+", stdin);
  // for writing output to output.txt
  freopen("output.txt", "w+", stdout);
#endif

  string a, b;
  cin >> a >> b;
  int n = a.length();
  int m = b.length();

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

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

  // for(int i=0;i<=n;i++) {
  // 	for(int j=0;j<=m;j++) {
  // 		cout << dp[i][j] << " ";
  // 	}
  // 	cout << '\n';
  // }

  // cout << dp[n][m] << '\n';

  string c = "";
  while (n > 0 and m > 0) {
    // cout << n << " " << m << '\n';
    if (a[n - 1] == b[m - 1]) {
      c = a[n - 1] + c;
      m--;
      n--;
      // continue;
    } else if (dp[n - 1][m] > dp[n][m - 1]) {
      n--;
      // continue;
    } else {
      m--;
      // continue;
    }
  }
  cout << c;
}

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

#define ll long long
#define pb push_back
#define mp make_pair

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

  // #ifndef ONLINE_JUDGE
  //    // for getting input from input.txt
  //    freopen("input.txt", "r+", stdin);
  //    // for writing output to output.txt
  //    freopen("output.txt", "w+", stdout);
  // #endif

  string a, b;
  cin >> a >> b;
  int n = a.length();
  int m = b.length();

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

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

  // for(int i=0;i<=n;i++) {
  // 	for(int j=0;j<=m;j++) {
  // 		cout << dp[i][j] << " ";
  // 	}
  // 	cout << '\n';
  // }

  // cout << dp[n][m] << '\n';

  string c = "";
  while (n > 0 and m > 0) {
    // cout << n << " " << m << '\n';
    if (a[n - 1] == b[m - 1]) {
      c = a[n - 1] + c;
      m--;
      n--;
      // continue;
    } else if (dp[n - 1][m] > dp[n][m - 1]) {
      n--;
      // continue;
    } else {
      m--;
      // continue;
    }
  }
  cout << c;
}
Problem p03165 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
#define FOR(i, i0, n) for (int i = (i0); i < (n); i++)
#define COUT(val) cout << (val) << "\n";
using namespace std;
using ll = long long int;

int main(void) {
  string s, t;
  cin >> s >> t;

  int ns = s.size(), nt = t.size();
  vector<vector<int>> dp(ns + 1, vector<int>(nt + 1, -1));
  FOR(i, 0, ns + 1) dp[i][0] = 0;
  FOR(j, 0, nt + 1) dp[0][j] = 0;

  FOR(i, 1, ns + 1) FOR(j, 1, nt + 1) {
    dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]);
    if (s[i - 1] == t[j - 1])
      dp[i][j] = max(dp[i][j], dp[i - 1][j - 1] + 1);
  }

  string ans = "";
  while (ns > 0 || nt > 0) {
    while (dp[ns][nt] == dp[ns - 1][nt])
      ns--;
    while (dp[ns][nt] == dp[ns][nt - 1])
      nt--;
    ans.push_back(s[ns - 1]);
    ns--;
    nt--;
  }
  reverse(ans.begin(), ans.end());
  COUT(ans);
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
#define FOR(i, i0, n) for (int i = (i0); i < (n); i++)
#define COUT(val) cout << (val) << "\n";
using namespace std;
using ll = long long int;

int main(void) {
  string s, t;
  cin >> s >> t;

  int ns = s.size(), nt = t.size();
  vector<vector<int>> dp(ns + 1, vector<int>(nt + 1, -1));
  FOR(i, 0, ns + 1) dp[i][0] = 0;
  FOR(j, 0, nt + 1) dp[0][j] = 0;

  FOR(i, 1, ns + 1) FOR(j, 1, nt + 1) {
    dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]);
    if (s[i - 1] == t[j - 1])
      dp[i][j] = max(dp[i][j], dp[i - 1][j - 1] + 1);
  }

  string ans = "";
  while (dp[ns][nt] > 0) {
    while (dp[ns][nt] == dp[ns - 1][nt])
      ns--;
    while (dp[ns][nt] == dp[ns][nt - 1])
      nt--;
    ans.push_back(s[ns - 1]);
    ns--;
    nt--;
  }
  reverse(ans.begin(), ans.end());
  COUT(ans);
}
Problem p03165 (C++)

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

using namespace std;

string s, t;
int dp[3000][3000];
int main() {
  cin >> s >> t;

  int ls = s.length();
  int lt = t.length();

  for (int i = 1; i <= ls; i++) {
    for (int j = 1; j <= lt; j++) {
      if (s[i - 1] == t[j - 1]) {
        dp[i][j] = dp[i - 1][j - 1] + 1;
      }

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

  int i = ls;
  int j = lt;
  string out;
  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 {
      out = out + s[i - 1];
      i--;
      j--;
    }
  }

  int lo = out.length();

  for (int i = lo - 1; i >= 0; i--) {
    cout << out[i];
  }

  cout << endl;
  return 0;
}


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

using namespace std;

string s, t;
int dp[4000][4000];
int main() {
  cin >> s >> t;

  int ls = s.length();
  int lt = t.length();

  for (int i = 1; i <= ls; i++) {
    for (int j = 1; j <= lt; j++) {
      if (s[i - 1] == t[j - 1]) {
        dp[i][j] = dp[i - 1][j - 1] + 1;
      }

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

  int i = ls;
  int j = lt;
  string out;
  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 {
      out = out + s[i - 1];
      i--;
      j--;
    }
  }

  int lo = out.length();

  for (int i = lo - 1; i >= 0; i--) {
    cout << out[i];
  }

  cout << endl;
  return 0;
}
Problem p03165 (C++)

Buggy submission (status: Runtime Error):
#define rep(i, n) REP(i, 0, n)
#define REP(i, a, b) for (int i = a; i < (int)(b); i++)
#include <bits/stdc++.h>

typedef long long ll;
using namespace std;
template <class T> inline void chmax(T &a, const T &b) {
  if (a < b)
    a = b;
}
template <class T> inline void chmin(T &a, const T &b) {
  if (a > b)
    a = b;
}

const ll MAXN = (ll)3e3 + 10;
const long long INF = 1LL << 60;
ll dp[MAXN][MAXN];

int main(void) {
  string s, t;
  cin >> s;
  cin >> t;
  rep(i, MAXN) rep(j, MAXN) dp[i][j] = 0;
  rep(i, s.size()) rep(j, t.size()) {
    if (s[i] == t[j])
      chmax(dp[i + 1][j + 1], dp[i][j] + 1);
    else {
      chmax(dp[i + 1][j + 1], dp[i][j + 1]);
      chmax(dp[i + 1][j + 1], dp[i + 1][j]);
    }
  }
  // cout<<dp[s.size()][t.size()]<<endl;

  int i = s.size();
  int j = t.size();
  stack<char> ans;
  while (1) {
    if (i == 0 && j == 0)
      break;
    if (dp[i][j] == dp[i - 1][j])
      i--;
    else if (dp[i][j - 1] == dp[i][j])
      j--;
    else {
      i--;
      j--;
      // cout<<s[i]<<endl;
      ans.push(s[i]);
    }
  }
  while (ans.size()) {
    cout << ans.top();
    ans.pop();
  }
  cout << endl;

  return 0;
}

Fixed submission:
#define rep(i, n) REP(i, 0, n)
#define REP(i, a, b) for (int i = a; i < (int)(b); i++)
#include <bits/stdc++.h>

typedef long long ll;
using namespace std;
template <class T> inline void chmax(T &a, const T &b) {
  if (a < b)
    a = b;
}
template <class T> inline void chmin(T &a, const T &b) {
  if (a > b)
    a = b;
}

const ll MAXN = (ll)3e3 + 10;
const long long INF = 1LL << 60;
ll dp[MAXN][MAXN];

int main(void) {
  string s, t;
  cin >> s;
  cin >> t;
  rep(i, MAXN) rep(j, MAXN) dp[i][j] = 0;
  rep(i, s.size()) rep(j, t.size()) {
    if (s[i] == t[j])
      chmax(dp[i + 1][j + 1], dp[i][j] + 1);
    else {
      chmax(dp[i + 1][j + 1], dp[i][j + 1]);
      chmax(dp[i + 1][j + 1], dp[i + 1][j]);
    }
  }
  // cout<<dp[s.size()][t.size()]<<endl;

  int i = s.size();
  int j = t.size();
  stack<char> ans;
  while (1) {
    if (i == 0 || j == 0)
      break;
    if (dp[i][j] == dp[i - 1][j])
      i--;
    else if (dp[i][j - 1] == dp[i][j])
      j--;
    else {
      i--;
      j--;
      // cout<<s[i]<<endl;
      ans.push(s[i]);
    }
  }
  while (ans.size()) {
    cout << ans.top();
    ans.pop();
  }
  cout << endl;

  return 0;
}
Problem p03165 (C++)

Buggy submission (status: Runtime Error):
#include <cstring>
#include <fstream>
#include <iostream>
#define nmax 3001

using namespace std;
ifstream fin("p.in");
ofstream fout("p.out");

int dist[nmax][nmax];

int main() {
  char s[nmax], t[nmax];
  cin >> s >> t;
  int n = strlen(s), m = strlen(t);

  for (int i = 0; i <= n; i++)
    for (int j = 0; j <= m; j++)
      if (i == 0 || j == 0)
        dist[i][j] = 0;
      else if (s[i - 1] == t[j - 1])
        dist[i][j] = 1 + dist[i - 1][j - 1];
      else
        dist[i][j] = max(dist[i - 1][j], dist[i][j - 1]);

  /*for(int i=1; i<=n; i++)
      {
          for(int j=1; j<=m; j++)
              fout<<dist[i][j]<<" ";
          fout<<endl;
      }*/

  char sol[nmax];
  int ct = 0;

  int i = n, j = m;
  while (i >= 0 && j >= 0) {
    if (s[i - 1] == t[j - 1]) {
      sol[ct++] = s[i - 1];
      i--;
      j--;
    } else if (dist[i - 1][j] > dist[i][j - 1])
      i--;
    else
      j--;
  }

  sol[ct] = NULL;
  for (int i = ct - 1; i >= 0; i--)
    cout << sol[i];

  return 0;
}


Fixed submission:
#include <cstring>
#include <fstream>
#include <iostream>
#define nmax 3001

using namespace std;
ifstream fin("p.in");
ofstream fout("p.out");

int dist[nmax][nmax];

int main() {
  char s[nmax], t[nmax];
  cin >> s >> t;
  int n = strlen(s), m = strlen(t);

  for (int i = 0; i <= n; i++)
    for (int j = 0; j <= m; j++)
      if (i == 0 || j == 0)
        dist[i][j] = 0;
      else if (s[i - 1] == t[j - 1])
        dist[i][j] = 1 + dist[i - 1][j - 1];
      else
        dist[i][j] = max(dist[i - 1][j], dist[i][j - 1]);

  /*for(int i=1; i<=n; i++)
      {
          for(int j=1; j<=m; j++)
              fout<<dist[i][j]<<" ";
          fout<<endl;
      }*/

  char sol[nmax];
  int ct = 0;

  int i = n, j = m;
  while (i > 0 && j > 0) {
    if (s[i - 1] == t[j - 1]) {
      sol[ct++] = s[i - 1];
      i--;
      j--;
    } else if (dist[i - 1][j] > dist[i][j - 1])
      i--;
    else
      j--;
  }

  sol[ct] = NULL;
  for (int i = ct - 1; i >= 0; i--)
    cout << sol[i];

  return 0;
}
Problem p03165 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define PI acos(-1)
#define mx 110
int fx[] = {0, 0, -1, 1};
int fy[] = {1, -1, 0, 0};
char s1[mx], s2[mx];
char symbol[mx][mx];
int i, j, m, n, larr[mx][mx];
void print(int i, int j) {
  if (i == 0 || j == 0)
    return;
  if (symbol[i][j] == 'c') {
    print(i - 1, j - 1);
    printf("%c", s1[i - 1]);
  } else if (symbol[i][j] == 'u') {
    print(i - 1, j);
  } else {
    print(i, j - 1);
  }
}
void Lcs() {
  m = strlen(s1);
  n = strlen(s2);
  for (i = 1; i <= m; i++) {
    for (j = 1; j <= n; j++) {
      if (s1[i - 1] == s2[j - 1]) {
        larr[i][j] = larr[i - 1][j - 1] + 1;
        symbol[i][j] = 'c'; // c -> diagonal
      } else if (larr[i - 1][j] >= larr[i][j - 1]) {
        larr[i][j] = larr[i - 1][j];
        symbol[i][j] = 'u'; // c -> upper
      } else {
        larr[i][j] = larr[i][j - 1];
        symbol[i][j] = 'l'; // c -> left
      }
    }
  }
}
int main() {
  scanf("%s", s1);
  scanf("%s", s2);
  Lcs();
  print(m, n);
  printf("\n");
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define PI acos(-1)
#define mx 3005
int fx[] = {0, 0, -1, 1};
int fy[] = {1, -1, 0, 0};
char s1[mx], s2[mx];
char symbol[mx][mx];
int i, j, m, n, larr[mx][mx];
void print(int i, int j) {
  if (i == 0 || j == 0)
    return;
  if (symbol[i][j] == 'c') {
    print(i - 1, j - 1);
    printf("%c", s1[i - 1]);
  } else if (symbol[i][j] == 'u') {
    print(i - 1, j);
  } else {
    print(i, j - 1);
  }
}
void Lcs() {
  m = strlen(s1);
  n = strlen(s2);
  for (i = 1; i <= m; i++) {
    for (j = 1; j <= n; j++) {
      if (s1[i - 1] == s2[j - 1]) {
        larr[i][j] = larr[i - 1][j - 1] + 1;
        symbol[i][j] = 'c'; // c -> diagonal
      } else if (larr[i - 1][j] >= larr[i][j - 1]) {
        larr[i][j] = larr[i - 1][j];
        symbol[i][j] = 'u'; // c -> upper
      } else {
        larr[i][j] = larr[i][j - 1];
        symbol[i][j] = 'l'; // c -> left
      }
    }
  }
}
int main() {
  scanf("%s", s1);
  scanf("%s", s2);
  Lcs();
  print(m, n);
  printf("\n");
  return 0;
}
Problem p03165 (C++)

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

int main() {
  std::string s, t, result;
  std::cin >> s >> t;
  int DP[301][301];

  for (int i = 0; i < (int)s.length(); i++)
    DP[i][0] = 0;
  for (int i = 0; i < (int)t.length(); i++)
    DP[0][i] = 0;

  for (int i = 1; i <= (int)s.length(); i++) {
    for (int j = 1; j <= (int)t.length(); j++) {
      if (s[i - 1] == t[j - 1])
        DP[i][j] = DP[i - 1][j - 1] + 1;
      else
        DP[i][j] = std::max(DP[i - 1][j], DP[i][j - 1]);
    }
  }
  int i = s.length(), j = t.length();
  while (i != 0 && j != 0) {
    if (s[i - 1] == t[j - 1]) {
      result = s[i - 1] + result;
      i--;
      j--;
    } else if (DP[i][j] == DP[i - 1][j])
      i = i - 1;
    else
      j = j - 1;
  }
  std::cout << result;
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <iostream>

int main() {
  std::string s, t, result;
  std::cin >> s >> t;
  int DP[3001][3001];

  for (int i = 0; i < (int)s.length(); i++)
    DP[i][0] = 0;
  for (int i = 0; i < (int)t.length(); i++)
    DP[0][i] = 0;

  for (int i = 1; i <= (int)s.length(); i++) {
    for (int j = 1; j <= (int)t.length(); j++) {
      if (s[i - 1] == t[j - 1])
        DP[i][j] = DP[i - 1][j - 1] + 1;
      else
        DP[i][j] = std::max(DP[i - 1][j], DP[i][j - 1]);
    }
  }
  int i = s.length(), j = t.length();
  while (i != 0 && j != 0) {
    if (s[i - 1] == t[j - 1]) {
      result = s[i - 1] + result;
      i--;
      j--;
    } else if (DP[i][j] == DP[i - 1][j])
      i = i - 1;
    else
      j = j - 1;
  }
  std::cout << result;
  return 0;
}
Problem p03165 (C++)

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

int main() {
  string s, t;
  cin >> s >> t;

  vector<vector<ll>> dp(s.size() + 1, vector<ll>(t.size() + 1, 0));
  vector<vector<pair<ll, ll>>> pre(
      s.size() + 1, vector<pair<ll, ll>>(t.size() + 1, {-1, -1}));

  ll max_value = -1;
  for (ll i = 0; i < s.size(); i++) {
    for (ll j = 0; j < t.size(); j++) {
      if (s[i] == t[j]) {
        if (dp[i][j] + 1 > dp[i + 1][j + 1]) {
          max_value = max(max_value, dp[i + 1][j + 1] = dp[i][j] + 1);
          pre[i + 1][j + 1] = {i, j};
        }
      }

      if (dp[i][j] > dp[i + 1][j]) {
        dp[i + 1][j] = dp[i][j];
        pre[i + 1][j] = {i, j};
      }
      if (dp[i][j] > dp[i][j + 1]) {
        dp[i][j + 1] = dp[i][j];
        pre[i][j + 1] = {i, j};
      }
    }
  }

  string ans;
  for (ll i = 0; i <= s.size(); i++) {
    for (ll j = 0; j <= t.size(); j++) {
      if (dp[i][j] == max_value) {
        while (pre[i][j].first != -1) {
          auto p = pre[i][j];
          if (0 <= p.first && p.first < i && 0 <= p.second && p.second < j &&
              s[p.first] == t[p.second]) {
            ans += s[p.first];
            max_value--;
          }
          i = p.first;
          j = p.second;
        }
        i = s.size() + 1;
        j = t.size() + 1;
      }
    }
  }

  assert(max_value == 0);
  reverse(ans.begin(), ans.end());
  cout << ans << endl;
}

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

int main() {
  string s, t;
  cin >> s >> t;

  vector<vector<ll>> dp(s.size() + 1, vector<ll>(t.size() + 1, 0));
  vector<vector<pair<ll, ll>>> pre(
      s.size() + 1, vector<pair<ll, ll>>(t.size() + 1, {-1, -1}));

  ll max_value = -1;
  for (ll i = 0; i < s.size(); i++) {
    for (ll j = 0; j < t.size(); j++) {
      if (s[i] == t[j]) {
        if (dp[i][j] + 1 > dp[i + 1][j + 1]) {
          max_value = max(max_value, dp[i + 1][j + 1] = dp[i][j] + 1);
          pre[i + 1][j + 1] = {i, j};
        }
      }

      if (dp[i][j] > dp[i + 1][j]) {
        dp[i + 1][j] = dp[i][j];
        pre[i + 1][j] = {i, j};
      }
      if (dp[i][j] > dp[i][j + 1]) {
        dp[i][j + 1] = dp[i][j];
        pre[i][j + 1] = {i, j};
      }
    }
  }

  string ans;
  for (ll i = 0; i <= s.size(); i++) {
    for (ll j = 0; j <= t.size(); j++) {
      if (dp[i][j] == max_value) {
        while (pre[i][j].first != -1) {
          auto p = pre[i][j];
          if (0 <= p.first && p.first < i && 0 <= p.second && p.second < j &&
              s[p.first] == t[p.second]) {
            ans += s[p.first];
            max_value--;
          }
          i = p.first;
          j = p.second;
        }
        i = s.size() + 1;
        j = t.size() + 1;
      }
    }
  }

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

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

using namespace std;

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

int main() {
  string s, t;
  cin >> s;
  cin >> t;

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

  string res = "";

  int i = s.length(), j = t.length();
  while (i > 0 && j > 0) {
    if (dp[i][j] == dp[i - 1][j]) {
      i--;
    } else if (dp[i][j] == dp[i][j - 1]) {
      j--;
    } else {
      res = s[i - 1] + res;
      i--;
      j--;
    }
  }

  cout << res << endl;

  return 0;
}

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

using namespace std;

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

int main() {
  string s, t;
  cin >> s;
  cin >> t;

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

  string res = "";

  int i = s.length(), j = t.length();
  while (i > 0 && j > 0) {
    if (dp[i][j] == dp[i - 1][j]) {
      i--;
    } else if (dp[i][j] == dp[i][j - 1]) {
      j--;
    } else {
      res = s[i - 1] + res;
      i--;
      j--;
    }
  }

  cout << res << endl;

  return 0;
}
Problem p03165 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
#include <cstring>
using namespace std;
int f[3005][3005], l1, l2, p[3005][3005];
char s1[3005], s2[3005];
inline void print(int x, int y) {
  if (!x && !y)
    return;
  if (p[x][y] == 1) {
    print(x - 1, y - 1);
    printf("%c", s1[x]);
  } else if (p[x][y] == 3)
    print(x, y - 1);
  else
    print(x - 1, y);
}
int main() {
  scanf("%s%s", s1 + 1, s2 + 1);
  l1 = strlen(s1 + 1), l2 = strlen(s2 + 1);
  for (int i = 1; i <= l1; i++)
    for (int j = 1; j <= l2; j++) {
      if (s1[i] == s2[j])
        f[i][j] = f[i - 1][j - 1] + 1, p[i][j] = 1;
      if (f[i - 1][j] > f[i][j])
        f[i][j] = f[i - 1][j], p[i][j] = 2;
      if (f[i][j - 1] > f[i][j])
        f[i][j] = f[i][j - 1], p[i][j] = 3;
    }
  print(l1, l2);
}

Fixed submission:
#include <algorithm>
#include <cstdio>
#include <cstring>
using namespace std;
int f[3005][3005], l1, l2, p[3005][3005];
char s1[3005], s2[3005];
inline void print(int x, int y) {
  if (!x && !y)
    return;
  if (p[x][y] == 1) {
    print(x - 1, y - 1);
    printf("%c", s1[x]);
  } else if (p[x][y] == 3)
    print(x, y - 1);
  else
    print(x - 1, y);
}
int main() {
  scanf("%s%s", s1 + 1, s2 + 1);
  l1 = strlen(s1 + 1), l2 = strlen(s2 + 1);
  for (int i = 1; i <= l1; i++)
    p[i][0] = 2;
  for (int j = 1; j <= l2; j++)
    p[0][j] = 3;
  for (int i = 1; i <= l1; i++)
    for (int j = 1; j <= l2; j++) {
      if (s1[i] == s2[j])
        f[i][j] = f[i - 1][j - 1] + 1, p[i][j] = 1;
      if (f[i - 1][j] > f[i][j])
        f[i][j] = f[i - 1][j], p[i][j] = 2;
      if (f[i][j - 1] > f[i][j])
        f[i][j] = f[i][j - 1], p[i][j] = 3;
    }
  print(l1, l2);
}
Problem p03165 (C++)

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

int main() {
  string s1, s2, ans = "";
  cin >> s1 >> s2;
  int l1 = s1.size(), l2 = s2.size();
  int dp[l2 + 1][l1 + 1];
  for (int i = 0; i < l1 + 1; ++i)
    for (int j = 0; j < l2 + 1; ++j)
      dp[i][j] = 0;
  for (int i = 1; i < l2 + 1; ++i)
    for (int j = 1; j < l1 + 1; ++j)
      if (s2[i - 1] == s1[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 ci = l2, cj = l1;
  while (dp[ci][cj] > 0) {
    if (dp[ci][cj] == dp[ci][cj - 1])
      --cj;
    else if (dp[ci][cj] == dp[ci - 1][cj])
      --ci;
    else {
      ans = s1[cj - 1] + ans;
      --ci;
      --cj;
    }
  }
  cout << ans;
}


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

int main() {
  string s1, s2, ans = "";
  cin >> s1 >> s2;
  int l1 = s1.size(), l2 = s2.size();
  int dp[l2 + 1][l1 + 1];
  for (int i = 0; i < l2 + 1; ++i)
    for (int j = 0; j < l1 + 1; ++j)
      dp[i][j] = 0;
  for (int i = 1; i < l2 + 1; ++i)
    for (int j = 1; j < l1 + 1; ++j)
      if (s2[i - 1] == s1[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 ci = l2, cj = l1;
  while (dp[ci][cj] > 0) {
    if (dp[ci][cj] == dp[ci][cj - 1])
      --cj;
    else if (dp[ci][cj] == dp[ci - 1][cj])
      --ci;
    else {
      ans = s1[cj - 1] + ans;
      --ci;
      --cj;
    }
  }
  cout << ans;
}
Problem p03165 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define int long long int
#define IOS ios_base::sync_with_stdio(false), cin.tie(NULL);
#define fr(i, a, b) for (int i = a; i <= b; i++)
#define rf(j, b, a) for (int j = b; j >= a; j--)
#define frr(k, a, b) for (int k = a; k <= b; k++)
#define mp make_pair
#define pb push_back
#define MOD 1000000007
using namespace std;
typedef pair<int, int> pii;
typedef pair<pii, int> ppii;
signed main() {
  IOS
#ifndef ONLINE_JUDGE
      freopen("in.txt", "r", stdin);
  freopen("out.txt", "w", stdout);
#endif
  string s1, s2;
  cin >> s1;
  cin >> s2;
  int l1 = s1.size();
  int l2 = s2.size();

  int dp[l1 + 1][l2 + 1];
  for (int i = 0; i <= l1; i++) {
    for (int j = 0; j <= l2; j++) {
      if (i == 0 || j == 0) {
        dp[i][j] = 0;
      } else if (s1[i - 1] == s2[j - 1]) {
        dp[i][j] = 1 + dp[i - 1][j - 1];
      } else {
        dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]);
      }
    }
  }

  string ans = "";
  int i = l1, j = l2;

  while (j > 0 && i > 0) {
    if (s1[i - 1] == s2[j - 1]) {
      ans = s1[i - 1] + ans;
      i--;
      j--;
    } else if (dp[i - 1][j] > dp[i][j - 1]) {
      i--;
    } else {
      j--;
    }
  }
  cout << ans << endl;
}


Fixed submission:
#include <bits/stdc++.h>
#define int long long int
#define IOS ios_base::sync_with_stdio(false), cin.tie(NULL);
#define fr(i, a, b) for (int i = a; i <= b; i++)
#define rf(j, b, a) for (int j = b; j >= a; j--)
#define frr(k, a, b) for (int k = a; k <= b; k++)
#define mp make_pair
#define pb push_back
#define MOD 1000000007
using namespace std;
typedef pair<int, int> pii;
typedef pair<pii, int> ppii;
signed main() {
  IOS
      /* #ifndef ONLINE_JUDGE
            freopen("in.txt", "r", stdin);
            freopen("out.txt", "w", stdout);
        #endif*/
      string s1,
      s2;
  cin >> s1;
  cin >> s2;
  int l1 = s1.size();
  int l2 = s2.size();

  int dp[l1 + 1][l2 + 1];
  for (int i = 0; i <= l1; i++) {
    for (int j = 0; j <= l2; j++) {
      if (i == 0 || j == 0) {
        dp[i][j] = 0;
      } else if (s1[i - 1] == s2[j - 1]) {
        dp[i][j] = 1 + dp[i - 1][j - 1];
      } else {
        dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]);
      }
    }
  }

  string ans = "";
  int i = l1, j = l2;

  while (j > 0 && i > 0) {
    if (s1[i - 1] == s2[j - 1]) {
      ans = s1[i - 1] + ans;
      i--;
      j--;
    } else if (dp[i - 1][j] > dp[i][j - 1]) {
      i--;
    } else {
      j--;
    }
  }
  cout << ans << endl;
}
Problem p03165 (C++)

Buggy submission (status: Runtime Error):
/*

Coded by peyha X

*/

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

const int NMAX = 1e3 + 1, AUCUN = 0;
string s, t;
char used[NMAX][NMAX];
int n, m, dp[NMAX][NMAX];
pair<int, int> last[NMAX][NMAX];

void retrieve(int k, int l) {
  if (k == 0 || l == 0) {
    return;
  }

  int nextK = last[k][l].first, nextL = last[k][l].second;
  retrieve(nextK, nextL);
  if (used[k][l] != AUCUN) {
    cout << used[k][l];
  }
}

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

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

  for (int k = 0; k <= n; k++) {
    used[k][0] = AUCUN;
  }
  for (int l = 0; l <= m; l++) {
    used[0][l] = AUCUN;
  }

  for (int k = 1; k <= n; k++) {
    for (int l = 1; l <= m; l++) {

      // cout << k << ' ' << l << endl;
      if (dp[k - 1][l] > dp[k][l]) {
        dp[k][l] = dp[k - 1][l];
        used[k][l] = AUCUN;
        last[k][l] = make_pair(k - 1, l);
      }
      if (dp[k][l - 1] > dp[k][l]) {
        dp[k][l] = dp[k][l - 1];
        used[k][l] = AUCUN;
        last[k][l] = make_pair(k, l - 1);
      }
      if (s[k - 1] == t[l - 1] && dp[k - 1][l - 1] + 1 > dp[k][l]) {
        dp[k][l] = dp[k - 1][l - 1] + 1;
        used[k][l] = s[k - 1];
        last[k][l] = make_pair(k - 1, l - 1);
      }
    }
  }

  retrieve(n, m);
  cout << '\n';
}


Fixed submission:
/*

Coded by peyha X

*/

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

const int NMAX = 3 * 1e3 + 2, AUCUN = 0;
string s, t;
char used[NMAX][NMAX];
int n, m, dp[NMAX][NMAX];
pair<int, int> last[NMAX][NMAX];

void retrieve(int k, int l) {
  if (k == 0 || l == 0) {
    return;
  }

  int nextK = last[k][l].first, nextL = last[k][l].second;
  retrieve(nextK, nextL);
  if (used[k][l] != AUCUN) {
    cout << used[k][l];
  }
}

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

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

  for (int k = 0; k <= n; k++) {
    used[k][0] = AUCUN;
  }
  for (int l = 0; l <= m; l++) {
    used[0][l] = AUCUN;
  }

  for (int k = 1; k <= n; k++) {
    for (int l = 1; l <= m; l++) {

      // cout << k << ' ' << l << endl;
      if (dp[k - 1][l] > dp[k][l]) {
        dp[k][l] = dp[k - 1][l];
        used[k][l] = AUCUN;
        last[k][l] = make_pair(k - 1, l);
      }
      if (dp[k][l - 1] > dp[k][l]) {
        dp[k][l] = dp[k][l - 1];
        used[k][l] = AUCUN;
        last[k][l] = make_pair(k, l - 1);
      }
      if (s[k - 1] == t[l - 1] && dp[k - 1][l - 1] + 1 > dp[k][l]) {
        dp[k][l] = dp[k - 1][l - 1] + 1;
        used[k][l] = s[k - 1];
        last[k][l] = make_pair(k - 1, l - 1);
      }
    }
  }

  retrieve(n, m);
  cout << '\n';
}
Problem p03165 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define forn(i, n) for (int i = 0; i < (int)n; ++i)
#define rfor(i, n) for (int i = n - 1; i >= 0; --i)
#define rmod(x, y) (((x % y) + y) % y)
using namespace std;
typedef vector<int>::iterator vit;
typedef unsigned int uint;
typedef unsigned short ushort;
typedef long long ll;
const int MAXN = 1024;

// OPERATORS

// GLOBALS

// FUNCTIONS
int arr[3001][3001];

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  string a, b;
  cin >> a >> b;
  for (int i = 0; i < 3001; i++) {
    arr[0][i] = 0;
  }
  for (int i = 0; i < 3001; i++) {
    arr[i][0] = 0;
  }
  // 1 \ 2| 3 _
  for (int i = 0; i < a.size(); i++) {
    for (int j = 0; j < b.size(); j++) {
      if (a[i] == b[j]) {
        arr[i + 1][j + 1] = arr[i][j] + 1;
      } else {
        arr[i + 1][j + 1] = max(arr[i][j + 1], arr[i + 1][j]);
      }
    }
  }
  int ii, jj;
  int mx = 0;
  for (int i = 0; i < a.size() + 1; i++) {
    for (int j = 0; j < b.size() + 1; j++) {
      if (arr[i + 1][j + 1] > mx) {
        ii = i + 1;
        jj = j + 1;
        mx = arr[i + 1][j + 1];
      }
    }
  }
  string f = "";

  while (arr[ii][jj]) {
    if (a[ii - 1] == b[jj - 1]) {
      f += a[ii - 1];
      ii--;
      jj--;
    } else {

      if (arr[ii - 1][jj] > arr[ii][jj - 1]) {
        ii = ii - 1;
      } else {
        jj = jj - 1;
      }
    }
  }
  reverse(f.begin(), f.end());
  cout << f << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define forn(i, n) for (int i = 0; i < (int)n; ++i)
#define rfor(i, n) for (int i = n - 1; i >= 0; --i)
#define rmod(x, y) (((x % y) + y) % y)
using namespace std;
typedef vector<int>::iterator vit;
typedef unsigned int uint;
typedef unsigned short ushort;
typedef long long ll;
const int MAXN = 1024;

// OPERATORS

// GLOBALS

// FUNCTIONS
int arr[3001][3001];

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  string a, b;
  cin >> a >> b;
  for (int i = 0; i < 3001; i++) {
    arr[0][i] = 0;
  }
  for (int i = 0; i < 3001; i++) {
    arr[i][0] = 0;
  }
  // 1 \ 2| 3 _
  for (int i = 0; i < a.size(); i++) {
    for (int j = 0; j < b.size(); j++) {
      if (a[i] == b[j]) {
        arr[i + 1][j + 1] = arr[i][j] + 1;
      } else {
        arr[i + 1][j + 1] = max(arr[i][j + 1], arr[i + 1][j]);
      }
    }
  }
  int ii, jj;
  int mx = 0;
  for (int i = 0; i < a.size(); i++) {
    for (int j = 0; j < b.size(); j++) {
      if (arr[i + 1][j + 1] > mx) {
        ii = i + 1;
        jj = j + 1;
        mx = arr[i + 1][j + 1];
      }
    }
  }
  string f = "";

  while (arr[ii][jj]) {
    if (a[ii - 1] == b[jj - 1]) {
      f += a[ii - 1];
      ii--;
      jj--;
    } else {

      if (arr[ii - 1][jj] > arr[ii][jj - 1]) {
        ii = ii - 1;
      } else {
        jj = jj - 1;
      }
    }
  }
  reverse(f.begin(), f.end());
  cout << f << endl;
  return 0;
}
Problem p03165 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
typedef long long ll;
using namespace std;
#define MX 1003
int dp[MX][MX];
int main() {
  string s, t;
  cin >> s >> t;
  for (int i = 1; i <= s.size(); ++i) {
    for (int j = 1; j <= t.size(); ++j) {
      if (s[i - 1] == t[j - 1])
        dp[i][j] = dp[i - 1][j - 1] + 1;
      else
        dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]);
    }
  }
  stack<int> st;
  int i = s.size();
  int j = t.size();
  while (dp[i][j] != 0) {
    if (dp[i][j] == dp[i - 1][j])
      i = i - 1;
    else if (dp[i][j] == dp[i][j - 1])
      j = j - 1;
    else {
      st.push(i);
      i = i - 1;
      j = j - 1;
    }
  }
  while (!st.empty())
    cout << s[st.top() - 1], st.pop();
  cout << endl;
}


Fixed submission:
#include <bits/stdc++.h>
typedef long long ll;
using namespace std;
#define MX 3003
int dp[MX][MX];
int main() {
  string s, t;
  cin >> s >> t;
  for (int i = 1; i <= s.size(); ++i) {
    for (int j = 1; j <= t.size(); ++j) {
      if (s[i - 1] == t[j - 1])
        dp[i][j] = dp[i - 1][j - 1] + 1;
      else
        dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]);
    }
  }
  stack<int> st;
  int i = s.size();
  int j = t.size();
  while (dp[i][j] != 0) {
    if (dp[i][j] == dp[i - 1][j])
      i = i - 1;
    else if (dp[i][j] == dp[i][j - 1])
      j = j - 1;
    else {
      st.push(i);
      i = i - 1;
      j = j - 1;
    }
  }
  while (!st.empty())
    cout << s[st.top() - 1], st.pop();
  cout << endl;
}
Problem p03165 (C++)

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

using namespace std;

int main() {
  string s1, s2;
  cin >> s1 >> s2;
  int m = s1.length(), n = s2.length();
  int lookup[m + 1][n + 1];
  memset(lookup, 0, sizeof lookup);
  int i, j;
  for (i = 1; i <= m; i++) {
    for (j = 1; j <= n; j++) {
      if (s1[i - 1] == s2[j - 1])
        lookup[i][j] = 1 + lookup[i - 1][j - 1];
      else
        lookup[i][j] = max(lookup[i][j - 1], lookup[i - 1][j]);
    }
  }

  // printf("%d\n", lookup[m][n]);

  int count = 0;
  i = m, j = n;
  string ans = "";
  while (count <= lookup[m][n]) {
    if (s1[i - 1] == s2[j - 1]) {
      ans += s1[i - 1];
      i--;
      j--;
      count++;
    } else {
      if (lookup[i][j - 1] > lookup[i - 1][j])
        j--;
      else
        i--;
    }
  }
  reverse(ans.begin(), ans.end());
  cout << ans;
  return 0;
}

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

using namespace std;

int main() {
  string s1, s2;
  cin >> s1 >> s2;
  int m = s1.length(), n = s2.length();
  int lookup[m + 1][n + 1];
  memset(lookup, 0, sizeof lookup);
  int i, j;
  for (i = 1; i <= m; i++) {
    for (j = 1; j <= n; j++) {
      if (s1[i - 1] == s2[j - 1])
        lookup[i][j] = 1 + lookup[i - 1][j - 1];
      else
        lookup[i][j] = max(lookup[i][j - 1], lookup[i - 1][j]);
    }
  }

  // printf("%d\n", lookup[m][n]);

  int count = 0;
  i = m, j = n;
  string ans = "";
  while (count < lookup[m][n]) {
    if (s1[i - 1] == s2[j - 1]) {
      ans += s1[i - 1];
      i--;
      j--;
      count++;
    } else {
      if (lookup[i][j - 1] > lookup[i - 1][j])
        j--;
      else
        i--;
    }
  }
  reverse(ans.begin(), ans.end());
  cout << ans;
  return 0;
}