QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#443700#8521. Pattern Search IIucup-team896#TL 0ms0kbC++142.0kb2024-06-15 16:19:142024-06-15 16:19:14

Judging History

你现在查看的是最新测评结果

  • [2024-06-15 16:19:14]
  • 评测
  • 测评结果:TL
  • 用时:0ms
  • 内存:0kb
  • [2024-06-15 16:19:14]
  • 提交

answer

#pragma GCC optimize("Ofast","unroll-loops")

#include <bits/stdc++.h>

#define rep(i, j, k) for (int i = (j); i <= (k); ++i)
#define per(i, j, k) for (int i = (j); i >= (k); --i)
#define SZ(v) int((v).size())
#define ALL(v) (v).begin(),(v).end()
#define fi first
#define se second
using ll = long long;
using pii = std::pair<int, int>;
using pll = std::pair<ll, ll>;

template<class T>inline void chkmn(T &x, T y) { if (y < x) x = y; }
template<class T>inline void chkmx(T &x, T y) { if (y > x) x = y; }

using namespace std;

const int maxn = 600010;
const int B = 8;

string s, t;
int n, m, a[maxn], b[maxn], msk[maxn];
char to[maxn][2], nxt[maxn][1 << B];

int main() {
  cin.tie(nullptr) -> ios::sync_with_stdio(false);
  s = "a"s, t = "b"s;
  while (SZ(t) <= SZ(s)) {
    string p = s + t;
    t = s, s = p;
  }
  cin >> s;
  // s = string(100000, 'a');
  n = SZ(s), m = SZ(t);
  rep (i, 1, n) a[i] = s[i - 1] - 'a';
  rep (i, 1, m) b[i] = t[i - 1] - 'a';
  to[m + 1][0] = to[m + 1][1] = 100;
  per (i, m, 1) {
    to[i][0] = to[i + 1][0] + 1, to[i][1] = to[i + 1][1] + 1;
    to[i][b[i]] = 0;
  }
  rep (i, 0, m) {
    rep (S, 0, (1 << B) - 1) {
      int pos = i;
      rep (j, 0, B - 1) {
        pos += to[pos + 1][S >> j & 1] + 1;
      }
      chkmn(pos, i + 127);
      nxt[i][S] = pos - i;
    }
  }cerr<<clock()<<endl;
  for (int i = 1; i <= n; i++) {
    msk[(i - 1) / B] |= (a[i] << ((i - 1) % B));
  }
  int ans = 1e9;
  per (i, m - n + 1, 1) if (a[1] == b[i]) {
    if (1. * clock() / CLOCKS_PER_SEC >= 3.9) break;
    int pos = i, cur = 1, tmp = 0, ok = 1;
    while (cur + B - 1 <= n) {
      pos += nxt[pos - 1][msk[tmp]], tmp++, cur += B;
      if (pos - i + n - cur + 1 >= ans) { ok = 0; break; }
      if (pos > m) break;
    }
    if (!ok) continue;
    if (pos > m) continue;
    while (cur <= n) pos += to[pos][a[cur]] + 1, cur++;
    if (pos <= m) chkmn(ans, pos - i);
  }
  cout << ans << '\n';
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 0
Time Limit Exceeded

input:

aabbaab

output:


result: