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

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

int main() {
  string s, t;
  cin >> s >> t;
  ll a[s.length() + 1][t.length() + 1], i, j;
  memset(a, 0, sizeof(a));
  for (i = 0; i <= s.length(); i++) {
    for (j = 0; j <= t.length(); j++) {
      if (i == 0 || j == 0) {
        a[i][j] = 0;
      } else {
        if (s[i - 1] == t[j - 1]) {
          a[i][j] = a[i - 1][j - 1] + 1;
        } else {
          a[i][j] = max(a[i - 1][j], a[i][j - 1]);
        }
      }
    }
  }

  string s1 = "";
  for (i = s.length(), j = t.length(); i > 0, j > 0;) {
    if (s[i - 1] == t[j - 1]) {
      s1 += s[i - 1];
      i--, j--;
    } else {
      if (a[i - 1][j] == a[i][j]) {
        i--;
      } else {
        j--;
      }
    }
  }
  reverse(s1.begin(), s1.end());
  cout << s1 << endl;
  // cout<<a[s.length()-1][t.length()-1]<<endl;
}

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

int main() {
  string s, t;
  cin >> s >> t;
  ll a[s.length() + 1][t.length() + 1], i, j;
  memset(a, 0, sizeof(a));
  for (i = 0; i <= s.length(); i++) {
    for (j = 0; j <= t.length(); j++) {
      if (i == 0 || j == 0) {
        a[i][j] = 0;
      } else {
        if (s[i - 1] == t[j - 1]) {
          a[i][j] = a[i - 1][j - 1] + 1;
        } else {
          a[i][j] = max(a[i - 1][j], a[i][j - 1]);
        }
      }
    }
  }

  string s1 = "";
  for (i = s.length(), j = t.length(); i > 0 && j > 0;) {
    if (s[i - 1] == t[j - 1]) {
      s1 += s[i - 1];
      i--, j--;
    } else {
      if (a[i - 1][j] == a[i][j]) {
        i--;
      } else {
        j--;
      }
    }
  }
  reverse(s1.begin(), s1.end());
  cout << s1 << endl;
  // cout<<a[s.length()-1][t.length()-1]<<endl;
}
Problem p03165 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
typedef pair<int, int> P;

string s, t;
string dp2[2][3030] = {};
int main() {
  cin >> s >> t;
  int n = s.size(), m = t.size();
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      if (s.at(i - 1) == t.at(j - 1)) {
        string tmp = dp2[(i - 1) & 1][j - 1];
        tmp.push_back(t.at(j - 1));
        dp2[i & 1][j] = tmp;
      } else {
        if (dp2[(i - 1) & 1][j].size() > dp2[i & 1][j - 1].size()) {
          dp2[i & 1][j] = dp2[(i - 1) & 1][j];
        } else {
          dp2[i & 1][j] = dp2[i & 1][j - 1];
        }
      }
    }
  }
  cout << dp2[n & 1][m] << endl;
  return 0;
}


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

string s, t;
string dp2[2][3030] = {};
int main() {
  cin >> s >> t;
  int n = s.size(), m = t.size();
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      if (s.at(i - 1) == t.at(j - 1)) {
        dp2[i & 1][j] = dp2[(i - 1) & 1][j - 1];
        dp2[i & 1][j].push_back(t.at(j - 1));
      } else {
        if (dp2[(i - 1) & 1][j].size() > dp2[i & 1][j - 1].size()) {
          dp2[i & 1][j] = dp2[(i - 1) & 1][j];
        } else {
          dp2[i & 1][j] = dp2[i & 1][j - 1];
        }
      }
    }
  }
  cout << dp2[n & 1][m] << endl;
  return 0;
}
Problem p03165 (C++)

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

using namespace std;

string s, t;
int dp[3010][3010];

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

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

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

  string ans;
  int i = n, j = m;
  while (i > 0 && j > 0) {
    if (s[i - 1] == t[j - 1]) {
      ans.push_back(s[i - 1]);
      i--, j--;
    } else if (dp[i - 1][j] > dp[i][j - 1])
      i--;
    else
      j--;
  }
  reverse(ans.begin(), ans.end());
  cout << ans << '\n';
}

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

using namespace std;

string s, t;
int dp[3010][3010];

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

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

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

  string ans;
  int i = n, j = m;
  while (i > 0 && j > 0) {
    if (s[i - 1] == t[j - 1]) {
      ans.push_back(s[i - 1]);
      i--, j--;
    } else if (dp[i - 1][j] > dp[i][j - 1])
      i--;
    else
      j--;
  }
  reverse(ans.begin(), ans.end());
  cout << ans << '\n';
}
Problem p03165 (C++)

Buggy submission (status: Runtime Error):
// #include"stdc++.h"
#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define fast                                                                   \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define cases                                                                  \
  ll t;                                                                        \
  cin >> t;                                                                    \
  while (t--)
#define f1(a, n) for (int i = a; i < n; i++)
#define f2(b, n) for (int j = b; j < n; j++)
#define INF 1000000000000009
#define pb push_back
ll ar[100005];
#define MAX 1000005
int main() {
  fast string s1, s2;
  ll dp[1005][1005];
  cin >> s1 >> s2;
  memset(dp, 0, sizeof(dp));
  ll ans = 0;

  f1(1, s1.length() + 1) {
    f2(1, s2.length() + 1) {
      dp[i][j] = max(dp[i - 1][j - 1], max(dp[i - 1][j], dp[i][j - 1]));
      if (s1[i - 1] == s2[j - 1])
        dp[i][j] = max(dp[i][j], dp[i - 1][j - 1] + 1);
    }
  }
  // cout<<dp[s1.size()][s2.size()]<<endl;
  string t;
  int i = s1.size(), j = s2.size();
  while (dp[i][j]) {
    if (dp[i][j] == dp[i - 1][j])
      i--;
    else if (dp[i][j] == dp[i][j - 1])
      j--;
    else if (dp[i][j] > dp[i - 1][j - 1])
      i--, j--, t += s1[i];
  }
  reverse(t.begin(), t.end());
  cout << t;

  return 0;
}


Fixed submission:
// #include"stdc++.h"
#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define fast                                                                   \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define cases                                                                  \
  ll t;                                                                        \
  cin >> t;                                                                    \
  while (t--)
#define f1(a, n) for (int i = a; i < n; i++)
#define f2(b, n) for (int j = b; j < n; j++)
#define INF 1000000000000009
#define pb push_back
ll ar[100005];
#define MAX 1000005
int main() {
  fast string s1, s2;
  ll dp[3005][3005];
  cin >> s1 >> s2;
  memset(dp, 0, sizeof(dp));
  ll ans = 0;

  f1(1, s1.length() + 1) {
    f2(1, s2.length() + 1) {
      dp[i][j] = max(dp[i - 1][j - 1], max(dp[i - 1][j], dp[i][j - 1]));
      if (s1[i - 1] == s2[j - 1])
        dp[i][j] = max(dp[i][j], dp[i - 1][j - 1] + 1);
    }
  }
  // cout<<dp[s1.size()][s2.size()]<<endl;
  string t;
  int i = s1.size(), j = s2.size();
  while (dp[i][j]) {
    if (dp[i][j] == dp[i - 1][j])
      i--;
    else if (dp[i][j] == dp[i][j - 1])
      j--;
    else if (dp[i][j] > dp[i - 1][j - 1])
      i--, j--, t += s1[i];
  }
  reverse(t.begin(), t.end());
  cout << t;

  return 0;
}
Problem p03165 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define int long long int
#define INF 1000000000000000000
#define fn(i, n) for (int i = 0; i < n; i += 1)
int w[100100], v[100100];
int dp[1001][1001] = {0};

signed main() {
  string s, t;
  cin >> s >> t;
  int m = s.size();
  int n = t.size();
  fn(i, m + 1) {
    fn(j, n + 1) {
      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]);
    }
  }
  string ans = "";
  int i = m, j = n;
  while (i > 0 && j > 0) {
    if (s[i - 1] == t[j - 1]) {
      ans += s[i - 1];
      i -= 1;
      j -= 1;
    } else if (dp[i - 1][j] > dp[i][j - 1])
      i -= 1;
    else
      j -= 1;
  }
  reverse(ans.begin(), ans.end());
  cout << ans << endl;

  return 0;
}


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

int dp[3001][3001] = {0};

signed main() {
  string s, t;
  cin >> s >> t;
  int m = s.size();
  int n = t.size();
  fn(i, m + 1) {
    fn(j, n + 1) {
      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]);
    }
  }
  string ans = "";
  int i = m, j = n;
  while (i > 0 && j > 0) {
    if (s[i - 1] == t[j - 1]) {
      ans += s[i - 1];
      i -= 1;
      j -= 1;
    } else if (dp[i - 1][j] > dp[i][j - 1])
      i -= 1;
    else
      j -= 1;
  }
  reverse(ans.begin(), ans.end());
  cout << ans << endl;

  return 0;
}
Problem p03165 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
using namespace std;
string ar[2][3050] = {};
string a, b;
string bg(string l, string r) {
  if (l.size() > r.size()) {
    return l;
  } else {
    return r;
  }
}
int main() {
  cin >> a >> b;
  for (int i = 1; i <= a.size(); i++) {
    for (int j = 1; j <= b.size(); j++) {
      ar[i % 2][j] = bg(ar[(i - 1) % 2][j], ar[i % 2][j - 1]);
      if (a[i - 1] == b[j - 1]) {
        ar[i % 2][j] = bg(ar[i % 2][j], ar[(i - 1) % 2][j - 1] + a[i - 1]);
      }
    }
  }
  cout << ar[a.size()][b.size()] << "\n";
  return 0;
}

Fixed submission:
#include <iostream>
using namespace std;
string ar[2][3050] = {};
string a, b;
string bg(string l, string r) {
  if (l.size() > r.size()) {
    return l;
  } else {
    return r;
  }
}
int main() {
  cin >> a >> b;
  for (int i = 1; i <= a.size(); i++) {
    for (int j = 1; j <= b.size(); j++) {
      ar[i % 2][j] = bg(ar[(i - 1) % 2][j], ar[i % 2][j - 1]);
      if (a[i - 1] == b[j - 1]) {
        ar[i % 2][j] = bg(ar[i % 2][j], ar[(i - 1) % 2][j - 1] + a[i - 1]);
      }
    }
  }
  cout << ar[a.size() % 2][b.size()] << "\n";
  return 0;
}
Problem p03165 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
using namespace std;
template <class T> bool max(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

template <class T> int maxReturn(T a, T b) {
  if (a > b) {
    return a;
  }
  return b;
}

template <class T> bool min(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

int main(void) {
  string s, t;
  cin >> s;
  cin >> t;
  int slength = s.length();
  int tlength = t.length();
  int dp[1000][1000];

  for (int i = 1; i <= slength; i++) {
    for (int a = 1; a <= tlength; a++) {
      if (s[i - 1] == t[a - 1]) {
        dp[i][a] = dp[i - 1][a - 1] + 1;
      } else {
        dp[i][a] = maxReturn(dp[i - 1][a], dp[i][a - 1]);
      }
    }
  }

  int i = slength;
  int j = tlength;
  string res = "";

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

  cout << res << endl;
}

Fixed submission:
#include <iostream>
using namespace std;
template <class T> bool max(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

template <class T> int maxReturn(T a, T b) {
  if (a > b) {
    return a;
  }
  return b;
}

template <class T> bool min(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

int main(void) {
  string s, t;
  cin >> s;
  cin >> t;
  int slength = s.length();
  int tlength = t.length();
  int dp[3100][3100] = {0};

  for (int i = 1; i <= slength; i++) {
    for (int a = 1; a <= tlength; a++) {
      if (s[i - 1] == t[a - 1]) {
        dp[i][a] = dp[i - 1][a - 1] + 1;
      } else {
        dp[i][a] = maxReturn(dp[i - 1][a], dp[i][a - 1]);
      }
    }
  }

  int i = slength;
  int j = tlength;
  string res = "";

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

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

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
typedef long long ll;
#define mod 1000000007
#define F first
#define S second
using namespace std;
string a, b;
ll memo[1005][1005];
ll solve(int i, int j) {
  if (i == a.size() || j == b.size())
    return 0;
  if (~memo[i][j])
    return memo[i][j];
  ll ret = 0;
  if (a[i] == b[j])
    ret = max(ret, solve(i + 1, j + 1) + 1);
  ret = max(ret, solve(i + 1, j));
  ret = max(ret, solve(i, j + 1));
  memo[i][j] = ret;
  return ret;
}

void print(int i, int j) {
  if (i == a.size() || j == b.size())
    exit(0);
  ll ret = memo[i][j];
  if (a[i] == b[j]) {
    cout << a[i];
    print(i + 1, j + 1);
  }
  if (solve(i + 1, j) == ret)
    print(i + 1, j);
  print(i, j + 1);
}

int main() {
  cin >> a >> b;
  memset(memo, -1, sizeof memo);
  solve(0, 0);
  print(0, 0);
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
typedef long long ll;
#define mod 1000000007
#define F first
#define S second
using namespace std;
string a, b;
ll memo[3005][3005];
ll solve(int i, int j) {
  if (i == a.size() || j == b.size())
    return 0;
  if (~memo[i][j])
    return memo[i][j];
  ll ret = 0;
  if (a[i] == b[j])
    ret = max(ret, solve(i + 1, j + 1) + 1);
  ret = max(ret, solve(i + 1, j));
  ret = max(ret, solve(i, j + 1));
  memo[i][j] = ret;
  return ret;
}

void print(int i, int j) {
  if (i == a.size() || j == b.size())
    exit(0);
  ll ret = memo[i][j];
  if (a[i] == b[j]) {
    cout << a[i];
    print(i + 1, j + 1);
  }
  if (solve(i + 1, j) == ret)
    print(i + 1, j);
  print(i, j + 1);
}

int main() {
  cin >> a >> b;
  memset(memo, -1, sizeof memo);
  solve(0, 0);
  print(0, 0);
  return 0;
}
Problem p03165 (C++)

Buggy submission (status: Runtime Error):
// g++ -std=gnu++14 a.cpp
// #include <bits/stdc++.h>

#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <iomanip>
#include <iostream>
#include <istream>
#include <iterator>
#include <map>
#include <math.h>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <vector>

using namespace std;

#define ll long long
#define rep(i, n) for (ll i = 0; i < (n); i++)
ll MOD = 1e9 + 7;
int INF = 1 << 30;
ll INFL = 1LL << 60;

int main() {
  string S, T;
  cin >> S >> T;
  int long_s = S.size();
  int long_t = T.size();
  vector<vector<ll>> dp(3010, vector<ll>(3010, 0));
  for (int i = 1; i <= long_s; i++) {
    for (int j = 1; j <= long_t; j++) {
      if (S.at(i - 1) == T.at(j - 1)) {
        dp[i][j] = max(dp[i - 1][j - 1] + 1, dp[i][j]);
      }
      dp[i][j] = max(dp[i][j], dp[i - 1][j]);
      dp[i][j] = max(dp[i][j], dp[i][j - 1]);
    }
  }
  /*
    string ans = "";
    for(int i = 1;i <= long_s;i++){
      for(int j = 1;j <= long_t;j++){
        if(dp[i-1][j-1]+1 == dp[i][j] && dp[i][j] == dp[i-1][j]+1 && dp[i][j] ==
    dp[i][j-1]+1){ ans += S.at(i-1);
        }
      }
    }
    */

  int i = long_s;
  int j = long_t;
  string ans = "";
  while (i >= 0 && j >= 0) {
    if (dp[i][j] == dp[i][j - 1]) {
      j--;
    } else if (dp[i][j] == dp[i - 1][j]) {
      i--;
    } else {
      ans = ans + S.at(i - 1);
      i--;
      j--;
    }
  }
  reverse(ans.begin(), ans.end());

  cout << ans << endl;
}


Fixed submission:
// g++ -std=gnu++14 a.cpp
// #include <bits/stdc++.h>

#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <iomanip>
#include <iostream>
#include <istream>
#include <iterator>
#include <map>
#include <math.h>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <vector>

using namespace std;

#define ll long long
#define rep(i, n) for (ll i = 0; i < (n); i++)
ll MOD = 1e9 + 7;
int INF = 1 << 30;
ll INFL = 1LL << 60;

int main() {
  string S, T;
  cin >> S >> T;
  int long_s = S.size();
  int long_t = T.size();
  vector<vector<ll>> dp(3010, vector<ll>(3010, 0));
  for (int i = 1; i <= long_s; i++) {
    for (int j = 1; j <= long_t; j++) {
      if (S.at(i - 1) == T.at(j - 1)) {
        dp[i][j] = max(dp[i - 1][j - 1] + 1, dp[i][j]);
      }
      dp[i][j] = max(dp[i][j], dp[i - 1][j]);
      dp[i][j] = max(dp[i][j], dp[i][j - 1]);
    }
  }
  /*
    string ans = "";
    for(int i = 1;i <= long_s;i++){
      for(int j = 1;j <= long_t;j++){
        if(dp[i-1][j-1]+1 == dp[i][j] && dp[i][j] == dp[i-1][j]+1 && dp[i][j] ==
    dp[i][j-1]+1){ ans += S.at(i-1);
        }
      }
    }
    */

  int i = long_s;
  int j = long_t;
  string ans = "";
  while (i > 0 && j > 0) {
    if (dp[i][j] == dp[i][j - 1]) {
      j--;
    } else if (dp[i][j] == dp[i - 1][j]) {
      i--;
    } else {
      ans = ans + S.at(i - 1);
      i--;
      j--;
    }
  }
  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 int ll;
string s, t;
ll dp[3007][3007];

int main() {
  cin >> s >> t;
  s += ';';
  t += '.';

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

  ll i = s.length() - 1, j = t.length() - 1;

  string ans = "";
  while (i + j) {
    ll cur = dp[i][j];
    if (dp[i - 1][j] == cur)
      i--;
    else if (dp[i][j - 1] == cur)
      j--;
    else {
      i--;
      j--;
      ans += s[i];
    }
  }

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

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

typedef long long int ll;
string s, t;
ll dp[3007][3007];

int main() {
  cin >> s >> t;
  s += ';';
  t += '.';

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

  ll i = s.length() - 1, j = t.length() - 1;

  string ans = "";
  while (i && j) {
    ll cur = dp[i][j];
    if (dp[i - 1][j] == cur)
      i--;
    else if (dp[i][j - 1] == cur)
      j--;
    else {
      i--;
      j--;
      ans += s[i];
    }
  }

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

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
int dir[1001][1001];
int dp[1001][1001];
string s1, s2;

int solve(int i, int j) {
  if (dp[i][j] != -1)
    return dp[i][j];

  if (i == s1.size() || j == s2.size()) {

    dir[i][j] = -1;
    return dp[i][j] = 0;
  }

  int anss = 0;
  int dik = -1;

  if (s1[i] == s2[j]) {
    int ans = 1 + solve(i + 1, j + 1);
    if (anss < ans) {
      dik = 0;
      anss = ans;
    }
  }
  int ans = solve(i, j + 1);
  if (ans > anss) {
    dik = 1;
    anss = ans;
  }
  ans = solve(i + 1, j);
  if (ans > anss) {
    dik = 2;
    anss = ans;
  }
  dp[i][j] = anss;
  dir[i][j] = dik;
  return dp[i][j];
}

string s;

void backtrack(int i, int j) {
  if (i == s1.size() || j == s2.size()) {
    return;
  }

  if (dir[i][j] == 0) {
    s.push_back(s1[i]);
    backtrack(i + 1, j + 1);
  } else if (dir[i][j] == 1) {
    backtrack(i, j + 1);
  } else if (dir[i][j] == 2) {
    backtrack(i + 1, j);
  }
}

int main() {
  memset(dp, -1, sizeof dp);
  cin >> s1 >> s2;
  int ans = solve(0, 0);
  // cout<<ans<<endl;
  backtrack(0, 0);
  cout << s << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int dir[3001][3001];
int dp[3001][3001];
string s1, s2;

int solve(int i, int j) {
  if (dp[i][j] != -1)
    return dp[i][j];

  if (i == s1.size() || j == s2.size()) {

    dir[i][j] = -1;
    return dp[i][j] = 0;
  }

  int anss = 0;
  int dik = -1;

  if (s1[i] == s2[j]) {
    int ans = 1 + solve(i + 1, j + 1);
    if (anss < ans) {
      dik = 0;
      anss = ans;
    }
  }
  int ans = solve(i, j + 1);
  if (ans > anss) {
    dik = 1;
    anss = ans;
  }
  ans = solve(i + 1, j);
  if (ans > anss) {
    dik = 2;
    anss = ans;
  }
  dp[i][j] = anss;
  dir[i][j] = dik;
  return dp[i][j];
}

string s;

void backtrack(int i, int j) {
  if (i == s1.size() || j == s2.size()) {
    return;
  }

  if (dir[i][j] == 0) {
    s.push_back(s1[i]);
    backtrack(i + 1, j + 1);
  } else if (dir[i][j] == 1) {
    backtrack(i, j + 1);
  } else if (dir[i][j] == 2) {
    backtrack(i + 1, j);
  }
}

int main() {
  memset(dp, -1, sizeof dp);
  cin >> s1 >> s2;
  int ans = solve(0, 0);
  // cout<<ans<<endl;
  backtrack(0, 0);
  cout << s << endl;
}
Problem p03165 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bits/stdc++.h>
using namespace std;
#define float double
const int LMT = 1000000000 + 5;
#define int long long
#define lp(i, n) for (i = 0; i < n; i++)
const int N = 1005, M = 1005;
int n, m, w[N], v[N], i, j;
string a, b;
int dp[N][M];
bool vis[N][M];
void solve(int n, int m) {
  for (int i = 1; i < n; i++) {
    for (int j = 1; j < m; j++) {
      if (a[i] == b[j])
        dp[i][j] = dp[i - 1][j - 1] + 1;
      else
        dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]);
    }
  }
}
int32_t main() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  cin >> a >> b;
  a = '@' + a;
  b = '@' + b;
  memset(dp, 0, sizeof(dp));
  solve(n = a.length(), m = b.length());
  // cout<<dp[n-1][m-1]<<endl;
  i = n - 1;
  j = m - 1;
  string ans = "";

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

      }*/

  while (i > 0 && j > 0) {
    // cout<<ans<<endl;
    // cout<<i<<" "<<j<<" ";
    int curr = dp[i][j], ca = dp[i - 1][j - 1], cl = dp[i][j - 1],
        cu = dp[i - 1][j];
    int mx = max(max(cl, cu), ca);
    // cout<<"mx="<<mx<<endl;
    if (mx == ca) {

      if (mx < dp[i][j])
        ans += a[i];
      i--;
      j--;
      continue;
    }
    if (mx == cl) {
      if (mx < dp[i][j])
        ans += a[i];
      j--;
      continue;

    } else {
      if (mx < dp[i][j])
        ans += a[i];
      i--;
    }
  }

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

Fixed submission:
#include <algorithm>
#include <bits/stdc++.h>
using namespace std;
#define float double
const int LMT = 1000000000 + 5;
#define int long long
#define lp(i, n) for (i = 0; i < n; i++)
const int N = 3005, M = 3005;
int n, m, w[N], v[N], i, j;
string a, b;
int dp[N][M];
bool vis[N][M];
void solve(int n, int m) {
  for (int i = 1; i < n; i++) {
    for (int j = 1; j < m; j++) {
      if (a[i] == b[j])
        dp[i][j] = dp[i - 1][j - 1] + 1;
      else
        dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]);
    }
  }
}
int32_t main() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  cin >> a >> b;
  a = '@' + a;
  b = '@' + b;
  memset(dp, 0, sizeof(dp));
  solve(n = a.length(), m = b.length());
  // cout<<dp[n-1][m-1]<<endl;
  i = n - 1;
  j = m - 1;
  string ans = "";

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

      }*/

  while (i > 0 && j > 0) {
    // cout<<ans<<endl;
    // cout<<i<<" "<<j<<" ";
    int curr = dp[i][j], ca = dp[i - 1][j - 1], cl = dp[i][j - 1],
        cu = dp[i - 1][j];
    int mx = max(max(cl, cu), ca);
    // cout<<"mx="<<mx<<endl;
    if (mx == ca) {

      if (mx < dp[i][j])
        ans += a[i];
      i--;
      j--;
      continue;
    }
    if (mx == cl) {
      if (mx < dp[i][j])
        ans += a[i];
      j--;
      continue;

    } else {
      if (mx < dp[i][j])
        ans += a[i];
      i--;
    }
  }

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

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

const int maxn = 3005;
int dp[maxn][maxn];
pair<int, int> dir[maxn][maxn];
string a, b;

int main(int argc, char const *argv[]) {
  //	freopen("in.txt", "r", stdin);
  cin >> a >> b;
  int n = a.size(), m = b.size();
  for (int i = 0; i <= n; i++) {
    for (int j = 0; j <= m; j++) {
      if (i == 0 || j == 0) {
        dp[i][j] = 0;
        dir[i][j] = make_pair(i, j);
      } else if (a[i - 1] == b[j - 1]) {
        dp[i][j] = dp[i - 1][j - 1] + 1;
        dir[i][j] = make_pair(i - 1, j - 1);
      } else {
        dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]);
        if (dp[i][j] == dp[i - 1][j]) {
          dir[i][j] = make_pair(i - 1, j);
        } else {
          dir[i][j] = make_pair(i, j - 1);
        }
      }
    }
  }
  string ans;
  pair<int, int> cur = make_pair(n, m);
  while (cur.first != 0 || cur.second != 0) {
    int i = cur.first, j = cur.second;
    if (dir[i][j] == make_pair(i - 1, j - 1)) {
      ans += a[i - 1];
      cur = make_pair(i - 1, j - 1);
    } else if (dir[i][j] == make_pair(i - 1, j)) {
      cur = make_pair(i - 1, j);
    } else {
      cur = make_pair(i, j - 1);
    }
  }
  reverse(ans.begin(), ans.end());
  cout << ans << endl;

  return 0;
}

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

const int maxn = 3005;
int dp[maxn][maxn];
pair<int, int> dir[maxn][maxn];
string a, b;

int main(int argc, char const *argv[]) {
  //	freopen("in.txt", "r", stdin);
  cin >> a >> b;
  int n = a.size(), m = b.size();
  for (int i = 0; i <= n; i++) {
    for (int j = 0; j <= m; j++) {
      if (i == 0 || j == 0) {
        dp[i][j] = 0;
        dir[i][j] = make_pair(i, j);
      } else if (a[i - 1] == b[j - 1]) {
        dp[i][j] = dp[i - 1][j - 1] + 1;
        dir[i][j] = make_pair(i - 1, j - 1);
      } else {
        dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]);
        if (dp[i][j] == dp[i - 1][j]) {
          dir[i][j] = make_pair(i - 1, j);
        } else {
          dir[i][j] = make_pair(i, j - 1);
        }
      }
    }
  }
  string ans;
  pair<int, int> cur = make_pair(n, m);
  while (cur.first != 0 && cur.second != 0) {
    int i = cur.first, j = cur.second;
    if (dir[i][j] == make_pair(i - 1, j - 1)) {
      ans += a[i - 1];
      cur = make_pair(i - 1, j - 1);
    } else if (dir[i][j] == make_pair(i - 1, j)) {
      cur = make_pair(i - 1, j);
    } else {
      cur = make_pair(i, j - 1);
    }
  }
  reverse(ans.begin(), ans.end());
  cout << ans << endl;

  return 0;
}
Problem p03165 (C++)

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

using namespace std;

int dp[1007][1007];

int main() {
  memset(dp, 0, sizeof(dp));
  string s, t;
  cin >> s >> t;
  for (int x = 1; x <= s.size(); x++) {
    for (int j = 1; j <= t.size(); j++) {
      if (s[x - 1] == t[j - 1])
        dp[x][j] = 1 + dp[x - 1][j - 1];
      else
        dp[x][j] = max(dp[x - 1][j], dp[x][j - 1]);
    }
  }
  int i = (int)s.size(), j = (int)t.size();
  string ans;
  while (i > 0 && j > 0) {
    if (dp[i][j] == dp[i - 1][j]) {
      i--;
    } else if (dp[i][j] == dp[i][j - 1])
      j--;
    else if (dp[i][j] == 1 + dp[i - 1][j - 1]) {
      ans.push_back(s[i - 1]);
      i--;
      j--;
    }
  }
  reverse(ans.begin(), ans.end());
  cout << ans << "\n";
  return 0;
}

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

using namespace std;

int dp[3007][3007];

int main() {
  memset(dp, 0, sizeof(dp));
  string s, t;
  cin >> s >> t;
  for (int x = 1; x <= s.size(); x++) {
    for (int j = 1; j <= t.size(); j++) {
      if (s[x - 1] == t[j - 1])
        dp[x][j] = 1 + dp[x - 1][j - 1];
      else
        dp[x][j] = max(dp[x - 1][j], dp[x][j - 1]);
    }
  }
  int i = (int)s.size(), j = (int)t.size();
  string ans;
  while (i > 0 && j > 0) {
    if (dp[i][j] == dp[i - 1][j]) {
      i--;
    } else if (dp[i][j] == dp[i][j - 1])
      j--;
    else if (dp[i][j] == 1 + dp[i - 1][j - 1]) {
      ans.push_back(s[i - 1]);
      i--;
      j--;
    }
  }
  reverse(ans.begin(), ans.end());
  cout << ans << "\n";
  return 0;
}
Problem p03165 (C++)

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

using namespace std;

int dp[3000][3000];

void lcs(string s1, string s2) {

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

int main() {
  string s1;
  string s2;

  getline(cin, s1);
  getline(cin, s2);

  lcs(s1, s2);

  int i = s1.size();
  int j = s2.size();
  string s3 = "";

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

  cout << s3 << '\n';

  return 0;
}


Fixed submission:
#include <iostream>

using namespace std;

int dp[5000][5000];

void lcs(string s1, string s2) {

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

int main() {
  string s1;
  string s2;

  getline(cin, s1);
  getline(cin, s2);

  lcs(s1, s2);

  int i = s1.size();
  int j = s2.size();
  string s3 = "";

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

  cout << s3 << '\n';

  return 0;
}
Problem p03165 (C++)

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


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

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
#define PI 3.14159265359
#define INF 1000100100
#define all(x) (x).begin(), (x).end()
#define P pair<int, int>
typedef long long ll;
using namespace std;

int main() {
  string s, t;
  cin >> s >> t;
  int sl = s.length();
  int tl = t.length();
  s = '#' + s;
  t = '#' + t;
  int dp[sl + 1][tl + 1];
  rep(i, sl + 1) dp[i][0] = 0;
  rep(i, tl + 1) dp[0][i] = 0;

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

  int len = dp[sl][tl];
  string ans = "";
  int nowi = sl;
  int nowj = tl;
  while (len) {
    if (s[nowi] == t[nowj]) {
      ans = s[nowi] + ans;
      nowi--;
      nowj--;
      len--;
    } else if (dp[nowi - 1][nowj] == dp[nowi][nowj - 1])
      nowi--;
    else
      nowj--;
  }
  cout << ans << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
#define PI 3.14159265359
#define INF 1000100100
#define all(x) (x).begin(), (x).end()
#define P pair<int, int>
typedef long long ll;
using namespace std;

int main() {
  string s, t;
  cin >> s >> t;
  int sl = s.length();
  int tl = t.length();
  s = '#' + s;
  t = '#' + t;
  int dp[sl + 1][tl + 1];
  rep(i, sl + 1) dp[i][0] = 0;
  rep(i, tl + 1) dp[0][i] = 0;

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

  int len = dp[sl][tl];
  string ans = "";
  int nowi = sl;
  int nowj = tl;
  while (len) {
    if (s[nowi] == t[nowj]) {
      ans = s[nowi] + ans;
      nowi--;
      nowj--;
      len--;
    } else if (dp[nowi][nowj] == dp[nowi - 1][nowj])
      nowi--;
    else
      nowj--;
  }
  cout << ans << endl;
  return 0;
}
Problem p03165 (C++)

Buggy submission (status: Runtime Error):
#include "bits/stdc++.h"
using namespace std;
#define MAX 300
int d[MAX][MAX];
string a, b;
int dp(int l, int r) {
  if (r < 0 || l < 0)
    return 0;
  if (d[l][r] == -1) {
    if (a[l] == b[r])
      d[l][r] = dp(l - 1, r - 1) + 1;
    else
      d[l][r] = max({dp(l - 1, r), dp(l, r - 1)});
  }
  return d[l][r];
}
string dp2(int l, int r) {
  if (l == 0) {
    string s = "";
    if (d[l][r])
      return s + a[l];
    else
      return "";
  }
  if (r == 0) {
    string s = "";
    if (d[l][r])
      return s + b[r];
    else
      return "";
  }
  if (a[l] == b[r]) {
    return dp2(l - 1, r - 1) + a[l];
  } else {
    int m = max(d[l - 1][r], d[l][r - 1]);
    if (m == d[l - 1][r])
      return dp2(l - 1, r);
    else
      return dp2(l, r - 1);
  }
}
int main() {
  cin >> a >> b;
  for (int i = 0; i < a.length(); i++)
    for (int j = 0; j < b.length(); j++)
      d[i][j] = -1;
  dp(a.length() - 1, b.length() - 1);
  cout << dp2(a.length() - 1, b.length() - 1) << endl;

  return 0;
}

Fixed submission:
#include "bits/stdc++.h"
using namespace std;
#define MAX 3000
int d[MAX][MAX];
string a, b;
int dp(int l, int r) {
  if (r < 0 || l < 0)
    return 0;
  if (d[l][r] == -1) {
    if (a[l] == b[r])
      d[l][r] = dp(l - 1, r - 1) + 1;
    else
      d[l][r] = max({dp(l - 1, r), dp(l, r - 1)});
  }
  return d[l][r];
}
string dp2(int l, int r) {
  if (l == 0) {
    string s = "";
    if (d[l][r])
      return s + a[l];
    else
      return "";
  }
  if (r == 0) {
    string s = "";
    if (d[l][r])
      return s + b[r];
    else
      return "";
  }
  if (a[l] == b[r]) {
    return dp2(l - 1, r - 1) + a[l];
  } else {
    int m = max(d[l - 1][r], d[l][r - 1]);
    if (m == d[l - 1][r])
      return dp2(l - 1, r);
    else
      return dp2(l, r - 1);
  }
}
int main() {
  cin >> a >> b;
  for (int i = 0; i < a.length(); i++)
    for (int j = 0; j < b.length(); j++)
      d[i][j] = -1;
  dp(a.length() - 1, b.length() - 1);
  cout << dp2(a.length() - 1, b.length() - 1) << endl;

  return 0;
}
Problem p03165 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define IOS                                                                    \
  ios::sync_with_stdio(0);                                                     \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define ll long long
#define ld long double
#define pb push_back
const ll mod = 1e9 + 7;
template <typename T> T pow(T a, T b, long long m) {
  T ans = 1;
  while (b > 0) {
    if (b % 2 == 1)
      ans = (ans * a) % m;
    b /= 2;
    a = (a * a) % m;
  }
  return ans % m;
}
const ll N = 1e6 + 8;
const ll INF = 1e18;
string a, b;
ll dp[3005][3005];
ll LCS(ll i, ll j) {
  if (i == 0 || j == 0)
    return 0;
  if (dp[i][j] != -1)
    return dp[i][j];
  ll ans = 0;
  if (a[i - 1] == b[j - 1]) {
    ans = 1 + LCS(i - 1, j - 1);
  }
  ans = max(ans, LCS(i - 1, j));
  ans = max(ans, LCS(i, j - 1));
  return dp[i][j] = ans;
}
int main() {
  IOS;
  cin >> a >> b;
  string s = "";
  memset(dp, -1, sizeof(dp));
  ll l = LCS(a.length(), b.length());
  ll n = a.length(), m = b.length();
  while (n > 0 || m > 0 || l != 0) {
    if (a[n - 1] == b[m - 1] && LCS(n, m) == l) {
      s += a[n - 1];
      // cout<<a[n-1];
      n--;
      m--;
      l--;
    } else if (LCS(n - 1, m) == l) {
      n--;
    } else
      m--;
  }
  reverse(s.begin(), s.end());
  cout << s << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define IOS                                                                    \
  ios::sync_with_stdio(0);                                                     \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define ll long long
#define ld long double
#define pb push_back
const ll mod = 1e9 + 7;
template <typename T> T pow(T a, T b, long long m) {
  T ans = 1;
  while (b > 0) {
    if (b % 2 == 1)
      ans = (ans * a) % m;
    b /= 2;
    a = (a * a) % m;
  }
  return ans % m;
}
const ll N = 1e6 + 8;
const ll INF = 1e18;
string a, b;
ll dp[3005][3005];
ll LCS(ll i, ll j) {
  if (i == 0 || j == 0)
    return 0;
  if (dp[i][j] != -1)
    return dp[i][j];
  ll ans = 0;
  if (a[i - 1] == b[j - 1]) {
    ans = 1 + LCS(i - 1, j - 1);
  }
  ans = max(ans, LCS(i - 1, j));
  ans = max(ans, LCS(i, j - 1));
  return dp[i][j] = ans;
}
int main() {
  IOS;
  cin >> a >> b;
  string s = "";
  memset(dp, -1, sizeof(dp));
  ll l = LCS(a.length(), b.length());
  ll n = a.length(), m = b.length();
  while (n > 0 && m > 0 && l != 0) {
    if (a[n - 1] == b[m - 1] && LCS(n, m) == l) {
      s += a[n - 1];
      // cout<<a[n-1];
      n--;
      m--;
      l--;
    } else if (LCS(n - 1, m) == l) {
      n--;
    } else
      m--;
  }
  reverse(s.begin(), s.end());
  cout << s << endl;
}
Problem p03165 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define ull unsigned long long int
#define ll long long
#define int long long
#define ld long double
#define endl '\n'
const int mod = 1e9 + 7;
#define F first
#define S second
#define pb push_back
#define mp make_pair
#define trace(x) cerr << #x << " is " << x << endl;
#define all(x) x.begin(), x.end()
#define reset(x, val) memset(x, val, sizeof(x))
#define flush fflush(stdout)
#define sz(a) a.size()
#define pii pair<int, int>
using namespace std;
const int N = 1e3 + 3;
int dp[N][N], n, m;

int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  string s, t;
  cin >> s >> t;
  n = sz(s);
  m = sz(t);
  string ans = "";
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      if (s[i - 1] == t[j - 1]) {
        dp[i][j] = dp[i - 1][j - 1] + 1;
      } else
        dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]);
    }
  }

  int i = n, j = m;
  while (i > 0 && j > 0) {
    // trace(ans);
    if (s[i - 1] == t[j - 1]) {
      ans = s[i - 1] + ans;
      i--;
      j--;
    } else {
      if (dp[i - 1][j] == dp[i][j])
        i--;
      else
        j--;
    }
  }
  cout << ans << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define ull unsigned long long int
#define ll long long
#define int long long
#define ld long double
#define endl '\n'
const int mod = 1e9 + 7;
#define F first
#define S second
#define pb push_back
#define mp make_pair
#define trace(x) cerr << #x << " is " << x << endl;
#define all(x) x.begin(), x.end()
#define reset(x, val) memset(x, val, sizeof(x))
#define flush fflush(stdout)
#define sz(a) a.size()
#define pii pair<int, int>
using namespace std;
const int N = 3005;
int dp[N][N], n, m;

int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  string s, t;
  cin >> s >> t;
  n = sz(s);
  m = sz(t);
  string ans = "";
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      if (s[i - 1] == t[j - 1]) {
        dp[i][j] = dp[i - 1][j - 1] + 1;
      } else
        dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]);
    }
  }

  int i = n, j = m;
  while (i > 0 && j > 0) {
    // trace(ans);
    if (s[i - 1] == t[j - 1]) {
      ans = s[i - 1] + ans;
      i--;
      j--;
    } else {
      if (dp[i - 1][j] == dp[i][j])
        i--;
      else
        j--;
    }
  }
  cout << ans << endl;
  return 0;
}
Problem p03165 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
int main() {
  string s1;
  string s2;
  cin >> s1 >> s2;
  int l1 = s1.length();
  int l2 = s2.length();
  int ai[l1][l2];
  if (s1[0] == s2[0])
    ai[0][0] = 1;
  else
    ai[0][0] = 0;
  for (int i = 1; i < l2; i++) {
    if (s1[0] == s2[i])
      ai[0][i] = 1;
    else
      ai[0][i] = ai[0][i - 1];
  }
  for (int i = 1; i < l1; i++) {
    if (s1[i] == s2[0])
      ai[i][0] = 1;
    else
      ai[i][0] = ai[i - 1][0];
  }
  for (int n1 = 1; n1 < l1; n1++) {
    for (int n2 = 1; n2 < l2; n2++) {
      if (s1[n1] == s2[n2])
        ai[n1][n2] = ai[n1 - 1][n2 - 1] + 1;
      else
        ai[n1][n2] =
            (ai[n1 - 1][n2] > ai[n1][n2 - 1]) ? ai[n1 - 1][n2] : ai[n1][n2 - 1];
    }
  }
  string s = "";
  int t = 0;
  int i = l1 - 1;
  int j = l2 - 1;
  while (t < 1) {
    if (s1[i] == s2[j]) {
      s = string(1, s1[i]) + s;
      i--;
      j--;
    } else {
      if (i > 0) {
        if (ai[i][j] == ai[i - 1][j])
          i--;
        else if (j > 0) {
          if (ai[i][j] == ai[i][j - 1])
            j--;
        }
      } else if (j > 0) {
        if (ai[i][j] == ai[i][j - 1])
          j--;
      }
    }
    if (i < 0 || j < 0)
      t++;
  }
  cout << s;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int main() {
  string s1;
  string s2;
  cin >> s1 >> s2;
  int l1 = s1.length();
  int l2 = s2.length();
  int ai[l1][l2];
  if (s1[0] == s2[0])
    ai[0][0] = 1;
  else
    ai[0][0] = 0;
  for (int i = 1; i < l2; i++) {
    if (s1[0] == s2[i])
      ai[0][i] = 1;
    else
      ai[0][i] = ai[0][i - 1];
  }
  for (int i = 1; i < l1; i++) {
    if (s1[i] == s2[0])
      ai[i][0] = 1;
    else
      ai[i][0] = ai[i - 1][0];
  }
  for (int n1 = 1; n1 < l1; n1++) {
    for (int n2 = 1; n2 < l2; n2++) {
      if (s1[n1] == s2[n2])
        ai[n1][n2] = ai[n1 - 1][n2 - 1] + 1;
      else
        ai[n1][n2] =
            (ai[n1 - 1][n2] > ai[n1][n2 - 1]) ? ai[n1 - 1][n2] : ai[n1][n2 - 1];
    }
  }
  string s = "";
  int t = 0;
  int i = l1 - 1;
  int j = l2 - 1;
  while (t < 1) {
    if (s1[i] == s2[j]) {
      s = string(1, s1[i]) + s;
      i--;
      j--;
    } else {
      if (i > 0) {
        if (ai[i][j] == ai[i - 1][j])
          i--;
        else if (j > 0) {
          if (ai[i][j] == ai[i][j - 1])
            j--;
        }
      } else if (j > 0) {
        if (ai[i][j] == ai[i][j - 1])
          j--;
      }
    }
    if (i < 0 || j < 0)
      t++;
    if ((i == 0 && j == 0) && s1[i] != s2[i])
      t++;
  }
  cout << s;
  return 0;
}
Problem p03165 (C++)

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

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

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

  string ans;
  int i = s.size(), j = t.size();
  while (ans.size() != dp[s.size()][t.size()]) {
    if (dp[i - 1][j] == dp[i][j])
      i--;
    else if (dp[i][j - 1] == dp[i][j])
      j--;
    else {
      i--, j--;
      ans += s[i];
    }
  }
  reverse(ans.begin(), ans.end());
  cout << ans << endl;

  return 0;
}


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

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

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

  string ans;
  int i = s.size(), j = t.size();
  while (ans.size() != dp[s.size()][t.size()]) {
    if (dp[i - 1][j] == dp[i][j])
      i--;
    else if (dp[i][j - 1] == dp[i][j])
      j--;
    else {
      i--, j--;
      ans += s[i];
    }
  }
  reverse(ans.begin(), ans.end());
  cout << ans << endl;

  return 0;
}
Problem p03165 (C++)

Buggy submission (status: Runtime Error):
#include "algorithm"
#include "cstring"
#include "iostream"
#include "math.h"
#include "string"
#include "vector"

using namespace std;
typedef long long ll;

string s, t;
int dpsize[3010][3010]; // i,jでのlcs

int main() {
  cin >> s >> t;
  int size_s = s.size();
  int size_t = t.size();
  for (int i = 0; i < 3010; i++) {
    dpsize[0][i] = 0;
    dpsize[i][0] = 0;
  }
  for (int i = 0; i < size_s; i++) {
    for (int j = 0; j < size_t; j++) {
      if (s[i] == t[j]) {
        dpsize[i + 1][j + 1] = dpsize[i][j] + 1;
      } else if (dpsize[i + 1][j] > dpsize[i][j + 1]) {
        dpsize[i + 1][j + 1] = dpsize[i + 1][j];
      } else {
        dpsize[i + 1][j + 1] = dpsize[i][j + 1];
      }
    }
  }

  string ans = "";
  int i = size_s;
  int j = size_t;
  while (i > 0 || j > 0) {
    if (dpsize[i][j] == dpsize[i - 1][j]) {
      i--;
    }
    if (dpsize[i][j] == dpsize[i][j - 1]) {
      j--;
    }
    if (dpsize[i][j] > dpsize[i - 1][j] && dpsize[i][j] > dpsize[i][j - 1]) {
      ans += s[i - 1];
      i--;
      j--;
    }
  }
  reverse(ans.begin(), ans.end());
  cout << ans << "\n";
}

Fixed submission:
#include "algorithm"
#include "cstring"
#include "iostream"
#include "math.h"
#include "string"
#include "vector"

using namespace std;
typedef long long ll;

string s, t;
int dpsize[3010][3010]; // i,jでのlcs

int main() {
  cin >> s >> t;
  int size_s = s.size();
  int size_t = t.size();
  for (int i = 0; i < 3010; i++) {
    dpsize[0][i] = 0;
    dpsize[i][0] = 0;
  }
  for (int i = 0; i < size_s; i++) {
    for (int j = 0; j < size_t; j++) {
      if (s[i] == t[j]) {
        dpsize[i + 1][j + 1] = dpsize[i][j] + 1;
      } else if (dpsize[i + 1][j] > dpsize[i][j + 1]) {
        dpsize[i + 1][j + 1] = dpsize[i + 1][j];
      } else {
        dpsize[i + 1][j + 1] = dpsize[i][j + 1];
      }
    }
  }

  string ans = "";
  int i = size_s;
  int j = size_t;
  while (dpsize[i][j] > 0) {
    if (dpsize[i][j] == dpsize[i - 1][j]) {
      i--;
    }
    if (dpsize[i][j] == dpsize[i][j - 1]) {
      j--;
    }
    if (dpsize[i][j] > dpsize[i - 1][j] && dpsize[i][j] > dpsize[i][j - 1]) {
      ans += s[i - 1];
      i--;
      j--;
    }
  }
  reverse(ans.begin(), ans.end());
  cout << ans << "\n";
}