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

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define pb push_back
#define int long long
#define MOD 998244353
using namespace std;
int32_t main() {
  string a, b;
  cin >> a >> b;
  int m = a.length();
  int n = b.length();
  int dp[300][300] = {0};
  for (int i = 1; i <= m; i++) {
    for (int j = 1; j <= n; j++) {
      if (a[i - 1] == b[j - 1])
        dp[i][j] = 1 + dp[i - 1][j - 1];
      else
        dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]);
    }
  }
  string ans;
  int i = m, j = n;
  while (i > 0 && j > 0) {
    if (a[i - 1] == b[j - 1]) {
      ans += a[i - 1];
      i--;
      j--;
    } else if (dp[i][j] == dp[i - 1][j])
      i--;
    else
      j--;
  }
  reverse(ans.begin(), ans.end());
  cout << ans;
}


Fixed submission:
#include <bits/stdc++.h>
#define pb push_back
#define int long long
#define MOD 998244353
using namespace std;
int32_t main() {
  string a, b;
  cin >> a >> b;
  int m = a.length();
  int n = b.length();
  int dp[3001][3001] = {0};
  for (int i = 1; i <= m; i++) {
    for (int j = 1; j <= n; j++) {
      if (a[i - 1] == b[j - 1])
        dp[i][j] = 1 + dp[i - 1][j - 1];
      else
        dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]);
    }
  }
  string ans;
  int i = m, j = n;
  while (i > 0 && j > 0) {
    if (a[i - 1] == b[j - 1]) {
      ans += a[i - 1];
      i--;
      j--;
    } else if (dp[i][j] == dp[i - 1][j])
      i--;
    else
      j--;
  }
  reverse(ans.begin(), ans.end());
  cout << ans;
}
Problem p03165 (C++)

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

using namespace std;

long long dp[3005][3005];

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

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

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

  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;
      dp[i][j] = max(dp[i][j], max(dp[i - 1][j], dp[i][j - 1]));
    }

  string ans;
  int x = n, y = m;

  while (x > 0 || y > 0)
    if (s[x - 1] == t[y - 1] && dp[x - 1][y - 1] + 1 == dp[x][y]) {
      ans += s[x - 1];
      x--;
      y--;
    } else if (dp[x - 1][y] == dp[x][y])
      x--;
    else
      y--;

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


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

using namespace std;

long long dp[3005][3005];

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

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

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

  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;
      dp[i][j] = max(dp[i][j], max(dp[i - 1][j], dp[i][j - 1]));
    }

  string ans;
  int x = n, y = m;

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

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

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <iostream>
#include <math.h>
#include <string>
#include <vector>
#define ll long long int
using namespace std;

int main() {
  string a, b;
  cin >> a >> b;
  ll aa, bb;
  // cout<<1;
  aa = a.size();
  bb = b.size();
  // cout<<2;
  vector<vector<ll>> arr(aa + 1, vector<ll>(bb + 1, 0));
  for (ll i = 0; i <= aa; i++) {
    for (ll j = 0; j <= bb; j++) {
      if (i == 0 || j == 0) {
        arr[i][j] = 0;
        //  cout<<arr[i][j]<<" ";
        continue;
      }

      if (a[i - 1] == b[j - 1])
        arr[i][j] =
            max(arr[i - 1][j - 1] + 1, max(arr[i - 1][j], arr[i][j - 1]));
      else
        arr[i][j] = max(arr[i - 1][j - 1], max(arr[i - 1][j], arr[i][j - 1]));
      //   cout<<arr[i][j]<<" ";
    }
    // cout<<endl;
  }
  vector<char> ans;
  ll j = bb;
  ll i = aa;
  while (1) {
    if (i == 0)
      break;
    if (j == 0)
      break;
    while (arr[i][j] == arr[i - 1][j] && i != 0)
      i--;
    // cout<<i<<endl;
    ans.push_back(a[i - 1]);
    i--;
    // cout<<a[i]<<endl;
    if (i == 0)
      break;
    if (j == 0)
      break;
    while (arr[i][j] == arr[i][j - 1] && j >= 1)
      j--;
    // cout<<" hello :"<<j<<endl;
    // cout<<"check i "<<i<<endl;
    if (i == 0)
      break;
    if (j == 0)
      break;
  }
  for (ll l = ans.size() - 1; l >= 0; l--)
    cout << ans[l];
  // cin>>a;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <iostream>
#include <math.h>
#include <string>
#include <vector>
#define ll long long int
using namespace std;

int main() {
  string a, b;
  cin >> a >> b;
  ll aa, bb;
  // cout<<1;
  aa = a.size();
  bb = b.size();
  // cout<<2;
  vector<vector<ll>> arr(aa + 1, vector<ll>(bb + 1, 0));
  for (ll i = 0; i <= aa; i++) {
    for (ll j = 0; j <= bb; j++) {
      if (i == 0 || j == 0) {
        arr[i][j] = 0;
        //  cout<<arr[i][j]<<" ";
        continue;
      }

      if (a[i - 1] == b[j - 1])
        arr[i][j] =
            max(arr[i - 1][j - 1] + 1, max(arr[i - 1][j], arr[i][j - 1]));
      else
        arr[i][j] = max(arr[i - 1][j - 1], max(arr[i - 1][j], arr[i][j - 1]));
      //   cout<<arr[i][j]<<" ";
    }
    // cout<<endl;
  }
  if (arr[aa][bb] == 0)
    return 0;
  vector<char> ans;
  ll j = bb;
  ll i = aa;
  while (1) {
    if (i == 0)
      break;
    if (j == 0)
      break;
    while (arr[i][j] == arr[i - 1][j] && i != 0)
      i--;
    // cout<<i<<endl;
    ans.push_back(a[i - 1]);
    i--;
    // cout<<a[i]<<endl;
    if (i == 0)
      break;
    if (j == 0)
      break;
    while (arr[i][j] == arr[i][j - 1] && j >= 1)
      j--;
    // cout<<" hello :"<<j<<endl;
    // cout<<"check i "<<i<<endl;
    if (i == 0)
      break;
    if (j == 0)
      break;
  }
  for (ll l = ans.size() - 1; l >= 0; l--)
    cout << ans[l];
  // cin>>a;
  return 0;
}
Problem p03165 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define LL long long
using namespace std;
int dp[1005][1005];
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  memset(dp, 0, sizeof(dp));
  string s, t;
  cin >> s;
  cin >> t;
  for (int i = 0; i <= s.size(); ++i)
    for (int j = 0; j <= t.size(); ++j) {
      if (i == 0 || j == 0)
        dp[i][j] = 0;
      else if (s[i - 1] == t[j - 1])
        dp[i][j] = dp[i - 1][j - 1] + 1;
      else
        dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]);
    }
  int a = s.size(), b = t.size();
  string ans;
  while (a && b) {
    if (s[a - 1] == t[b - 1]) {
      ans.push_back(s[a - 1]);
      a--;
      b--;
    } else if (dp[a - 1][b] > dp[a][b - 1])
      a--;
    else
      b--;
  }
  reverse(ans.begin(), ans.end());
  cout << ans << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define LL long long
using namespace std;
int dp[3005][3005];
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  memset(dp, 0, sizeof(dp));
  string s, t;
  cin >> s;
  cin >> t;
  for (int i = 0; i <= s.size(); ++i)
    for (int j = 0; j <= t.size(); ++j) {
      if (i == 0 || j == 0)
        dp[i][j] = 0;
      else if (s[i - 1] == t[j - 1])
        dp[i][j] = dp[i - 1][j - 1] + 1;
      else
        dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]);
    }
  int a = s.size(), b = t.size();
  string ans;
  while (a && b) {
    if (s[a - 1] == t[b - 1]) {
      ans.push_back(s[a - 1]);
      a--;
      b--;
    } else if (dp[a - 1][b] > dp[a][b - 1])
      a--;
    else
      b--;
  }
  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;

const int INF = 1e9 + 5;
const int N = 1e5;
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);

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

  vector<vector<int>> dp(s.size() + 1, vector<int>(t.size() + 1));
  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] = 1 + dp[i - 1][j - 1];
      dp[i][j] = max(dp[i][j], max(dp[i - 1][j], dp[i][j - 1]));
    }
  }

  stack<char> ans;
  int ans_size = dp[s.size()][t.size()];
  pair<int, int> curr = {s.size(), t.size()};
  while (ans_size > 0) {
    int curr_val = dp[curr.first][curr.second];
    int prev_val = dp[curr.first - 1][curr.second - 1];

    if (prev_val + 1 == curr_val && !((curr.first == 1) ^ (curr.second == 1))) {
      ans.push(s[curr.first - 1]);
      ans_size--;
      curr = {curr.first - 1, curr.second - 1};
      continue;
    }

    prev_val = dp[curr.first - 1][curr.second];
    if (prev_val == curr_val)
      curr = {curr.first - 1, curr.second};
    else
      curr = {curr.first, curr.second - 1};
  }
  while (!ans.empty()) {
    cout << ans.top();
    ans.pop();
  }
  cout << "\n";
}

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

const int INF = 1e9 + 5;
const int N = 1e5;
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);

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

  vector<vector<int>> dp(s.size() + 1, vector<int>(t.size() + 1));
  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] = 1 + dp[i - 1][j - 1];
      dp[i][j] = max(dp[i][j], max(dp[i - 1][j], dp[i][j - 1]));
    }
  }

  stack<char> ans;
  int ans_size = dp[s.size()][t.size()];
  pair<int, int> curr = {s.size(), t.size()};
  while (ans_size > 0) {
    int curr_val = dp[curr.first][curr.second];
    int prev_val = dp[curr.first - 1][curr.second - 1];

    if (prev_val + 1 == curr_val && s[curr.first - 1] == t[curr.second - 1]) {
      ans.push(s[curr.first - 1]);
      ans_size--;
      curr = {curr.first - 1, curr.second - 1};
      continue;
    }

    prev_val = dp[curr.first - 1][curr.second];
    if (prev_val == curr_val)
      curr = {curr.first - 1, curr.second};
    else
      curr = {curr.first, curr.second - 1};
  }
  while (!ans.empty()) {
    cout << ans.top();
    ans.pop();
  }
  cout << "\n";
}
Problem p03165 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>
using namespace std;
#define forr(i, m, n) for (int i = (m); i <= (n); i++)
#define DBG 0

#define MAXN 3001
string z[MAXN][MAXN];

int main() {
  size_t ns, nt;
  int cnt;
  string s, t, str;
  cin >> s >> t;
  ns = s.size();
  nt = t.size();
  if (DBG) {
    cout << "s:" << s << " t:" << t << " nst:" << ns << " " << nt << endl;
  }
  str = "";
  forr(j, 0, nt - 1) {
    if (s[0] == t[j])
      str = string(1, s[0]);
    z[0][j] = str;
  }
  forr(i, 1, ns - 1) {
    z[i][0] = (s[i] == t[0] ? string(1, s[i]) : "");
    forr(j, 1, nt - 1) {
      string cat, cat2;
      str = z[i - 1][j - 1] + (s[i] == t[j] ? string(1, s[i]) : "");
      cat =
          (z[i - 1][j].size() > z[i][j - 1].size() ? z[i - 1][j] : z[i][j - 1]);
      z[i][j] = (cat.size() > str.size() ? cat : str);
    }
  }
  cout << z[ns - 1][nt - 1];
}


Fixed submission:
#include <iostream>
using namespace std;
#define forr(i, m, n) for (int i = (m); i <= (n); i++)
#define DBG 0

#define MAXN 3001
string z[MAXN][MAXN];

int main() {
  size_t ns, nt;
  int cnt;
  string s, t, str;
  cin >> s >> t;
  ns = s.size();
  nt = t.size();
  if (DBG) {
    cout << "s:" << s << " t:" << t << " nst:" << ns << " " << nt << endl;
  }
  str = "";
  forr(j, 0, nt - 1) {
    if (s[0] == t[j])
      str = string(1, s[0]);
    z[0][j] = str;
  }
  forr(i, 1, ns - 1) {
    z[i][0] = (s[i] == t[0] ? string(1, s[i]) : "");
    forr(j, 1, nt - 1) {
      int s1, s2, s3;
      s1 = z[i - 1][j].size();
      s2 = z[i][j - 1].size();
      s3 = z[i - 1][j - 1].size() + (s[i] == t[j] ? 1 : 0);
      if (s1 >= s2 && s1 >= s3)
        z[i][j] = z[i - 1][j];
      else if (s2 >= s1 && s2 >= s3)
        z[i][j] = z[i][j - 1];
      else
        z[i][j] = z[i - 1][j - 1] + (s[i] == t[j] ? string(1, s[i]) : "");
    }
  }
  cout << z[ns - 1][nt - 1];
}
Problem p03165 (C++)

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

#define ll long long int
#define vi vector<int>
#define ii pair<int, int>
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define pll pair<ll, ll>
#define vv vector

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif

  int n, m;
  string s1, s2;
  cin >> s1 >> s2;
  n = s1.size(), m = s2.size();
  int dp[n + 1][m + 1];
  for (int i = 0; i <= n; i++) {
    for (int j = 0; j <= m; j++) {
      if (i == 0 || j == 0)
        dp[i][j] = 0;
      else if (s1[i - 1] == s2[j - 1])
        dp[i][j] = dp[i - 1][j - 1] + 1;
      else
        dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]);
    }
  }
  string ans = "";
  /*for(int i=0;i<=n;i++){
      for(int j=0;j<=m;j++)
          cout<<dp[i][j]<<' ';
      cout<<'\n';
  }*/
  int i = n, j = m;
  while (i || j) {
    if (dp[i - 1][j] != dp[i][j] && dp[i][j - 1] != dp[i][j])
      ans = s1[i - 1] + ans, i--, j--;
    else if (dp[i - 1][j] == dp[i][j])
      i--;
    else
      j--;
  }
  cout << ans;
}

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

#define ll long long int
#define vi vector<int>
#define ii pair<int, int>
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define pll pair<ll, ll>
#define vv vector

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif

  int n, m;
  string s1, s2;
  cin >> s1 >> s2;
  n = s1.size(), m = s2.size();
  int dp[n + 1][m + 1];
  for (int i = 0; i <= n; i++) {
    for (int j = 0; j <= m; j++) {
      if (i == 0 || j == 0)
        dp[i][j] = 0;
      else if (s1[i - 1] == s2[j - 1])
        dp[i][j] = dp[i - 1][j - 1] + 1;
      else
        dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]);
    }
  }
  string ans = "";
  /*for(int i=0;i<=n;i++){
      for(int j=0;j<=m;j++)
          cout<<dp[i][j]<<' ';
      cout<<'\n';
  }*/
  int i = n, j = m;
  while (i > 0 && j > 0) {
    if (dp[i - 1][j] != dp[i][j] && dp[i][j - 1] != dp[i][j])
      ans = s1[i - 1] + ans, i--, j--;
    else if (dp[i - 1][j] == dp[i][j])
      i--;
    else
      j--;
  }
  cout << ans;
}
Problem p03165 (C++)

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

#define ll long long

int 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);

  string a, b;
  cin >> a >> b;
  ll i, j, m = a.length(), n = b.length();
  int dp[m + 1][n + 1];
  memset(dp, 0, sizeof dp);
  for (i = 1; i <= m; i++) {
    for (j = 1; j <= n; 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]);
    }
  }
  // cout<<dp[m][n];
  i = m;
  j = n;
  vector<char> out;
  while (i > 0 && j > 0 && dp[i][j] > 0) {
    if (dp[i][j] == dp[i - 1][j])
      i--;
    else if (dp[i][j] == dp[i][j - 1])
      j--;
    else {
      i--;
      j--;
      out.push_back(a[i]);
    }
  }
  for (i = out.size() - 1; i >= 0; i--)
    cout << out[i];
  return 0;
}

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

#define ll long long

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

  string a, b;
  cin >> a >> b;
  ll i, j, m = a.length(), n = b.length();
  int dp[m + 1][n + 1];
  memset(dp, 0, sizeof dp);
  for (i = 1; i <= m; i++) {
    for (j = 1; j <= n; 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]);
    }
  }
  // cout<<dp[m][n];
  i = m;
  j = n;
  vector<char> out;
  while (i > 0 && j > 0 && dp[i][j] > 0) {
    if (dp[i][j] == dp[i - 1][j])
      i--;
    else if (dp[i][j] == dp[i][j - 1])
      j--;
    else {
      i--;
      j--;
      out.push_back(a[i]);
    }
  }
  for (i = out.size() - 1; i >= 0; i--)
    cout << out[i];
  return 0;
}
Problem p03165 (C++)

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

int main() {
  string s, t;
  cin >> s >> t;
  s = " " + s;
  t = " " + t;
  int dp[110][110] = {0};
  for (int y = 0; y < (int)s.size(); y++) {
    for (int x = 0; x < (int)t.size(); x++) {
      if (y == 0 || x == 0)
        continue;
      if (s[y] == t[x]) {
        dp[y][x] = dp[y - 1][x - 1] + 1;
      } else {
        dp[y][x] = max(dp[y - 1][x], dp[y][x - 1]);
      }
    }
  }
  int length = dp[(int)s.size() - 1][(int)t.size() - 1];
  string ans = "";
  int itrx = (int)t.size() - 1, itry = (int)s.size() - 1;
  while (length) {
    if (s[itry] == t[itrx]) {
      ans += s[itry];
      length--;
      itry--;
      itrx--;
    } else if (dp[itry][itrx] == dp[itry - 1][itrx]) {
      itry--;
    } else if (dp[itry][itrx] == dp[itry][itrx - 1]) {
      itrx--;
    }
  }
  reverse(ans.begin(), ans.end());
  cout << ans << endl;
}


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

int main() {
  string s, t;
  cin >> s >> t;
  s = " " + s;
  t = " " + t;
  int dp[3010][3010] = {0};
  for (int y = 0; y < (int)s.size(); y++) {
    for (int x = 0; x < (int)t.size(); x++) {
      if (y == 0 || x == 0)
        continue;
      if (s[y] == t[x]) {
        dp[y][x] = dp[y - 1][x - 1] + 1;
      } else {
        dp[y][x] = max(dp[y - 1][x], dp[y][x - 1]);
      }
    }
  }
  int length = dp[(int)s.size() - 1][(int)t.size() - 1];
  string ans = "";
  int itrx = (int)t.size() - 1, itry = (int)s.size() - 1;
  while (length) {
    if (s[itry] == t[itrx]) {
      ans += s[itry];
      length--;
      itry--;
      itrx--;
    } else if (dp[itry][itrx] == dp[itry - 1][itrx]) {
      itry--;
    } else if (dp[itry][itrx] == dp[itry][itrx - 1]) {
      itrx--;
    }
  }
  reverse(ans.begin(), ans.end());
  cout << ans << endl;
}
Problem p03165 (C++)

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

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<long, long> pll;
typedef vector<int> vi;
typedef vector<long long> vll;

#define forn(i, n) for (int i = 0; i < n; i++)
#define mp makepair
#define pb push_back

string s, t;
string ans;

int main() {
  cin >> s >> t;
  int dp[200][200] = {0};
  for (int i = 1; i <= s.length(); i++) {
    for (int j = 1; j <= t.length(); j++) {
      if (s[i - 1] == t[j - 1]) {
        dp[i][j] = 1 + dp[i - 1][j - 1];
      } else {
        dp[i][j] = max(dp[i][j - 1], dp[i - 1][j]);
      }
    }
  }
  string ans = "";
  int i = s.length();
  int j = t.length();
  while (i > 0 && j > 0) {
    if (s[i - 1] == t[j - 1]) {
      i--;
      j--;
      ans = t[j] + ans;
    } else if (dp[i - 1][j] > dp[i][j - 1]) {
      i--;
    } else {
      j--;
    }
  }
  cout << ans << endl;
}


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

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<long, long> pll;
typedef vector<int> vi;
typedef vector<long long> vll;

#define forn(i, n) for (int i = 0; i < n; i++)
#define mp makepair
#define pb push_back

string s, t;
string ans;

int main() {
  cin >> s >> t;
  int dp[3001][3001] = {0};
  for (int i = 1; i <= s.length(); i++) {
    for (int j = 1; j <= t.length(); j++) {
      if (s[i - 1] == t[j - 1]) {
        dp[i][j] = 1 + dp[i - 1][j - 1];
      } else {
        dp[i][j] = max(dp[i][j - 1], dp[i - 1][j]);
      }
    }
  }
  string ans = "";
  int i = s.length();
  int j = t.length();
  while (i > 0 && j > 0) {
    if (s[i - 1] == t[j - 1]) {
      i--;
      j--;
      ans = t[j] + ans;
    } else if (dp[i - 1][j] > dp[i][j - 1]) {
      i--;
    } else {
      j--;
    }
  }
  cout << ans << endl;
}
Problem p03165 (C++)

Buggy submission (status: Runtime Error):
#include "bits/stdc++.h"
using namespace std;
#define ll long long int
#define rep(i, n) for (int i = 0; i < n; i++)
#define rrep(i, n) for (int i = n; i >= 0; i--)
#define REP(i, s, t) for (int i = s; i <= t; i++)
#define RREP(i, s, t) for (int i = s; i >= t; i--)
#define dump(x) cerr << #x << " = " << (x) << endl;
#define INF 2000000000
#define mod 1000000007
#define INF2 1000000000000000000

int dp[3010][3010];
int main(void) {
  cin.tie(0);
  ios::sync_with_stdio(false);
  string S, T;
  cin >> S >> T;
  rep(i, S.length()) {
    rep(j, T.length()) {
      if (S[i] == T[j])
        dp[i + 1][j + 1] = dp[i][j] + 1;
      else
        dp[i + 1][j + 1] = max(dp[i + 1][j], dp[i][j + 1]);
    }
  }
  // cout << dp[S.length()][T.length()] << endl;
  int i = S.length(), j = T.length();
  string A;
  while (i > 0 || j > 0) {
    // cout << i << " " << j << " " << dp[i][j] << A << endl;
    if (S[i - 1] == T[j - 1]) {
      A = S[i - 1] + A;
      i--;
      j--;
    } else if (dp[i][j] == dp[i - 1][j])
      i--;
    else
      j--;
  }
  cout << A << endl;

  return 0;
}


Fixed submission:
#include "bits/stdc++.h"
using namespace std;
#define ll long long int
#define rep(i, n) for (int i = 0; i < n; i++)
#define rrep(i, n) for (int i = n; i >= 0; i--)
#define REP(i, s, t) for (int i = s; i <= t; i++)
#define RREP(i, s, t) for (int i = s; i >= t; i--)
#define dump(x) cerr << #x << " = " << (x) << endl;
#define INF 2000000000
#define mod 1000000007
#define INF2 1000000000000000000

int dp[3010][3010];
int main(void) {
  cin.tie(0);
  ios::sync_with_stdio(false);
  string S, T;
  cin >> S >> T;
  rep(i, S.length()) {
    rep(j, T.length()) {
      if (S[i] == T[j])
        dp[i + 1][j + 1] = dp[i][j] + 1;
      else
        dp[i + 1][j + 1] = max(dp[i + 1][j], dp[i][j + 1]);
    }
  }
  // cout << dp[S.length()][T.length()] << endl;
  int i = S.length(), j = T.length();
  string A;
  while (i > 0 && j > 0) {
    // cout << i << " " << j << " " << dp[i][j] << A << endl;
    if (S[i - 1] == T[j - 1]) {
      A = S[i - 1] + A;
      i--;
      j--;
    } else if (dp[i][j] == dp[i - 1][j])
      i--;
    else
      j--;
  }
  cout << A << endl;

  return 0;
}
Problem p03165 (C++)

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

int main(void) {
  string s1;
  cin >> s1;

  string s2;
  cin >> s2;

  int n = s1.size();
  int m = s2.size();

  int dp[n + 1][n + 1];
  memset(dp, 0, sizeof(dp));

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

  string ans;
  while (i > 0 && j > 0) {
    if (s1[i - 1] == s2[j - 1]) {
      ans.push_back(s1[i - 1]);
      i--;
      j--;
    } else if (dp[i - 1][j] > dp[i][j - 1]) {
      i--;
    } else
      j--;
  }

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

  cout << ans;
  return 0;
}

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

int main(void) {
  string s1;
  cin >> s1;

  string s2;
  cin >> s2;

  int n = s1.size();
  int m = s2.size();

  vector<vector<int>> dp;
  for (int i = 0; i < s1.size() + 1; i++) {
    vector<int> temp;
    for (int j = 0; j < s2.size() + 1; j++)
      temp.push_back(0);
    dp.push_back(temp);
  }

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

  string ans;
  while (i > 0 && j > 0) {
    if (s1[i - 1] == s2[j - 1]) {
      ans.push_back(s1[i - 1]);
      i--;
      j--;
    } else if (dp[i - 1][j] > dp[i][j - 1]) {
      i--;
    } else
      j--;
  }

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

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

Buggy submission (status: Time Limit Exceeded):
/* Author- Abhijeet Trigunait */

#include <bits/stdc++.h>
#define lld long long int
#define F first
#define S second
#define P pair<int, int>
#define pb push_back
#define mod 1e9 + 7
#define setbits(x) __builtin_popcountll(x)
#define zerobits(x) __builtin_ctzll(x)
#define gcd(x, y) __gcd(x, y)
#define endl '\n'

using namespace std;
lld dp[3005][3005];

string getStr(string &s1, string &s2, lld len) {
  string lcs;
  lld i = 0, j = 0;
  while (len > 0) {
    if (s1[i] == s2[j]) {
      lcs.pb(s1[i]);
      ++i;
      ++j;
      --len;
    } else {
      if (dp[i][j + 1] > dp[i + 1][j]) {
        ++j;
      } else
        ++i;
    }
  }
  return lcs;
}

lld getLength(string s1, string s2, lld i, lld j) {
  if (i >= s1.length() or j >= s2.length())
    return 0;
  if (dp[i][j] != -1)
    return dp[i][j];

  if (s1[i] == s2[j]) {
    return dp[i][j] = 1 + getLength(s1, s2, i + 1, j + 1);
  } else {
    return dp[i][j] =
               max(getLength(s1, s2, i, j + 1), getLength(s1, s2, i + 1, j));
  }
}

string solve(string s1, string s2) {
  memset(dp, -1, sizeof(dp));
  lld len = getLength(s1, s2, 0, 0);
  return getStr(s1, s2, len);
}

int main() {

  string s1, s2;
  cin >> s1 >> s2;
  cout << solve(s1, s2) << endl;
}

Fixed submission:
/* Author- Abhijeet Trigunait */

#include <bits/stdc++.h>
#define lld long long int
#define F first
#define S second
#define P pair<int, int>
#define pb push_back
#define mod 1e9 + 7
#define setbits(x) __builtin_popcountll(x)
#define zerobits(x) __builtin_ctzll(x)
#define gcd(x, y) __gcd(x, y)
#define endl '\n'

using namespace std;
lld dp[3005][3005];

string getStr(string &s1, string &s2, lld len) {
  string lcs;
  lld i = 0, j = 0;
  while (len > 0) {
    if (s1[i] == s2[j]) {
      lcs.pb(s1[i]);
      ++i;
      ++j;
      --len;
    } else {
      if (dp[i][j + 1] > dp[i + 1][j]) {
        ++j;
      } else
        ++i;
    }
  }
  return lcs;
}

lld getLength(string &s1, string &s2, lld i, lld j) {
  if (i >= s1.length() or j >= s2.length())
    return 0;
  if (dp[i][j] != -1)
    return dp[i][j];

  if (s1[i] == s2[j]) {
    return dp[i][j] = 1 + getLength(s1, s2, i + 1, j + 1);
  } else {
    return dp[i][j] =
               max(getLength(s1, s2, i, j + 1), getLength(s1, s2, i + 1, j));
  }
}

string solve(string s1, string s2) {
  memset(dp, -1, sizeof(dp));
  lld len = getLength(s1, s2, 0, 0);
  return getStr(s1, s2, len);
}

int main() {

  string s1, s2;
  cin >> s1 >> s2;
  cout << solve(s1, s2) << endl;
}
Problem p03165 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
long n, m, w[100000], v[1000000], kq = -5;
long long f[5000][5000];
string s, t;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin >> s >> t;
  s = ' ' + s;
  t = ' ' + t;
  for (long i = 1; i < s.size(); i++) {
    for (long j = 1; j < t.size(); j++) {
      if (s[i] == t[j]) {
        f[i][j] = f[i - 1][j - 1] + 1;
      } else
        f[i][j] = max(f[i - 1][j], f[i][j - 1]);
    }
  }
  string res = "";
  long m = s.size() - 1;
  long n = t.size() - 1;
  while (m > 0 && n > 0) {
    if (s[m] == t[n]) {
      res = s[m] + res;
      m--;
      n--;
    }
    if (f[m][n] == f[m - 1][n]) {
      m--;
    } else
      n--;
  }
  cout << res;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
long n, m, w[100000], v[1000000], kq = -5;
long long f[5000][5000];
string s, t;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin >> s >> t;
  s = ' ' + s;
  t = ' ' + t;
  for (long i = 1; i < s.size(); i++) {
    for (long j = 1; j < t.size(); j++) {
      if (s[i] == t[j]) {
        f[i][j] = f[i - 1][j - 1] + 1;
      } else
        f[i][j] = max(f[i - 1][j], f[i][j - 1]);
    }
  }
  string res = "";
  long m = s.size() - 1;
  long n = t.size() - 1;
  while (m > 0 && n > 0) {
    if (s[m] == t[n]) {
      res = s[m] + res;
      m--;
      n--;
      continue;
    }
    if (f[m][n] == f[m - 1][n]) {
      m--;
    } else
      n--;
  }
  cout << res;
}
Problem p03165 (C++)

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

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

  int dp[m + 1][n + 1][2]; // last [2] here 0 stores value and 1 will store from
                           // the valve came
  // 1 if it came from diagonal
  // 2 if it came from row
  // 3 if it came from column
  // when row and column are same and add 2
  // we may store the the cordinates from where it came by using pair in vector
  int i, j;

  for (i = 0; i <= m; i++)
    dp[i][0][0] = 0;

  for (i = 0; i <= n; i++)
    dp[0][i][0] = 0;

  for (i = 1; i <= m; i++) {
    for (j = 1; j <= n; j++) {
      if (s[i - 1] == t[j - 1]) {
        dp[i][j][0] = dp[i - 1][j - 1][0] + 1;
        dp[i][j][1] = 1;
      } else {
        dp[i][j][0] = max(dp[i - 1][j][0], dp[i][j - 1][0]);
        if (dp[i - 1][j][0] > dp[i][j - 1][0])
          dp[i][j][1] = 3;
        else
          dp[i][j][1] = 2;
      }
    }
  }
  int len = dp[m][n][0];
  char ans[len + 1];

  j = n;
  for (i = m; i > 0;) {
    for (; j > 0;) {
      if (dp[i][j][1] == 1) {
        ans[len--] = t[j - 1];
        i--;
        j--;
      } else if (dp[i][j][1] == 2) {
        j--;
      } else {
        i--;
      }
    }
    if (j <= 0)
      break;
  }

  len = dp[m][n][0];
  for (i = 1; i <= len; i++)
    cout << ans[i];

  return 0;
}

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

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

  int dp[m + 1][n + 1][2]; // last [2] here 0 stores value and 1 will store from
                           // the valve came
  // 1 if it came from diagonal
  // 2 if it came from row
  // 3 if it came from column
  // when row and column are same and add 2
  // we may store the the cordinates from where it came by using pair in vector
  int i, j;

  for (i = 0; i <= m; i++)
    dp[i][0][0] = 0;

  for (i = 0; i <= n; i++)
    dp[0][i][0] = 0;

  for (i = 1; i <= m; i++) {
    for (j = 1; j <= n; j++) {
      if (s[i - 1] == t[j - 1]) {
        dp[i][j][0] = dp[i - 1][j - 1][0] + 1;
        dp[i][j][1] = 1;
      } else {
        dp[i][j][0] = max(dp[i - 1][j][0], dp[i][j - 1][0]);
        if (dp[i - 1][j][0] > dp[i][j - 1][0])
          dp[i][j][1] = 3;
        else
          dp[i][j][1] = 2;
      }
    }
  }
  int len = dp[m][n][0];
  char ans[len + 1];

  j = n;
  for (i = m; i > 0;) {
    for (; j > 0;) {
      if (dp[i][j][1] == 1) {
        ans[len--] = t[j - 1];
        i--;
        j--;
      } else if (dp[i][j][1] == 2) {
        j--;
      } else {
        i--;
      }
      if (i <= 0)
        break;
    }
    if (j <= 0)
      break;
  }

  len = dp[m][n][0];
  for (i = 1; i <= len; i++)
    cout << ans[i];

  return 0;
}
Problem p03165 (C++)

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

using namespace std;

#define ll long long
#define pll pair<ll, ll>
#define F first
#define S second
#define pb push_back
#define ld long double
ll mod = 1000000007;
string ans;
ll func1(vector<vector<pll>> &dp, string s, string t, int a, int b) {
  if (a == 0 || b == 0)
    return 0;
  if (dp[a][b].F != -1)
    return dp[a][b].F;
  if (s[a - 1] == t[b - 1]) {
    // ans.pb(s[a-1]);
    dp[a][b].S = 1;
    return dp[a][b].F = 1 + func1(dp, s, t, a - 1, b - 1);
  }
  ll x = func1(dp, s, t, a - 1, b);
  ll y = func1(dp, s, t, a, b - 1);
  if (x >= y) {
    dp[a][b].S = 2;
    return dp[a][b].F = x;
  }
  dp[a][b].S = 0;
  return dp[a][b].F = y;
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  ll t = 1;
  // cin>>t;
  while (t--) {
    string s, t;
    cin >> s >> t;
    vector<vector<pll>> dp(s.size() + 1, vector<pll>(t.size() + 1, {-1, -1}));
    ll tp = func1(dp, s, t, s.size(), t.size());
    ll x = dp[s.size()][t.size()].S;
    ll i = s.size(), j = t.size();
    while (i * j != 0) {
      if (x == 1) {
        ans.pb(s[i - 1]);
        i--;
        j--;
      }
      if (x == 0)
        j--;
      else if (x == 2)
        i--;
      x = dp[i][j].S;
    }
    reverse(ans.begin(), ans.end());
    cout << ans;
  }
}

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

using namespace std;

#define ll long long
#define pll pair<ll, ll>
#define F first
#define S second
#define pb push_back
#define ld long double
ll mod = 1000000007;
string ans;
ll func1(vector<vector<pll>> &dp, string &s, string &t, int a, int b) {
  if (a == 0 || b == 0)
    return 0;
  if (dp[a][b].F != -1)
    return dp[a][b].F;
  if (s[a - 1] == t[b - 1]) {
    // ans.pb(s[a-1]);
    dp[a][b].S = 1;
    return dp[a][b].F = 1 + func1(dp, s, t, a - 1, b - 1);
  }
  ll x = func1(dp, s, t, a - 1, b);
  ll y = func1(dp, s, t, a, b - 1);
  if (x >= y) {
    dp[a][b].S = 2;
    return dp[a][b].F = x;
  }
  dp[a][b].S = 0;
  return dp[a][b].F = y;
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  ll t = 1;
  // cin>>t;
  while (t--) {
    string s, t;
    cin >> s >> t;
    vector<vector<pll>> dp(s.size() + 1, vector<pll>(t.size() + 1, {-1, -1}));
    ll tp = func1(dp, s, t, s.size(), t.size());
    ll x = dp[s.size()][t.size()].S;
    ll i = s.size(), j = t.size();
    while (i * j != 0) {
      if (x == 1) {
        ans.pb(s[i - 1]);
        i--;
        j--;
      }
      if (x == 0)
        j--;
      else if (x == 2)
        i--;
      x = dp[i][j].S;
    }
    reverse(ans.begin(), ans.end());
    cout << ans;
  }
}
Problem p03165 (C++)

Buggy submission (status: Runtime Error):
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#define N 3000

int flag[N][N], dp[N][N];
char s1[N], s2[N];
void LCS() {
  memset(dp, 0, sizeof(dp));
  memset(flag, 0, sizeof(flag));
  int n = strlen(s1), m = strlen(s2);
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++) {
      if (s1[i - 1] == s2[j - 1])
        dp[i][j] = dp[i - 1][j - 1] + 1, flag[i][j] = 0;
      else if (dp[i - 1][j] >= dp[i][j - 1])
        dp[i][j] = dp[i - 1][j], flag[i][j] = 1;
      else
        dp[i][j] = dp[i][j - 1], flag[i][j] = -1;
    }
}
void PrintLCS(int i, int j) {
  if (i == 0 || j == 0)
    return;
  if (flag[i][j] == 0) {
    PrintLCS(i - 1, j - 1);
    std::cout << s1[i - 1];
  } else if (flag[i][j] == 1)
    PrintLCS(i - 1, j);
  else
    PrintLCS(i, j - 1);
}
int main() {
  std::cin >> s1 >> s2;
  LCS();
  PrintLCS(strlen(s1), strlen(s2));
  return 0;
}


Fixed submission:
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#define N 3001

int flag[N][N], dp[N][N];
char s1[N], s2[N];
void LCS() {
  memset(dp, 0, sizeof(dp));
  memset(flag, 0, sizeof(flag));
  int n = strlen(s1), m = strlen(s2);
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++) {
      if (s1[i - 1] == s2[j - 1])
        dp[i][j] = dp[i - 1][j - 1] + 1, flag[i][j] = 0;
      else if (dp[i - 1][j] >= dp[i][j - 1])
        dp[i][j] = dp[i - 1][j], flag[i][j] = 1;
      else
        dp[i][j] = dp[i][j - 1], flag[i][j] = -1;
    }
}
void PrintLCS(int i, int j) {
  if (i == 0 || j == 0)
    return;
  if (flag[i][j] == 0) {
    PrintLCS(i - 1, j - 1);
    std::cout << s1[i - 1];
  } else if (flag[i][j] == 1)
    PrintLCS(i - 1, j);
  else
    PrintLCS(i, j - 1);
}
int main() {
  std::cin >> s1 >> s2;
  LCS();
  PrintLCS(strlen(s1), strlen(s2));
  return 0;
}
Problem p03165 (C++)

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

  if (s.size() < t.size())
    s.swap(t);

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

  int a = s.size();
  int b = t.size();
  string ans;
  while (1) {
    if (a == 0 && b == 0)
      break;
    if (s[a - 1] == t[b - 1]) {
      ans = ans + s[a - 1];
      a = a - 1;
      b = b - 1;
    } else {
      if (dp[a - 1][b] == dp[a][b])
        a = a - 1;
      else
        b = b - 1;
    }
  }

  reverse(ans.begin(), ans.end());
  cout << ans << "\n";

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int dp[3400][3400];
int main() {
  string s, t;
  getline(cin, s);
  getline(cin, t);

  if (s.size() < t.size())
    s.swap(t);

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

  int a = s.size();
  int b = t.size();
  string ans;
  while (1) {
    if (a == 0 || b == 0)
      break;
    if (s[a - 1] == t[b - 1]) {
      ans = ans + s[a - 1];
      a = a - 1;
      b = b - 1;
    } else {
      if (dp[a - 1][b] == dp[a][b])
        a = a - 1;
      else
        b = b - 1;
    }
  }

  reverse(ans.begin(), ans.end());
  cout << ans << "\n";

  return 0;
}
Problem p03165 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define endl '\n'
using namespace std;
int dp[1001][1001];
int lcs(string a, string b, int m, int n) {
  if (m == 0 || n == 0)
    return 0;
  if (dp[m][n] != -1)
    return dp[m][n];
  if (a[m - 1] == b[n - 1])
    return dp[m][n] = 1 + lcs(a, b, m - 1, n - 1);
  return dp[m][n] = max(lcs(a, b, m, n - 1), lcs(a, b, m - 1, n));
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  int m, n;
  string a, b;
  getline(cin, a);
  getline(cin, b);
  m = a.length();
  n = b.length();
  for (int i = 0; i <= m; i++) {
    for (int j = 0; j <= n; j++) {
      dp[i][j] = -1;
    }
  }

  for (int i = 0; i <= m; i++) {
    for (int j = 0; j <= n; j++) {
      if (i == 0 || j == 0)
        dp[i][j] = 0;
      else 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 = m, j = n;
  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>
#define endl '\n'
using namespace std;
int dp[3001][3001];
int lcs(string a, string b, int m, int n) {
  if (m == 0 || n == 0)
    return 0;
  if (dp[m][n] != -1)
    return dp[m][n];
  if (a[m - 1] == b[n - 1])
    return dp[m][n] = 1 + lcs(a, b, m - 1, n - 1);
  return dp[m][n] = max(lcs(a, b, m, n - 1), lcs(a, b, m - 1, n));
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  int m, n;
  string a, b;
  getline(cin, a);
  getline(cin, b);
  m = a.length();
  n = b.length();
  for (int i = 0; i <= m; i++) {
    for (int j = 0; j <= n; j++) {
      dp[i][j] = -1;
    }
  }

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

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define mod 1000000007
#define INF (1 << 29)
#define MAX_N 1000010
/* ちゃんと考えてわかって実装 */

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

  int dp[3000][3000];
  for (int i = 0; i <= s.size(); i++)
    dp[0][i] = 0;
  for (int i = 0; i <= t.size(); i++)
    dp[i][0] = 0;

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

  int i = s.size();
  int j = t.size();
  string ans = "";
  if (dp[i][j] == 0)
    string ans = " ";
  while (i > 0 && j > 0) {
    if (dp[i - 1][j] == dp[i][j]) {
      i--;
    } else if (dp[i][j - 1] == dp[i][j]) {
      j--;
    } else {
      ans = s[i - 1] + ans;
      i--;
      j--;
    }
  }
  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define mod 1000000007
#define INF (1 << 29)
#define MAX_N 1000010
/* ちゃんと考えてわかって実装 */

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

  int dp[3001][3001];
  for (int i = 0; i <= s.size(); i++)
    dp[0][i] = 0;
  for (int i = 0; i <= t.size(); i++)
    dp[i][0] = 0;

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

  int i = s.size();
  int j = t.size();
  string ans = "";
  if (dp[i][j] == 0)
    string ans = " ";
  while (i > 0 && j > 0) {
    if (dp[i - 1][j] == dp[i][j]) {
      i--;
    } else if (dp[i][j - 1] == dp[i][j]) {
      j--;
    } else {
      ans = s[i - 1] + ans;
      i--;
      j--;
    }
  }
  cout << ans << endl;
  return 0;
}
Problem p03165 (C++)

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

using namespace std;

int lookup[20][20];
string str = "";

void LCS(string p, string q, int m, int n) {
  if (m == 0 || n == 0)
    return;

  if (p[m - 1] == q[n - 1]) {
    str += p[m - 1];
    return LCS(p, q, m - 1, n - 1);
  }

  if (lookup[m - 1][n] > lookup[m][n - 1])
    return LCS(p, q, m - 1, n);
  else
    return LCS(p, q, m, n - 1);
}

void LCS_length(string p, string q, int m, int n) {

  for (int i = 0; i <= m; i++)
    lookup[i][0] = 0;
  for (int j = 0; j <= n; j++)
    lookup[0][j] = 0;

  for (int i = 1; i <= m; i++) {
    for (int j = 1; j <= n; j++) {
      if (p[i - 1] == q[j - 1])
        lookup[i][j] = lookup[i - 1][j - 1] + 1;

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

int main() {
  string X;
  string Y;

  cin >> X >> Y;

  // printf("Longest Common Subsequence is
  // %d\n",LCS_length(X,Y,X.length(),Y.length()));
  LCS_length(X, Y, X.length(), Y.length());
  LCS(X, Y, X.length(), Y.length());

  for (int i = str.length() - 1; i >= 0; i--)
    cout << str[i];
  cout << endl;

  return 0;
}


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

using namespace std;

int lookup[3005][3005];
string str = "";

void LCS(string p, string q, int m, int n) {
  if (m == 0 || n == 0)
    return;

  if (p[m - 1] == q[n - 1]) {
    str += p[m - 1];
    return LCS(p, q, m - 1, n - 1);
  }

  if (lookup[m - 1][n] > lookup[m][n - 1])
    return LCS(p, q, m - 1, n);
  else
    return LCS(p, q, m, n - 1);
}

void LCS_length(string p, string q, int m, int n) {

  for (int i = 0; i <= m; i++)
    lookup[i][0] = 0;
  for (int j = 0; j <= n; j++)
    lookup[0][j] = 0;

  for (int i = 1; i <= m; i++) {
    for (int j = 1; j <= n; j++) {
      if (p[i - 1] == q[j - 1])
        lookup[i][j] = lookup[i - 1][j - 1] + 1;

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

int main() {
  string X;
  string Y;

  cin >> X >> Y;

  // printf("Longest Common Subsequence is
  // %d\n",LCS_length(X,Y,X.length(),Y.length()));
  LCS_length(X, Y, X.length(), Y.length());
  LCS(X, Y, X.length(), Y.length());

  for (int i = str.length() - 1; i >= 0; i--)
    cout << str[i];
  cout << endl;

  return 0;
}
Problem p03165 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
void solve() {
  string s, t;
  cin >> s >> t;
  int n = s.size(), m = t.size();
  int arr[n + 1][m + 1];
  for (int i = 0; i <= n; i++) {
    for (int j = 0; j <= m; j++) {
      if ((i == 0) || (j == 0))
        arr[i][j] = 0;
      else {
        if (s[i - 1] == t[j - 1]) {
          arr[i][j] = arr[i - 1][j - 1] + 1;
        } else {
          arr[i][j] = max(arr[i - 1][j], arr[i][j - 1]);
        }
      }
    }
  }
  // cout<<arr[n][m];
  char backtrack[arr[n][m]];
  int z = arr[n][m] - 1, count1 = 0;
  int a = n, b = m;
  while (count1 < arr[n][m]) {
    if (arr[a][b] != max(arr[a - 1][b], arr[a][b - 1])) {
      backtrack[z] = s[a - 1];
      z--;
      a--;
      b--;
      count1++;
    } else {
      if (arr[a][b] == arr[a - 1][b]) {
        a--;
      } else {
        b--;
      }
    }
  }
  for (int i = 0; i < arr[n][m]; i++) {
    cout << backtrack[i];
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
#ifndef ONLINE_JUDGE
  freopen("/home/ubuntu/.config/sublime-text-3/Packages/User/input.txt", "r",
          stdin);
  freopen("/home/ubuntu/.config/sublime-text-3/Packages/User/output.txt", "w",
          stdout);
#endif

  solve();
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
void solve() {
  string s, t;
  cin >> s >> t;
  int n = s.size(), m = t.size();
  int arr[n + 1][m + 1];
  for (int i = 0; i <= n; i++) {
    for (int j = 0; j <= m; j++) {
      if ((i == 0) || (j == 0))
        arr[i][j] = 0;
      else {
        if (s[i - 1] == t[j - 1]) {
          arr[i][j] = arr[i - 1][j - 1] + 1;
        } else {
          arr[i][j] = max(arr[i - 1][j], arr[i][j - 1]);
        }
      }
    }
  }
  // cout<<arr[n][m];
  char backtrack[arr[n][m]];
  int z = arr[n][m] - 1, count1 = 0;
  int a = n, b = m;
  while (count1 < arr[n][m]) {
    if (arr[a][b] != max(arr[a - 1][b], arr[a][b - 1])) {
      backtrack[z] = s[a - 1];
      z--;
      a--;
      b--;
      count1++;
    } else {
      if (arr[a][b] == arr[a - 1][b]) {
        a--;
      } else {
        b--;
      }
    }
  }
  for (int i = 0; i < arr[n][m]; i++) {
    cout << backtrack[i];
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);

  solve();
}