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


#include <bits/stdc++.h>

#define int long long int
#define rep(a, b, c) for (int a = b; a < c; a++)
#define repm(a, b, c) for (int a = (b - 1); a >= c; a--)
#define pb push_back
#define str string
#define sf(a) scanfs("%d", &a)
#define pb push_back
#define mp make_pair
#define Fi first
#define Se second
#define ALL(v) (v).begin(), (v).end()

using namespace std;

const int INF = 1e18 + 9;
const int Mod = 1e9 + 7;
inline int replac(str s) {
  double ans = 0;
  rep(i, 0, s.length()) { ans += (s[i] - '0') * pow(10, s.length() - i - 1); }
  return (int)ans;
}
inline string numstr(int m) {
  str s = "";
  while (m > 0) {
    char c;
    int a = m / 10;
    if (a > 0)
      a = m % (a * 10);
    else
      a = m;
    c = (char)('0' + a);
    s += c;
    m /= 10;
  }
  str st = "";
  for (int i = s.size() - 1; i >= 0; i--) {
    st += s[i];
  }
  return st;
}
typedef vector<int> vi;
typedef pair<int, int> pii;
typedef vector<pii> vii;
int a[100005], b[100005];
signed main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int dp[5005];
  rep(i, 0, 5005) dp[i] = 0;
  int n, m;
  cin >> n >> m;
  rep(i, 0, n) { cin >> a[i]; }
  rep(i, 0, m) { cin >> b[i]; }
  int ans = 0;
  // rep(i,0,n){
  rep(j, 0, m) {
    // if(a[i]==b[j]){
    int len = 0;
    // if(j>0)len=dp[j-1];
    int h = 0;
    int k = j;
    while (1) {
      for (; h < n; h++) {
        if (a[h] == b[k]) {
          len++;
          break;
        }
      }
      if (h == n)
        break;
      else {
        h++;
        k++;
      }
    }
    ans = max(ans, len);
    // if(h==n)len=0;
  }
  //}
  cout << ans << endl;
  return 0;
}

Error: TLE

Fixed submission:


#include <bits/stdc++.h>

#define int long long
#define rep(a, b, c) for (int a = b; a < c; a++)
#define repm(a, b, c) for (int a = (b - 1); a >= c; a--)
#define pb push_back
#define str string
#define sf(a) scanfs("%d", &a)
#define pb push_back
#define mp make_pair
#define Fi first
#define Se second
#define ALL(v) (v).begin(), (v).end()

using namespace std;

const int INF = 1e18 + 9;
const int Mod = 1e9 + 7;
inline int replac(str s) {
  double ans = 0;
  rep(i, 0, s.length()) { ans += (s[i] - '0') * pow(10, s.length() - i - 1); }
  return (int)ans;
}
inline string numstr(int m) {
  str s = "";
  while (m > 0) {
    char c;
    int a = m / 10;
    if (a > 0)
      a = m % (a * 10);
    else
      a = m;
    c = (char)('0' + a);
    s += c;
    m /= 10;
  }
  str st = "";
  for (int i = s.size() - 1; i >= 0; i--) {
    st += s[i];
  }
  return st;
}
typedef vector<int> vi;
typedef pair<int, int> pii;
typedef vector<pii> vii;
int a[5000], b[5000], n, m, dp[5002];
int f(int aa, int bb) {
  if ((n - aa) <= dp[bb]) {
    return dp[bb];
  } else if (bb == 0 || aa == 0) {
    if (a[aa] == b[bb])
      return dp[bb] = 1;
    else
      return 0;
  } else if (a[aa] == b[bb]) {
    return dp[bb] = f(aa - 1, bb - 1) + 1;
  } else {
    return dp[bb] = max(f(aa - 1, bb), f(aa, bb - 1));
  }
}
signed main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cin >> n >> m;
  rep(i, 0, 5001) { dp[i] = 0; }
  rep(i, 0, n) { cin >> a[i]; }
  rep(j, 0, m) { cin >> b[j]; }
  cout << f(n - 1, m - 1) << endl;
  return 0;
}