Problem p00495 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

#define fst first
#define scd second
#define PB push_back
#define MP make_pair
#define chmin(a, b) a = min(a, b)
#define chmax(a, b) a = max(a, b)
#define rep(i, x) for (int i = 0; i < (x); ++i)
#define rep1(i, x) for (int i = 1; i <= (x); ++i)
#define rrep(i, x) for (int i = (x)-1; i >= 0; --i)
#define rrep1(i, x) for (int i = (x); i >= 1; --i)
#define FOR(i, a, x) for (int i = (a); i < (x); ++i)
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()

template <typename T> T get() {
  T a;
  cin >> a;
  return a;
}
template <typename T> T rev(T a) {
  reverse(all(a));
  return a;
}
template <typename T> istream &operator>>(istream &is, vector<T> &vec) {
  rep(i, vec.size()) is >> vec[i];
  return is;
}
template <typename T> vector<T> &sort(vector<T> a) {
  sort(all(a));
  return a;
}

typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> P;

const int inf = 1e9;
const ll linf = 3e18;

int a[5000], b[5000];

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

  int A, B;
  cin >> A >> B;

  rep(i, A) cin >> a[i];
  rep(i, B) cin >> b[i];

  int ma = 0;

  rep(i, B) {
    int idx = i;

    int cnt = 0;

    rep(j, A) {
      if (a[j] == b[idx]) {
        idx++;
        cnt++;
      }

      if (idx == B)
        break;
    }

    chmax(ma, cnt);
  }

  cout << ma << endl;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

#define fst first
#define scd second
#define PB push_back
#define MP make_pair
#define chmin(a, b) a = min(a, b)
#define chmax(a, b) a = max(a, b)
#define rep(i, x) for (int i = 0; i < (x); ++i)
#define rep1(i, x) for (int i = 1; i <= (x); ++i)
#define rrep(i, x) for (int i = (x)-1; i >= 0; --i)
#define rrep1(i, x) for (int i = (x); i >= 1; --i)
#define FOR(i, a, x) for (int i = (a); i < (x); ++i)
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()

template <typename T> T get() {
  T a;
  cin >> a;
  return a;
}
template <typename T> T rev(T a) {
  reverse(all(a));
  return a;
}
template <typename T> istream &operator>>(istream &is, vector<T> &vec) {
  rep(i, vec.size()) is >> vec[i];
  return is;
}
template <typename T> vector<T> &sort(vector<T> a) {
  sort(all(a));
  return a;
}

typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> P;

const int inf = 1e9;
const ll linf = 3e18;

int a[5000], b[5000];

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

  int A, B;
  cin >> A >> B;

  rep(i, A) cin >> a[i];
  rep(i, B) cin >> b[i];

  int len = B;

  while (len) {
    int l = 0, r = len;

    while (r <= B) {
      int idx = l;

      for (int i = 0; i < A; ++i) {
        if (a[i] == b[idx])
          idx++;
        if (idx == r) {
          cout << r - l << endl;
          return 0;
        }
      }

      l++;
      r++;
    }

    len--;
  }
}