alexjercan/bugnet huggingface dataset:
Problem p00494 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define range(i, a, b) for (int i = (a); i < (b); i++)
#define rep(i, b) for (int i = 0; i < (b); i++)
#define all(a) (a).begin(), (a).end()
#define show(x) cerr << #x << " = " << (x) << endl;
// const int INF = 1e8;
using namespace std;

vector<pair<char, int>> v;

int main() {
  string s;
  cin >> s;
  s += "banpei";

  char cur = 'x';
  int cnt;
  rep(i, s.size()) {
    if (cur != s[i]) {
      v.emplace_back(cur, cnt);
      cur = s[i];
      cnt = 1;
    } else {
      cnt++;
    }
  }

  int ans = 0;
  rep(i, v.size() - 2) {
    if (v[i].first == 'J' && v[i + 1].first == 'O' && v[i + 2].first == 'I') {
      int a = v[i].second;
      int b = v[i + 1].second;
      int c = v[i + 2].second;
      if (a >= b && b <= c)
        ans = max(ans, b);
    }
  }

  //    rep(i,v.size()){ cout << v[i].first << ' '; }cout << endl;
  //    rep(i,v.size()){ cout << v[i].second << ' '; }cout << endl;

  cout << ans << endl;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define range(i, a, b) for (int i = (a); i < (b); i++)
#define rep(i, b) for (int i = 0; i < (b); i++)
#define all(a) (a).begin(), (a).end()
#define show(x) cerr << #x << " = " << (x) << endl;
// const int INF = 1e8;
using namespace std;

vector<pair<char, int>> v;

int main() {
  string s;
  cin >> s;
  s += "x";

  char cur = 'x';
  int cnt;
  rep(i, s.size()) {
    if (cur != s[i]) {
      if (cur != 'x') {
        v.emplace_back(cur, cnt);
      }
      cur = s[i];
      cnt = 1;
    } else {
      cnt++;
    }
  }

  int ans = 0;
  rep(i, v.size() - 2) {
    if (v[i].first == 'J' && v[i + 1].first == 'O' && v[i + 2].first == 'I') {
      ans = max(ans, min(min(v[i].second, v[i + 1].second), v[i + 2].second));
    }
  }

  //    rep(i,v.size()){ cout << v[i].first << ' '; }cout << endl;
  //    rep(i,v.size()){ cout << v[i].second << ' '; }cout << endl;

  cout << ans << endl;
}
Problem p00494 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <string>
#include <vector>
int map[105][105] = {0};
using namespace std;
int main() {
  string hoge, temp1 = "", temp2 = "";
  cin >> hoge;
  hoge += 'E';
  int ans = 0, i = 0, cntJ = 0, cntO = 0, cntI = 0;
  bool flagO = false, flagI = false;
  while (1) {
    cntJ = 0;
    cntO = 0;
    cntI = 0;
    while (hoge[i] == 'J') {
      cntJ++;
      i++;
      flagO = true;
    }
    while (flagO && hoge[i] == 'O') {
      cntO++;
      i++;
      flagI = true;
    }
    while (flagI && hoge[i] == 'I') {
      cntI++;
      i++;
    }
    if (!flagI || !flagO)
      i++;
    if (flagI && (cntJ >= cntO) && (cntO <= cntI))
      ans = max(ans, cntO);
    flagI = false;
    flagO = false;
    if (i > hoge.size())
      break;
  }

  cout << ans << endl;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <string>
#include <vector>
int map[105][105] = {0};
using namespace std;
int main() {
  string hoge, temp1 = "", temp2 = "";
  cin >> hoge;
  hoge += 'E';
  int ans = 0, i = 0, cntJ = 0, cntO = 0, cntI = 0;
  bool flagO = false, flagI = false;
  while (1) {
    cntJ = 0;
    cntO = 0;
    cntI = 0;
    while (hoge[i] == 'J') {
      cntJ++;
      i++;
      flagO = true;
    }
    while (flagO && hoge[i] == 'O') {
      cntO++;
      i++;
      flagI = true;
    }
    flagO = false;
    while (flagI && hoge[i] == 'I') {
      cntI++;
      i++;
    }
    if (!flagI)
      i++;
    if (flagI && (cntJ >= cntO) && (cntO <= cntI))
      ans = max(ans, cntO);
    flagI = false;
    if (hoge[i] == 'E')
      break;
  }

  cout << ans << endl;
}
Problem p00494 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cstdio>
#include <iostream>
#include <string>
using namespace std;

int main() {
  char str[1000002];
  while (cin >> str) {
    static int a[3][1000001];
    int ans = 0;

    int size;
    for (size = 0; str[size] != '\\0'; size++)
      ;

    for (int i = 0; i < 3; i++)
      for (int j = 0; j < size; j++)
        a[i][j] = 0;
    if (str[0] == 'J')
      a[0][0] = 1;
    else if (str[0] == 'O')
      a[1][0] = 1;
    else if (str[0] == 'I')
      a[2][0] = 1;

    for (int i = 1; i < size; i++) {
      if (str[i] == 'J')
        a[0][i] = a[0][i - 1] + 1;
      else if (str[i] == 'O')
        a[1][i] = a[1][i - 1] + 1;
      else if (str[i] == 'I')
        a[2][i] = a[2][i - 1] + 1;
    }

    for (int i = 0; i < size - 2; i++) {
      if (a[0][i] > 0 && a[0][i + 1] == 0) {
        int c = a[0][i];
        if (a[1][i + 1] == 0)
          continue;
        i++;
        for (; i < size; i++) {
          if (a[1][i] > 0 && a[1][i + 1] == 0) {
            if (a[1][i] > c)
              break;
            c = a[1][i];
            i++;
            int c2 = i;
            for (; i < size; i++)
              if (a[2][i] == 0)
                break;
            if (i - c2 < c) {
              i--;
              break;
            }
            ans = max(ans, c);
            i--;
            break;
          }
        }
      }
    }

    cout << ans << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
#include <string>
using namespace std;

int main() {
  string str;
  while (cin >> str) {
    static int a[3][1000001];
    int ans = 0;
    for (int i = 0; i < 3; i++)
      for (int j = 0; j < str.size(); j++)
        a[i][j] = 0;
    if (str[0] == 'J')
      a[0][0] = 1;
    else if (str[0] == 'O')
      a[1][0] = 1;
    else if (str[0] == 'I')
      a[2][0] = 1;

    for (int i = 1; i < str.size(); i++) {
      if (str[i] == 'J')
        a[0][i] = a[0][i - 1] + 1;
      else if (str[i] == 'O')
        a[1][i] = a[1][i - 1] + 1;
      else if (str[i] == 'I')
        a[2][i] = a[2][i - 1] + 1;
    }

    for (int i = 0; i < str.size() - 2; i++) {
      if (a[0][i] > 0 && a[0][i + 1] == 0) {
        int c = a[0][i];
        i++;
        if (a[1][i] > 0) {
          for (; i < str.size() - 1; i++) {
            if (a[1][i] > 0 && a[1][i + 1] == 0) {
              if (a[1][i] > c)
                break;
              c = a[1][i];
              i++;
              int c2 = i;
              for (; i < str.size(); i++)
                if (a[2][i] == 0)
                  break;
              if (i - c2 < c)
                break;
              ans = max(ans, c);
            }
          }
        }
      }
    }

    cout << ans << endl;
  }
  return 0;
}
Problem p00494 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

using namespace std;

int main() {
  string q;
  cin >> q;
  int q_len = q.size();

  int J_length = 0;
  int O_length = 0;
  int I_length = 0;

  int check = 0;
  int ans = 0;
  for (int i = 0; i < q_len; ++i) {
    if (q.at(i) == 'J') {
      if (check != 1) {
        J_length = 0;
        O_length = 0;
        I_length = 0;
        ++J_length;
        check = 1;
      } else if (check == 1) {
        ++J_length;
      }
    } else if (q.at(i) == 'O') {
      if (check == 1) {
        ++O_length;
        check = 2;
      } else if (check == 2) {
        ++O_length;
      } else {
        J_length = 0;
        O_length = 0;
        I_length = 0;
        check = 0;
      }
    } else if (q.at(i) == 'I') {
      if (check == 2) {
        ++I_length;
        check = 3;
      } else if (check == 3) {
        ++I_length;
      } else {
        J_length = 0;
        O_length = 0;
        I_length = 0;
        check = 0;
      }
    }

    if (check == 3 && J_length >= O_length && O_length <= I_length) {
      ans = max(ans, O_length);
    }
  }
  cout << ans << endl;

  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

int main() {
  string q;
  cin >> q;
  int q_len = q.size();
  int q_l = q_len / 3;

  int ans = 0;
  string JOI;
  string J("J");
  string O("O");
  string I("I");
  string J2;
  string O2;
  string I2;
  for (int i = 1; i <= q_l; ++i) {
    // if(i != 1){
    /*JOI.insert(JOI.begin() + (i - 1),'J');
    JOI.insert(JOI.begin() + i + (i - 1),'O');
    JOI.insert(JOI.begin() + i + i + (i - 1),'I');*/
    //}
    J2 += J;
    O2 += O;
    I2 += I;
    JOI = J2;
    JOI += O2;
    JOI += I2;
    int a = (int)q.find(JOI);

    if (a != -1) {
      ans = i;
    }
  }
  cout << ans << endl;

  return 0;
}
Problem p00494 (C++) — buggy submission, verdict: Runtime Error

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

#define FOR(i, a, b) for (int i = a; i <= b; i++)
#define FORD(i, a, b) for (int i = a; i >= b; i--)
#define FORV(i, a, b) for (int i = a; i * i <= b; i++)
#define Forv(i, a, b) for (int i = a; i * i < b; i++)
#define For(i, a, b) for (int i = a; i < b; i++)
#define Ford(i, a, b) for (int i = a; i > b; i--)
#define Fill(s, a) memset(s, a, sizeof(s))
#define pb push_back
#define mp make_pair
#define fi first
#define se second

string s;
vector<pair<int, int>> a;
int ans = 0;

int main() {
  cin >> s;
  For(i, 0, s.size()) {
    int cnt;
    if (s[i] == 'J') {
      cnt = 0;
      while (s[i] == 'J') {
        cnt++;
        i++;
      }
      a.pb(mp(1, cnt));
    }
    if (s[i] == 'O') {
      cnt = 0;
      while (s[i] == 'O') {
        cnt++;
        i++;
      }
      a.pb(mp(2, cnt));
    }
    if (s[i] == 'I') {
      cnt = 0;
      while (s[i] == 'I') {
        cnt++;
        i++;
      }
      a.pb(mp(3, cnt));
    }
    i--;
  }
  if (a.size() <= 2) {
    cout << "0\\n";
    return 0;
  }
  For(i, 0, a.size() - 2) if (a[i].fi == 1 && a[i + 1].fi == 2 &&
                              a[i + 2].fi == 3 &&
                              min(a[i].se, min(a[i + 1].se, a[i + 2].se)) ==
                                  a[i + 1].se) {
    ans = max(ans, min(a[i].se, min(a[i + 1].se, a[i + 2].se)));
    i += 2;
  }
  cout << ans << '\\n';
}

Error: 0

Fixed submission:

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

#define FOR(i, a, b) for (int i = a; i <= b; i++)
#define FORD(i, a, b) for (int i = a; i >= b; i--)
#define FORV(i, a, b) for (int i = a; i * i <= b; i++)
#define Forv(i, a, b) for (int i = a; i * i < b; i++)
#define For(i, a, b) for (int i = a; i < b; i++)
#define Ford(i, a, b) for (int i = a; i > b; i--)
#define Fill(s, a) memset(s, a, sizeof(s))
#define pb push_back
#define mp make_pair
#define fi first
#define se second

string s;
vector<pair<int, int>> a;
int ans = 0;

int main() {
  cin >> s;
  For(i, 0, s.size()) {
    int cnt;
    if (s[i] == 'J') {
      cnt = 0;
      while (s[i] == 'J') {
        cnt++;
        i++;
      }
      a.pb(mp(1, cnt));
    }
    if (s[i] == 'O') {
      cnt = 0;
      while (s[i] == 'O') {
        cnt++;
        i++;
      }
      a.pb(mp(2, cnt));
    }
    if (s[i] == 'I') {
      cnt = 0;
      while (s[i] == 'I') {
        cnt++;
        i++;
      }
      a.pb(mp(3, cnt));
    }
    i--;
  }
  For(i, 0,
      a.size() - 2) if (a[i].fi == 1 && a[i + 1].fi == 2 && a[i + 2].fi == 3) {
    ans = max(ans, min(a[i].se, min(a[i + 1].se, a[i + 2].se)));
    i += 2;
  }
  cout << ans << '\\n';
}
Problem p00495 (C++) — buggy submission, verdict: Memory Limit Exceeded

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

int main() {
  int a, b;
  cin >> a >> b;
  vector<int> an(a), br(b);
  for (auto &i : an)
    cin >> i;
  for (auto &i : br)
    cin >> i;
  int res = 0;
  for (auto i = 0; i < b; i++) {
    int use = 0;
    for (auto &j : an) {
      if (j == br[use + i])
        use++;
      if (use == b)
        break;
    }
    res = max(res, use);
  }
  cout << res << endl;
}

Error: MLE

Fixed submission:

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

int main() {
  int a, b;
  cin >> a >> b;
  vector<int> an(a), br(b);
  for (auto &i : an)
    cin >> i;
  for (auto &i : br)
    cin >> i;
  vector<vector<int>> dp(a + 1, vector<int>(b + 1));
  int res = 0;
  for (auto i = 0; i < b; i++) {
    int use = 0;
    for (auto &j : an) {
      if (j == br[use + i])
        use++;
      if (use == b)
        break;
    }
    res = max(res, use);
  }
  cout << res << endl;
}
Problem p00495 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <algorithm>
#include <iostream>
using namespace std;

int a, b, acards[5001], bcards[5001], mx = 0, mxcards[5001], dp1[5001],
                                      dp2[5001];

int main() {
  cin >> a >> b;
  for (int i = 1; i <= a; i++)
    cin >> acards[i];
  for (int i = 1; i <= b; i++)
    cin >> bcards[i];

  for (int i = 1; i <= a; i++) {
    for (int j = 1; j <= b; j++) {
      if (acards[i] == bcards[j]) {
        dp2[j] = mxcards[j - 1] + 1;
        mx = max(mx, dp2[j]);
      }
    }
    for (int j = 1; j <= b; j++) {
      dp1[j] = dp2[j];
      mxcards[j] = max(mxcards[j], dp2[j]);
    }
  }
  cout << mx << endl;
  return 0;
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <iostream>
using namespace std;

int a, b, acards[5001], bcards[5001], mx = 0, mxcards[5001], dp[5001][5001];

int main() {
  cin >> a >> b;
  for (int i = 1; i <= a; i++)
    cin >> acards[i];
  for (int i = 1; i <= b; i++)
    cin >> bcards[i];

  for (int i = 1; i <= a; i++) {
    for (int j = 1; j <= b; j++) {
      if (acards[i] == bcards[j]) {
        dp[i][j] = mxcards[j - 1] + 1;
        mx = max(mx, dp[i][j]);
      }
    }
    for (int j = 1; j <= b; j++) {
      mxcards[j] = max(mxcards[j], dp[i][j]);
    }
  }
  cout << mx << endl;
  return 0;
}
Problem p00495 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <bits/stdc++.h>
#define int long long
#define PB push_back
#define MP make_pair
#define F first
#define S second
using namespace std;

int a, b, ac[5010], bc[5010];
int dp[2][5010];
int ans = 0;

signed main() {
  cin >> a >> b;
  for (int i = 1; i <= a; i++) {
    cin >> ac[i];
  }
  for (int i = 1; i <= b; i++) {
    cin >> bc[i];
  }

  for (int i = 1; i <= b; i++) {
    int mx = 0;
    memset(dp[i % 2], 0, sizeof dp[i % 2]);

    for (int j = 1; j <= a; j++) {
      if (ac[j] == bc[i]) {
        dp[i % 2][j] = mx + 1;
        ans = max(ans, dp[i % 2][j]);
      }
      mx = max(mx, dp[!(i % 2)][j]);
    }
  }

  cout << ans << endl;
  return 0;
}

Error: MLE

Fixed submission:

#include <bits/stdc++.h>
#define int long long
#define PB push_back
#define MP make_pair
#define F first
#define S second
using namespace std;

int a, b, ac[5010], bc[5010];
int dp[5010][5010];
int ans = 0;

signed main() {
  cin >> a >> b;
  for (int i = 1; i <= a; i++) {
    cin >> ac[i];
  }
  for (int i = 1; i <= b; i++) {
    cin >> bc[i];
  }

  for (int i = 1; i <= b; i++) {
    int mx = 0;
    for (int j = 1; j <= a; j++) {
      if (ac[j] == bc[i]) {
        dp[i][j] = mx + 1;
        ans = max(ans, dp[i][j]);
      }
      mx = max(mx, dp[i - 1][j]);
    }
  }

  cout << ans << endl;
  return 0;
}
Problem p00495 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef pair<int, int> P;
typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vll;
#define pu push
#define pb push_back
#define mp make_pair
#define eps 1e-9
#define INF 2000000000
#define sz(x) ((int)(x).size())
#define fi first
#define sec second
#define SORT(x) sort((x).begin(), (x).end())
int main() {
  int n, m, ans = 0;
  vector<int> v1, v2;
  cin >> n >> m;
  for (int i = 0; i < n; i++) {
    int k;
    cin >> k;
    v1.pb(k);
  }
  for (int i = 0; i < m; i++) {
    int k;
    cin >> k;
    v2.pb(k);
  }
  for (int i = 0; i < v2.size(); i++) {
    int s = i, t = 0;
    while (s < v2.size() && t < v1.size()) {
      if (v2[s] == v1[t]) {
        s++;
        ans = max(ans, s - i);
      }
      t++;
    }
  }
  cout << ans << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef pair<int, int> P;
typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vll;
#define pu push
#define pb push_back
#define mp make_pair
#define eps 1e-9
#define INF 2000000000
#define sz(x) ((int)(x).size())
#define fi first
#define sec second
#define SORT(x) sort((x).begin(), (x).end())
int main() {
  int n, m, ans = 0;
  vector<int> v1, v2;
  cin >> n >> m;
  for (int i = 0; i < n; i++) {
    int k;
    cin >> k;
    v1.pb(k);
  }
  for (int i = 0; i < m; i++) {
    int k;
    cin >> k;
    v2.pb(k);
  }
  for (int i = min(n, m); i >= 0; i--) {
    for (int j = 0; j <= m - i; j++) {
      bool k = false;
      int s = j, t = 0;
      while (1) {
        if (s == j + i) {
          k = true;
          break;
        }
        if (t == v1.size())
          break;
        if (v1[t] == v2[s])
          s++;
        t++;
      }
      if (k) {
        ans = i;
        goto end;
      }
    }
  }
end:;
  cout << ans << endl;
  return 0;
}
Problem p00495 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

#define FOR(i, a, b) for (long long i = (a); i < (b); i++)
#define REP(i, N) for (long long i = 0; i < (N); i++)
#define ALL(s) (s).begin(), (s).end()
#define fi first
#define se second

#define PI acos(-1.0)
#define INF 1000000007
#define EPS 1e-10
#define MAX_N 20000
#define MAX_M 16

typedef long long ll;
typedef pair<ll, ll> P;
typedef pair<double, double> PD;
typedef pair<string, ll> PS;
typedef vector<ll> V;
typedef pair<P, char> PC;

int a, b;
int ca[10000], cb[10000];

int main() {
  cin >> a >> b;
  REP(i, a) cin >> ca[i];
  REP(i, b) cin >> cb[i];
  int ans = 0, co = 0, bpos = 0;
  REP(i, b) {
    bpos = i;
    co = 0;
    REP(j, a) {
      if (ca[j] == cb[bpos]) {
        co++;
        bpos++;
      }
    }
    ans = max(ans, co);
  }
  cout << ans << endl;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

#define FOR(i, a, b) for (long long i = (a); i < (b); i++)
#define REP(i, N) for (long long i = 0; i < (N); i++)
#define ALL(s) (s).begin(), (s).end()
#define fi first
#define se second

#define PI acos(-1.0)
#define INF 1000000007
#define EPS 1e-10
#define MAX_N 20000
#define MAX_M 16

typedef long long ll;
typedef pair<ll, ll> P;
typedef pair<double, double> PD;
typedef pair<string, ll> PS;
typedef vector<ll> V;
typedef pair<P, char> PC;

int a, b;
int ca[10000], cb[10000];

int main() {
  cin >> a >> b;
  REP(i, a) cin >> ca[i];
  REP(i, b) cin >> cb[i];
  int ans = 0, co = 0, pos = 0;
  REP(i, b) {
    FOR(j, pos, a) {
      if (ca[j] == cb[i]) {
        pos = j + 1;
        co++;
        if (j != a - 1)
          break;
      }
      if (j == a - 1) {
        ans = max(ans, co);
        co = 0;
        pos = 0;
        i--;
      }
    }
  }
  cout << ans << endl;
}
Problem p00495 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <stdio.h>
short dp[5001][5001];
int a[5000];
int b[5000];
int max(int a, int b) { return a > b ? a : b; }
int main() {
  int n, m;
  scanf("%d %d", &n, &m);
  int i, j;
  for (i = 0; i < n; i++)
    scanf("%d", &a[i]);
  for (i = 0; i < m; i++)
    scanf("%d", &b[i]);
  for (i = 0; i <= n; i++)
    for (j = 0; j <= m; j++)
      dp[i][j] = 0;
  for (i = 1; i <= n; i++) {
    for (j = 1; j <= m; j++) {
      dp[i][j] = dp[i - 1][j];
      if (a[i - 1] == b[j - 1])
        dp[i][j] = max(dp[i][j], dp[i - 1][j - 1] + 1);
    }
  }
  int ans = 0;
  for (i = 0; i <= m; i++)
    ans = max(ans, dp[n][i]);
  printf("%d\\n", ans);
  return 0;
}

Error: MLE

Fixed submission:

#include <stdio.h>
int dp[5001][5001];
int a[5000];
int b[5000];
int max(int a, int b) { return a > b ? a : b; }
int main() {
  int n, m;
  scanf("%d %d", &n, &m);
  int i, j;
  for (i = 0; i < n; i++)
    scanf("%d", &a[i]);
  for (i = 0; i < m; i++)
    scanf("%d", &b[i]);
  for (i = 0; i <= n; i++)
    for (j = 0; j <= m; j++)
      dp[i][j] = 0;
  for (i = 1; i <= n; i++) {
    for (j = 1; j <= m; j++) {
      dp[i][j] = dp[i - 1][j];
      if (a[i - 1] == b[j - 1])
        dp[i][j] = max(dp[i][j], dp[i - 1][j - 1] + 1);
    }
  }
  int ans = 0;
  for (i = 0; i <= m; i++)
    ans = max(ans, dp[n][i]);
  printf("%d\\n", ans);
  return 0;
}
Problem p00495 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cstdio>
#include <queue>
#include <set>
#define REP(i, n) for (int i = 0; i < (int)(n); i++)

inline int getInt() {
  int s;
  scanf("%d", &s);
  return s;
}

using namespace std;

int main() {
  int a = getInt();
  int b = getInt();

  vector<int> va(a);
  vector<int> vb(b);

  REP(i, a) va[i] = getInt();
  REP(i, b) vb[i] = getInt();

  int ans = 0;
  REP(i, b) {
    int ptr = i;
    REP(j, a) if (ptr != b) {
      if (va[j] == vb[ptr]) {
        ptr++;
      }
    }
    ans = max(ans, ptr - i);
  }

  printf("%d\\n", ans);

  return 0;
}

Error: TLE

Fixed submission:

#include <cstdio>
#include <iostream>
#include <vector>

#define REP(i, n) for (int i = 0; i < (int)(n); i++)

using namespace std;

inline int getInt() {
  int s;
  scanf("%d", &s);
  return s;
}

int main() {
  while (true) {
    int n1 = getInt();
    int n2 = getInt();

    if (n1 + n2 == 0)
      break;

    vector<vector<int>> dp(n2 + 1, vector<int>(n1 + 1));

    vector<int> a(n1);
    vector<int> b(n2);

    REP(i, n1) a[i] = getInt();
    REP(i, n2) b[i] = getInt();

    REP(i, n2 + 1) REP(j, n1 + 1) {
      if (i != n2)
        dp[i + 1][j] = max(dp[i + 1][j], dp[i][j]);
      if (j != n1)
        dp[i][j + 1] = max(dp[i][j + 1], dp[i][j]);
      if (i != n2 && j != n1 && b[i] == a[j])
        dp[i + 1][j + 1] = max(dp[i + 1][j + 1], 1 + dp[i][j]);
    }

    cout << dp[n2][n1] << endl;
  }
  return 0;
}
Problem p00495 (C++) — buggy submission, verdict: Memory Limit Exceeded

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

#define loop(i, n) for (int i = 1; i <= n; i++)
int dp[2][5002];
int a[5002];
int b[5002];

int main() {
  int A, B;
  cin >> A >> B;
  loop(i, A) cin >> a[i];
  loop(j, B) cin >> b[j];
  int m = 0;
  loop(i, A) loop(j, B) {
    dp[i & 1][j] = dp[i & 1 ^ 1][j];
    if (a[i] == b[j]) {
      dp[i & 1][j] = dp[i & 1 ^ 1][j - 1] + 1;
    }
    m = max(m, dp[i & 1][j]);
  }
  cout << m << endl;
  return 0;
}

Error: MLE

Fixed submission:

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

#define loop(i, n) for (int i = 1; i <= n; i++)
int dp[5002][5002];
int a[5002];
int b[5002];

int main() {
  int A, B;
  cin >> A >> B;
  loop(i, A) cin >> a[i];
  loop(j, B) cin >> b[j];
  int m = 0;
  loop(i, A) loop(j, B) {
    dp[i][j] = dp[i - 1][j];
    if (a[i] == b[j]) {
      dp[i][j] = dp[i - 1][j - 1] + 1;
    }
    m = max(m, dp[i][j]);
  }
  cout << m << endl;
  return 0;
}