alexjercan/bugnet huggingface dataset:
Problem p00495 (C++) — buggy submission, verdict: Memory Limit Exceeded

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

short int n, m, res = 0, a[5001], b[5001], best[5001][5001];

int main() {
  cin >> m >> n;
  for (int i = 1; i <= m; i++)
    cin >> a[i];
  for (int j = 1; j <= n; j++)
    cin >> b[j];
  for (int i = 1; i <= m; i++)
    for (int j = 1; j <= n; j++) {
      if (a[i] == b[j])
        best[i][j] = best[i - 1][j - 1] + 1;
      else
        best[i][j] = best[i - 1][j];
      res = max(res, best[i][j]);
    }
  cout << res << '\\n';
  return 0;
}

Error: MLE

Fixed submission:

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

int n, m, res = 0, a[5001], b[5001], best[5001][5001];

int main() {
  scanf("%d%d", &m, &n);
  for (int i = 1; i <= m; i++)
    scanf("%d", &a[i]);
  for (int j = 1; j <= n; j++)
    scanf("%d", &b[j]);
  for (int i = 1; i <= m; i++)
    for (int j = 1; j <= n; j++) {
      if (a[i] == b[j])
        best[i][j] = best[i - 1][j - 1] + 1;
      else
        best[i][j] = best[i - 1][j];
      res = max(res, best[i][j]);
    }
  cout << res << '\\n';
  return 0;
}
Problem p00495 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define RFOR(i, a, b) for (int i = (b)-1; i >= (a); i--)
#define REP(i, n) for (int i = 0; i < (n); i++)
#define RREP(i, n) for (int i = n - 1; i >= 0; i--)

#define PB push_back
#define INF (1 << 29)
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define CLR(a) memset(a, 0, sizeof(a))
const int dx[] = {-1, 0, 0, 1}, dy[] = {0, 1, -1, 0};

typedef long long int ll;

using namespace std;

int a, b;
int main() {
  cin >> a >> b;
  vector<int> c1(a);
  vector<int> c2(b);
  int dp[2][5001];
  memset(dp, 0, sizeof(dp));
  REP(i, a) { cin >> c1[i]; }
  REP(i, b) { cin >> c2[i]; }
  int ans = 0;
  REP(i, b) {
    REP(j, a) {
      if (c2[i] == c1[j]) {
        dp[0][j + 1] = dp[1][j] + 1;
      } else {
        dp[0][j + 1] = dp[0][j];
      }
      ans = max(ans, dp[0][j + 1]);
    }
    swap(dp[0], dp[1]);
  }
  cout << ans << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define RFOR(i, a, b) for (int i = (b)-1; i >= (a); i--)
#define REP(i, n) for (int i = 0; i < (n); i++)
#define RREP(i, n) for (int i = n - 1; i >= 0; i--)

#define PB push_back
#define INF (1 << 29)
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define CLR(a) memset(a, 0, sizeof(a))
const int dx[] = {-1, 0, 0, 1}, dy[] = {0, 1, -1, 0};

typedef long long int ll;

using namespace std;

int a, b;
bool isin(vector<int> &v, vector<int> &c) {
  int x = 0;
  int count = 0;
  REP(i, v.size()) {
    FOR(j, x, c.size()) {
      if (v[i] == c[j]) {
        x = j + 1;
        count++;
        break;
      }
    }
  }
  return count == v.size();
}

int main() {
  cin >> a >> b;
  vector<int> c1(a);
  vector<int> c2(b);
  REP(i, a) { cin >> c1[i]; }
  REP(i, b) { cin >> c2[i]; }
  int ans = 0;
  REP(i, b) {
    REP(j, b - i) {
      vector<int> temp(b - (j + i));
      FOR(k, i, b - j) { temp[k - i] = c2[k]; }
      if (isin(temp, c1)) {
        /*
        REP(k,temp.size()) cout << " " << temp[k];
        cout << endl;
        */
        ans = max((int)(temp.size()), ans);
        break;
      }
    }
  }
  cout << ans << endl;
  return 0;
}
Problem p00495 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
int dp[5001];
int a[5001], b[5001];
int main() {
  int A, B;
  scanf("%d %d", &A, &B);
  for (int i = 1; i <= A; i++) {
    scanf("%d", &a[i]);
  }
  for (int i = 1; i <= B; i++) {
    scanf("%d", &b[i]);
  }
  int ans = 0;
  for (int i = 1; i <= A; i++) {
    for (int j = B; j >= 1; j--) {
      if (a[i] == b[j]) {
        dp[j] = dp[j - 1] + 1;
      }
      ans = max(ans, dp[j]);
    }
  }
  printf("%d\\n", ans);
}

Error: MLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int dp[5001][5001];
int a[5001], b[5001];
int main() {
  int A, B;
  scanf("%d %d", &A, &B);
  for (int i = 1; i <= A; i++) {
    scanf("%d", &a[i]);
  }
  for (int i = 1; i <= B; i++) {
    scanf("%d", &b[i]);
  }
  int ans = 0;
  for (int i = 1; i <= A; i++) {
    for (int j = 1; j <= B; j++) {
      if (a[i] == b[j]) {
        dp[i][j] = dp[i - 1][j - 1] + 1;
      } else {
        dp[i][j] = dp[i - 1][j];
      }
      ans = max(ans, dp[i][j]);
    }
  }
  printf("%d\\n", ans);
}
Problem p00495 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <algorithm>
#include <cstring>
#include <iostream>
using namespace std;
#define REP(i, n) for (int i = 0; i < (int)n; ++i)
int a[5000], b[5000], dp[5001];

int main() {
  int A, B;
  while (cin >> A >> B) {
    REP(i, A) cin >> a[i];
    REP(i, B) cin >> b[i];
    memset(dp, 0, sizeof(dp));
    REP(i, A) {
      for (int j = B - 1; j >= 0; --j) {
        if (a[i] == b[j]) {
          dp[j + 1] = dp[j] + 1;
        }
      }
    }
    cout << *max_element(dp, dp + B + 1) << endl;
  }
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cassert>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <valarray>
#include <vector>
using namespace std;

#define REP(i, n) for (int i = 0; i < (int)n; ++i)
#define FOR(i, c)                                                              \\
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define ALL(c) (c).begin(), (c).end()
typedef long long ll;
typedef pair<int, int> pii;
const int INF = 1 << 29;
const double PI = acos(-1);
const double EPS = 1e-8;

int a[5000];
int b[5000];
int dp[5001][5001];
int main() {
  int A, B;
  while (cin >> A >> B) {
    REP(i, A) cin >> a[i];
    REP(i, B) cin >> b[i];
    int ans = 0;
    dp[0][0] = 0;
    REP(i, A) {
      REP(j, B) {
        if (a[i] == b[j]) {
          dp[i + 1][j + 1] = dp[i][j] + 1;
          ans = max(ans, dp[i + 1][j + 1]);
        } else
          dp[i + 1][j + 1] = dp[i][j + 1];
      }
    }
    cout << ans << endl;
  }
}
Problem p00495 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>

using namespace std;

int main() {
  int A, B;
  int indB = 0;

  cin >> A >> B;

  int a[A];
  int b[B];

  for (int i = 0; i < A; ++i) {
    cin >> a[i];
  }
  for (int i = 0; i < B; ++i) {
    cin >> b[i];
  }

  int dp[A][2];

  if (a[0] == b[0])
    dp[0][0] = 1;
  else
    dp[0][0] = 0;

  for (int i = 1; i < A; ++i) {
    if (a[i] == b[0])
      dp[i][0] = 1;
    else
      dp[i][0] = max(dp[i - 1][0], 0);
  }

  int ans = 0;

  for (int j = 1; j < B; ++j) {
    indB = j % 2;
    if (a[0] == b[j])
      dp[0][indB] = 1;
    else
      dp[0][indB] = 0;
    for (int i = 1; i < A; ++i) {
      if (a[i] == b[j]) {
        dp[i][indB] = dp[i - 1][(indB + 1) % 2] + 1;
        ans = max(dp[i][indB], ans);
      } else {
        dp[i][indB] = dp[i - 1][indB];
      }
    }
  }

  cout << ans << endl;

  return 0;
}

Error: 0

Fixed submission:

#include <iostream>

using namespace std;

int main() {
  int A, B;

  cin >> A >> B;

  int a[A];
  int b[B];

  for (int i = 0; i < A; ++i) {
    cin >> a[i];
  }
  for (int i = 0; i < B; ++i) {
    cin >> b[i];
  }

  int dp[A][B];

  if (a[0] == b[0])
    dp[0][0] = 1;
  else
    dp[0][0] = 0;

  for (int i = 1; i < A; ++i) {
    if (a[i] == b[0])
      dp[i][0] = 1;
    else
      dp[i][0] = max(dp[i - 1][0], 0);
  }

  for (int j = 1; j < B; ++j) {
    if (a[0] == b[j])
      dp[0][j] = 1;
    else
      dp[0][j] = 0;
  }

  int ans = 0;

  for (int j = 1; j < B; ++j) {
    for (int i = 1; i < A; ++i) {
      if (a[i] == b[j]) {
        dp[i][j] = dp[i - 1][j - 1] + 1;
        ans = max(dp[i][j], ans);
      } else {
        dp[i][j] = dp[i - 1][j];
      }
    }
  }

  cout << ans << endl;

  return 0;
}
Problem p00495 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <algorithm>
#include <bits/stdc++.h>
#include <queue>
#include <vector>
using namespace std;
typedef long long ll;

int A, B, a[5000], b[5000];
int dp[5001];

int main() {
  scanf("%d%d", &A, &B);
  for (int i = 0; i < A; i++)
    scanf("%d", &a[i]);
  for (int i = 0; i < B; i++)
    scanf("%d", &b[i]);
  int res = 0;
  for (int i = 1; i <= A; i++)
    for (int j = B; j >= 1; j--) {
      if (a[i - 1] == b[j - 1])
        dp[j] = max(dp[j - 1] + 1, dp[j]);
      res = max(res, dp[j]);
    }
  printf("%d\\n", res);
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <bits/stdc++.h>
#include <queue>
#include <vector>
using namespace std;
typedef long long ll;

int A, B, a[5000], b[5000];
int dp[5001][5001]; // i??\\??????????????°??¨j????????£????????§??°

int main() {
  scanf("%d%d", &A, &B);
  for (int i = 0; i < A; i++)
    scanf("%d", &a[i]);
  for (int i = 0; i < B; i++)
    scanf("%d", &b[i]);
  int res = 0;
  for (int i = 1; i <= A; i++)
    for (int j = 1; j <= B; j++) {
      dp[i][j] = dp[i - 1][j];
      if (a[i - 1] == b[j - 1])
        dp[i][j] = max(dp[i - 1][j - 1] + 1, dp[i][j]);
      res = max(res, dp[i][j]);
    }
  printf("%d\\n", res);
}
Problem p00495 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <cstdio>
#include <cstring>
#define rep(i, n) for (int i = 1; i <= (n); i++)
#define max(a, b) (((a) > (b)) ? (a) : (b))

int dat[5005][2];
short int dp[5005][5005];
int A, B;
int main(void) {
  scanf("%d%d", &A, &B);
  rep(i, A) scanf("%d", &(dat[i][0]));
  rep(i, B) scanf("%d", &(dat[i][1]));
  rep(i, A) {
    rep(j, B) {
      dp[i][j] = max(dp[i - 1][j],
                     ((dat[i][0] == dat[j][1]) ? (dp[i - 1][j - 1] + 1) : 0));
      // printf("%d  %d  %d\\n",i,j,dp[i][j]);
    }
  }
  int ans = 0;
  rep(i, B) ans = max(ans, dp[A][i]);
  printf("%d\\n", ans);
  return 0;
}

Error: MLE

Fixed submission:

#include <cstdio>
#include <cstring>
#define rep(i, n) for (int i = 1; i <= (n); i++)
#define max(a, b) (((a) > (b)) ? (a) : (b))

int dat[5005][2];
int dp[5005][5005];
int A, B;
int main(void) {
  scanf("%d%d", &A, &B);
  rep(i, A) scanf("%d", &(dat[i][0]));
  rep(i, B) scanf("%d", &(dat[i][1]));
  rep(i, A) {
    rep(j, B) {
      dp[i][j] = max(dp[i - 1][j],
                     ((dat[i][0] == dat[j][1]) ? (dp[i - 1][j - 1] + 1) : 0));
      // printf("%d  %d  %d\\n",i,j,dp[i][j]);
    }
  }
  int ans = 0;
  rep(i, B) ans = max(ans, dp[A][i]);
  printf("%d\\n", ans);
  return 0;
}
Problem p00495 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
vector<int> A;
vector<int> B;

bool check(int len) {
  for (int begin = 0; begin < B.size() - len + 1; begin++) {
    int bidx = begin;
    for (int j = 0; j < A.size(); j++) {
      if (A[j] == B[bidx])
        bidx++;
      if (bidx - begin >= len)
        return true;
    }
  }
  return false;
}

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

  int a, b;
  cin >> a >> b;
  A = vector<int>(a);
  B = vector<int>(b);
  for (int i = 0; i < a; i++) {
    cin >> A[i];
  }
  for (int i = 0; i < b; i++) {
    cin >> B[i];
  }

  int l = 0, r = b;
  while (l + 1 < r) {
    int mid = (l + r) / 2;
    if (check(mid)) {
      l = mid;
    } else {
      r = mid;
    }
  }

  cout << l << endl;

  return 0;
}

Error: MLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
vector<int> A;
vector<int> B;

int memo[5000][5000];

int func(int a, int b) {
  if (memo[a][b] != -1)
    return memo[a][b];
  if (a == A.size() || b == B.size()) {
    return 0;
  }
  int res = 0;
  res = func(a + 1, b);
  if (A[a] == B[b]) {
    res = max(res, func(a + 1, b + 1) + 1);
  }

  return res;
}

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

  for (int i = 0; i < 5000; i++)
    for (int j = 0; j < 5000; j++)
      memo[i][j] = -1;

  int a, b;
  cin >> a >> b;
  A = vector<int>(a);
  B = vector<int>(b);
  for (int i = 0; i < a; i++) {
    cin >> A[i];
  }
  for (int i = 0; i < b; i++) {
    cin >> B[i];
  }

  int ans = 0;
  for (int i = 0; i < b; i++) {
    ans = max(ans, func(0, i));
  }

  cout << ans << endl;

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

#include <cstdio>
#include <cstdlib>
using namespace std;
int field[2][5001] = {0};
int main(void) {
  int current = 1;
  int n_a, n_b;
  scanf("%d%d", &n_a, &n_b);
  int a[5001];
  for (int i = 1; i <= n_a; i++)
    scanf("%d", &a[i]);
  int b[5001];
  for (int i = 1; i <= n_b; i++)
    scanf("%d", &b[i]);
  for (int i = 1; i <= n_a; i++) {
    for (int j = 1; j <= n_b; j++) {
      field[current][j] = field[abs(current - 1)][j];
      if (a[i] == b[j] &&
          field[current][j] <= field[abs(current - 1)][j - 1] + 1)
        field[current][j] = field[abs(current - 1)][j - 1] + 1;
    }
    current = 1 - current;
  }
  int max = 0;
  for (int i = 0; i <= 1; i++) {
    for (int j = 1; j <= n_b; j++) {
      if (max <= field[i][j])
        max = field[i][j];
    }
  }
  printf("%d\\n", max);
  return 0;
}

Error: 0

Fixed submission:

#include <cstdio>
#include <cstdlib>
using namespace std;
int field[2][5001] = {0};
int main(void) {
  int current = 1;
  int n_a, n_b;
  scanf("%d%d", &n_a, &n_b);
  int a[5001];
  for (int i = 1; i <= n_a; i++)
    scanf("%d", &a[i]);
  int b[5001];
  for (int i = 1; i <= n_b; i++)
    scanf("%d", &b[i]);
  for (int i = 1; i <= n_a; i++) {
    for (int j = 1; j <= n_b; j++) {
      field[current][j] = field[abs(current - 1)][j];
      if (a[i] == b[j] &&
          field[current][j] <= field[abs(current - 1)][j - 1] + 1)
        field[current][j] = field[abs(current - 1)][j - 1] + 1;
    }
    current = 1 - current;
  }
  int max = 0;
  for (int i = 1; i <= 2; i++) {
    for (int j = 1; j <= n_b; j++) {
      if (max <= field[i][j])
        max = field[i][j];
    }
  }
  printf("%d\\n", max);
  return 0;
}
Problem p00495 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <algorithm>
#include <iostream>
using namespace std;
int a, b, ans = 0;
int A[5001], B[5001];
int dp[2][5001];

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cin >> a >> b;
  for (int i = 1; i <= a; i++)
    cin >> A[i];
  for (int i = 1; i <= b; i++)
    cin >> B[i];
  for (int i = 1; i <= a; i++) {
    for (int j = 1; j <= b; j++) {
      if (A[i] == B[j]) {
        dp[i % 2][j] = dp[1 - i % 2][j - 1] + 1;
      } else {
        dp[i % 2][j] = dp[1 - i % 2][j];
      }
      ans = max(ans, dp[i % 2][j]);
    }
  }
  cout << ans << endl;
  return 0;
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <iostream>
using namespace std;
int a, b, ans = 0;
int A[5001], B[5001];
int dp[5001][5001];

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cin >> a >> b;
  for (int i = 1; i <= a; i++)
    cin >> A[i];
  for (int i = 1; i <= b; i++)
    cin >> B[i];
  for (int i = 1; i <= a; i++) {
    for (int j = 1; j <= b; j++) {
      if (A[i] == B[j]) {
        dp[i][j] = dp[i - 1][j - 1] + 1;
      } else {
        dp[i][j] = dp[i - 1][j];
      }
      ans = max(ans, dp[i][j]);
    }
  }
  cout << ans << endl;
  return 0;
}
Problem p00495 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <cstdio>
#include <cstring>
#include <iostream>

using namespace std;

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

int card[2][5000];
int a, b;

int main() {
  cin >> a >> b;

  REP(i, a) scanf("%d", &card[0][i]);
  REP(i, b) scanf("%d", &card[1][i]);

  int ans = 0;
  REP(i, b) {
    int pos = i, cnt = 0;
    REP(j, a) {
      if (pos >= b)
        break;
      if (card[0][j] != card[1][pos])
        continue;
      pos++;
    }
    ans = max(ans, pos - i);
  }

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

Error: MLE

Fixed submission:

#include <cstdio>
#include <cstring>
#include <iostream>

using namespace std;

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

int card[2][5000];
int dp[5001][5001];
int a, b;

int solve() {
  int res = 0;
  REP(i, a) REP(j, b) {
    if (card[0][i] == card[1][j]) {
      dp[i + 1][j + 1] = dp[i][j] + 1;
    } else {
      dp[i + 1][j + 1] = max(dp[i + 1][j], 0);
    }
    res = max(res, dp[i + 1][j + 1]);
  }
  return res;
}

int main() {
  cin >> a >> b;

  int in;
  REP(i, a) {
    scanf("%d", &in);
    card[0][i] = in;
  }
  REP(i, b) {
    scanf("%d", &in);
    card[1][i] = in;
  }

  int ans = solve();

  int tmp[5000];
  REP(i, b) tmp[i] = card[1][b - i - 1];
  REP(i, b) card[1][i] = tmp[i];

  memset(dp, 0, sizeof(dp));
  ans = max(ans, solve());

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

#include <cstdio>
#include <vector>
using namespace std;

int a, b;
vector<int> c, d;
int ans;

int f(int x) {
  int y = 0;
  for (int i = 0; i < a; i++) {
    if (d[x] == c[i]) {
      x++;
      y++;
    }
  }
  return y;
}

int main() {
  scanf("%d %d", &a, &b);
  for (int i = 0; i < a; i++) {
    int t;
    scanf("%d", &t);
    c.push_back(t);
  }
  for (int i = 0; i < b; i++) {
    int t;
    scanf("%d", &t);
    d.push_back(t);
  }

  for (int i = 0; i < b; i++) {
    ans = max(ans, f(i));
  }
  printf("%d\\n", ans);
  return 0;
}

Error: TLE

Fixed submission:

#include <cstdio>
#include <vector>
using namespace std;

int a, b;
vector<int> c, d;

bool match(int x, int y) {
  int p = 0;
  for (int i = 0; i < y; i++) {
    while (c[p] != d[x + i]) {
      p++;
      if (p > c.size() - 1)
        return false;
    }
    p++;
  }
  return true;
}

int main() {
  scanf("%d %d", &a, &b);
  for (int i = 0; i < a; i++) {
    int t;
    scanf("%d", &t);
    c.push_back(t);
  }
  for (int i = 0; i < b; i++) {
    int t;
    scanf("%d", &t);
    d.push_back(t);
  }

  for (int i = b; i > 0; i--) {
    for (int j = 0; j + i < b + 1; j++) {
      if (match(j, i)) {
        printf("%d\\n", i);
        return 0;
      }
    }
  }
  return 0;
}
Problem p00495 (C++) — buggy submission, verdict: Memory Limit Exceeded

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

int na, nb;
int a[5000], b[5000];
int dp[2][5001];

int main() {
  scanf("%d%d", &na, &nb);
  for (int i = 0; i < na; i++)
    scanf("%d", &a[i]);
  for (int i = 0; i < nb; i++)
    scanf("%d", &b[i]);

  for (int i = 0; i < 2; i++)
    for (int j = 0; j <= nb; j++)
      dp[i][j] = 0;

  int ans = 0;
  for (int i = 0; i < na; i++)
    for (int j = 0; j < nb; j++) {
      int t = (i & 1) ? 1 : 0;
      dp[1 - t][j + 1] = max(dp[1 - t][j + 1], dp[t][j + 1]);
      if (a[i] == b[j]) {
        dp[1 - t][j + 1] = max(dp[1 - t][j + 1], dp[t][j] + 1);
        ans = max(ans, dp[1 - t][j + 1]);
      }
    }
  printf("%d\\n", ans);
}


Error: MLE

Fixed submission:

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

int na, nb;
int a[5000], b[5000];
int dp[5001][5001];

int main() {
  scanf("%d%d", &na, &nb);
  for (int i = 0; i < na; i++)
    scanf("%d", &a[i]);
  for (int i = 0; i < nb; i++)
    scanf("%d", &b[i]);

  for (int i = 0; i <= na; i++)
    for (int j = 0; j <= nb; j++)
      dp[i][j] = 0;

  int ans = 0;
  for (int i = 0; i < na; i++)
    for (int j = 0; j < nb; j++) {
      dp[i + 1][j + 1] = max(dp[i + 1][j + 1], dp[i][j + 1]);
      if (a[i] == b[j]) {
        dp[i + 1][j + 1] = max(dp[i + 1][j + 1], dp[i][j] + 1);
        ans = max(ans, dp[i + 1][j + 1]);
      }
    }
  printf("%d\\n", ans);
}
Problem p00495 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <vector>

using namespace std;

int main() {
  int A, B;

  scanf("%d", &A);
  scanf("%d", &B);

  vector<int> P(A);
  vector<int> Q(B);

  for (int i = 0; i < A; i++) {
    scanf("%d", &P[i]);
  }
  for (int i = 0; i < B; i++) {
    scanf("%d", &Q[i]);
  }

  int l = 1;
  int r = B;
  int m;

  while (r - l > 1) {
    m = (l + r) / 2;

    bool ok = false;

    for (int j = 0; j < (B - m); j++) {
      int ptr = j;

      for (int k = 0; k < A; k++) {
        if (P[k] == Q[ptr]) {
          ptr++;
        }

        if (ptr == j + m) {
          break;
        }
      }

      if (ptr == j + m) {
        ok = true;
      }
    }

    if (ok) {
      l = m;
    } else {
      r = m;
    }
  }

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

  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <vector>

using namespace std;

int main() {
  int A, B;

  scanf("%d", &A);
  scanf("%d", &B);

  vector<int> P(A);
  vector<int> Q(B);

  for (int i = 0; i < A; i++) {
    scanf("%d", &P[i]);
  }
  for (int i = 0; i < B; i++) {
    scanf("%d", &Q[i]);
  }

  int ret = 0;

  for (int i = 1; i < B; i++) {
    for (int j = 0; j < (B - i); j++) {
      int ptr = j;

      for (int k = 0; k < A; k++) {
        if (P[k] == Q[ptr]) {
          ptr++;
        }

        if (ptr == j + i) {
          break;
        }
      }

      if (ptr == j + i) {
        ret = i;
      }
    }
  }

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

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

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

const int MAX_N = 5001;

int A, B;
int tA[MAX_N], tB[MAX_N];

int f(int *a, int na, int *b, int nb) {
  int j = 0;
  for (int i = 0; i < na && j < nb; ++i) {
    if (a[i] == b[j])
      ++j;
  }
  return j;
}

int main() {
  cin >> A >> B;
  for (int i = 0; i < A; ++i)
    cin >> tA[i];
  for (int i = 0; i < B; ++i)
    cin >> tB[i];
  int ans = 0;
  for (int i = 0; i < B; ++i)
    ans = max(ans, f(tA, A, tB + i, B - i));
  cout << ans << endl;
  return 0;
}

Error: TLE

Fixed submission:

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

const int MAX_N = 5001;

int A, B;
int tA[MAX_N], tB[MAX_N];

bool f(int *a, int na, int *b, int nb) {
  for (int i = 0, j = 0; i < na; ++i) {
    if (a[i] == b[j])
      ++j;
    if (j == nb)
      return true;
  }
  return false;
}

int main() {
  cin >> A >> B;
  for (int i = 0; i < A; ++i)
    cin >> tA[i];
  for (int i = 0; i < B; ++i)
    cin >> tB[i];
  for (int n = (int)min(A, B); n > 0; --n) {
    for (int i = 0; i + n <= B; ++i) {
      if (f(tA, A, tB + i, n)) {
        cout << n << endl;
        return 0;
      }
    }
  }
  cout << 0 << endl;
  return 0;
}
Problem p00495 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int n, m, a[10000], b[10000], cnt, sum;

int main() {
  cin >> n >> m;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  for (int i = 0; i < m; i++) {
    cin >> b[i];
  }
  for (int i = 0; i < m; i++) {
    cnt = i;
    for (int j = 0; j < n; j++) {
      if (a[j] == b[cnt]) {
        cnt++;
      }
    }
    sum = max(sum, cnt - i);
  }
  cout << sum << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <queue>
using namespace std;
int MAX, n, m, o, a[10000], b[10000], c[10000];
queue<int> Q;

int main() {
  cin >> n >> m;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
  }
  for (int i = 1; i <= m; i++) {
    cin >> c[i];
  }
  for (int g = 1; g <= m; g++) {
    for (int h = g; h <= m; h++) {
      while (!Q.empty()) {
        Q.pop();
      }
      for (int i = g; i <= h; i++) {
        Q.push(c[i]);
      }
      for (int i = 1; i <= n; i++) {
        if (a[i] == Q.front()) {
          Q.pop();
        }
        if (Q.empty()) {
          goto Exit;
        }
      }
      goto E;
    Exit:;
      MAX = max(MAX, h - g + 1);
    E:;
    }
  }
  cout << MAX << endl;
  return 0;
}