QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#443694 | #8521. Pattern Search II | ucup-team896# | RE | 0ms | 0kb | C++14 | 2.1kb | 2024-06-15 16:18:51 | 2024-06-15 16:18:51 |
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 = 450010;
const int B = 8;
mt19937 rnd(random_device{}());
string s, t;
int n, m, a[maxn], b[maxn], msk[maxn];
char to[maxn][2], nxt[maxn][1 << B];
vector<int> p;
int main() {
cin.tie(nullptr) -> ios::sync_with_stdio(false);
s = "a"s, t = "b"s;
while (SZ(t) < 600000) {
string p = s + t;
t = s, s = p;
}
t.resize(600000);
cin >> s;
if (t.find(s) != t.npos) return cout << SZ(s) << '\n', 0;
// 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;
rep (i, 1, m - n + 1) if (a[1] == b[i]) p.emplace_back(i);
for (int i : p) {
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
Runtime Error
input:
aabbaab