結果
問題 | No.2231 Surprising Flash! |
ユーザー | 👑 hos.lyric |
提出日時 | 2023-02-24 21:45:12 |
言語 | C++14 (gcc 12.3.0 + boost 1.83.0) |
結果 |
AC
|
実行時間 | 247 ms / 4,000 ms |
コード長 | 17,605 bytes |
コンパイル時間 | 2,201 ms |
コンパイル使用メモリ | 140,932 KB |
実行使用メモリ | 118,568 KB |
最終ジャッジ日時 | 2024-06-12 07:31:53 |
合計ジャッジ時間 | 10,515 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge5 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 2 ms
6,812 KB |
testcase_01 | AC | 3 ms
6,944 KB |
testcase_02 | AC | 23 ms
6,940 KB |
testcase_03 | AC | 174 ms
6,940 KB |
testcase_04 | AC | 13 ms
6,944 KB |
testcase_05 | AC | 14 ms
6,940 KB |
testcase_06 | AC | 19 ms
6,940 KB |
testcase_07 | AC | 3 ms
6,940 KB |
testcase_08 | AC | 6 ms
6,940 KB |
testcase_09 | AC | 1 ms
6,940 KB |
testcase_10 | AC | 5 ms
6,940 KB |
testcase_11 | AC | 202 ms
89,080 KB |
testcase_12 | AC | 217 ms
118,292 KB |
testcase_13 | AC | 213 ms
118,568 KB |
testcase_14 | AC | 181 ms
78,468 KB |
testcase_15 | AC | 201 ms
88,952 KB |
testcase_16 | AC | 167 ms
12,252 KB |
testcase_17 | AC | 183 ms
21,960 KB |
testcase_18 | AC | 216 ms
80,952 KB |
testcase_19 | AC | 217 ms
82,004 KB |
testcase_20 | AC | 226 ms
80,476 KB |
testcase_21 | AC | 231 ms
79,496 KB |
testcase_22 | AC | 223 ms
81,632 KB |
testcase_23 | AC | 220 ms
79,196 KB |
testcase_24 | AC | 222 ms
79,064 KB |
testcase_25 | AC | 228 ms
79,964 KB |
testcase_26 | AC | 225 ms
81,280 KB |
testcase_27 | AC | 224 ms
79,836 KB |
testcase_28 | AC | 220 ms
78,776 KB |
testcase_29 | AC | 247 ms
93,060 KB |
testcase_30 | AC | 241 ms
92,716 KB |
testcase_31 | AC | 238 ms
93,616 KB |
testcase_32 | AC | 241 ms
91,896 KB |
testcase_33 | AC | 247 ms
92,136 KB |
testcase_34 | AC | 203 ms
89,504 KB |
testcase_35 | AC | 206 ms
89,056 KB |
testcase_36 | AC | 204 ms
89,816 KB |
testcase_37 | AC | 198 ms
91,044 KB |
testcase_38 | AC | 198 ms
90,508 KB |
testcase_39 | AC | 107 ms
14,616 KB |
testcase_40 | AC | 83 ms
15,236 KB |
testcase_41 | AC | 2 ms
6,948 KB |
testcase_42 | AC | 2 ms
6,940 KB |
testcase_43 | AC | 2 ms
6,940 KB |
testcase_44 | AC | 48 ms
6,944 KB |
ソースコード
#include <cassert> #include <cmath> #include <cstdint> #include <cstdio> #include <cstdlib> #include <cstring> #include <algorithm> #include <bitset> #include <complex> #include <deque> #include <functional> #include <iostream> #include <map> #include <numeric> #include <queue> #include <set> #include <sstream> #include <string> #include <unordered_map> #include <unordered_set> #include <utility> #include <vector> using namespace std; using Int = long long; template <class T1, class T2> ostream &operator<<(ostream &os, const pair<T1, T2> &a) { return os << "(" << a.first << ", " << a.second << ")"; }; template <class T> ostream &operator<<(ostream &os, const vector<T> &as) { const int sz = as.size(); os << "["; for (int i = 0; i < sz; ++i) { if (i >= 256) { os << ", ..."; break; } if (i > 0) { os << ", "; } os << as[i]; } return os << "]"; } template <class T> void pv(T a, T b) { for (T i = a; i != b; ++i) cerr << *i << " "; cerr << endl; } template <class T> bool chmin(T &t, const T &f) { if (t > f) { t = f; return true; } return false; } template <class T> bool chmax(T &t, const T &f) { if (t < f) { t = f; return true; } return false; } //////////////////////////////////////////////////////////////////////////////// template <unsigned M_> struct ModInt { static constexpr unsigned M = M_; unsigned x; constexpr ModInt() : x(0U) {} constexpr ModInt(unsigned x_) : x(x_ % M) {} constexpr ModInt(unsigned long long x_) : x(x_ % M) {} constexpr ModInt(int x_) : x(((x_ %= static_cast<int>(M)) < 0) ? (x_ + static_cast<int>(M)) : x_) {} constexpr ModInt(long long x_) : x(((x_ %= static_cast<long long>(M)) < 0) ? (x_ + static_cast<long long>(M)) : x_) {} ModInt &operator+=(const ModInt &a) { x = ((x += a.x) >= M) ? (x - M) : x; return *this; } ModInt &operator-=(const ModInt &a) { x = ((x -= a.x) >= M) ? (x + M) : x; return *this; } ModInt &operator*=(const ModInt &a) { x = (static_cast<unsigned long long>(x) * a.x) % M; return *this; } ModInt &operator/=(const ModInt &a) { return (*this *= a.inv()); } ModInt pow(long long e) const { if (e < 0) return inv().pow(-e); ModInt a = *this, b = 1U; for (; e; e >>= 1) { if (e & 1) b *= a; a *= a; } return b; } ModInt inv() const { unsigned a = M, b = x; int y = 0, z = 1; for (; b; ) { const unsigned q = a / b; const unsigned c = a - q * b; a = b; b = c; const int w = y - static_cast<int>(q) * z; y = z; z = w; } assert(a == 1U); return ModInt(y); } ModInt operator+() const { return *this; } ModInt operator-() const { ModInt a; a.x = x ? (M - x) : 0U; return a; } ModInt operator+(const ModInt &a) const { return (ModInt(*this) += a); } ModInt operator-(const ModInt &a) const { return (ModInt(*this) -= a); } ModInt operator*(const ModInt &a) const { return (ModInt(*this) *= a); } ModInt operator/(const ModInt &a) const { return (ModInt(*this) /= a); } template <class T> friend ModInt operator+(T a, const ModInt &b) { return (ModInt(a) += b); } template <class T> friend ModInt operator-(T a, const ModInt &b) { return (ModInt(a) -= b); } template <class T> friend ModInt operator*(T a, const ModInt &b) { return (ModInt(a) *= b); } template <class T> friend ModInt operator/(T a, const ModInt &b) { return (ModInt(a) /= b); } explicit operator bool() const { return x; } bool operator==(const ModInt &a) const { return (x == a.x); } bool operator!=(const ModInt &a) const { return (x != a.x); } friend std::ostream &operator<<(std::ostream &os, const ModInt &a) { return os << a.x; } }; //////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////// constexpr unsigned MO = 998244353U; constexpr unsigned MO2 = 2U * MO; constexpr int FFT_MAX = 23; using Mint = ModInt<MO>; constexpr Mint FFT_ROOTS[FFT_MAX + 1] = {1U, 998244352U, 911660635U, 372528824U, 929031873U, 452798380U, 922799308U, 781712469U, 476477967U, 166035806U, 258648936U, 584193783U, 63912897U, 350007156U, 666702199U, 968855178U, 629671588U, 24514907U, 996173970U, 363395222U, 565042129U, 733596141U, 267099868U, 15311432U}; constexpr Mint INV_FFT_ROOTS[FFT_MAX + 1] = {1U, 998244352U, 86583718U, 509520358U, 337190230U, 87557064U, 609441965U, 135236158U, 304459705U, 685443576U, 381598368U, 335559352U, 129292727U, 358024708U, 814576206U, 708402881U, 283043518U, 3707709U, 121392023U, 704923114U, 950391366U, 428961804U, 382752275U, 469870224U}; constexpr Mint FFT_RATIOS[FFT_MAX] = {911660635U, 509520358U, 369330050U, 332049552U, 983190778U, 123842337U, 238493703U, 975955924U, 603855026U, 856644456U, 131300601U, 842657263U, 730768835U, 942482514U, 806263778U, 151565301U, 510815449U, 503497456U, 743006876U, 741047443U, 56250497U, 867605899U}; constexpr Mint INV_FFT_RATIOS[FFT_MAX] = {86583718U, 372528824U, 373294451U, 645684063U, 112220581U, 692852209U, 155456985U, 797128860U, 90816748U, 860285882U, 927414960U, 354738543U, 109331171U, 293255632U, 535113200U, 308540755U, 121186627U, 608385704U, 438932459U, 359477183U, 824071951U, 103369235U}; // as[rev(i)] <- \sum_j \zeta^(ij) as[j] void fft(Mint *as, int n) { assert(!(n & (n - 1))); assert(1 <= n); assert(n <= 1 << FFT_MAX); int m = n; if (m >>= 1) { for (int i = 0; i < m; ++i) { const unsigned x = as[i + m].x; // < MO as[i + m].x = as[i].x + MO - x; // < 2 MO as[i].x += x; // < 2 MO } } if (m >>= 1) { Mint prod = 1U; for (int h = 0, i0 = 0; i0 < n; i0 += (m << 1)) { for (int i = i0; i < i0 + m; ++i) { const unsigned x = (prod * as[i + m]).x; // < MO as[i + m].x = as[i].x + MO - x; // < 3 MO as[i].x += x; // < 3 MO } prod *= FFT_RATIOS[__builtin_ctz(++h)]; } } for (; m; ) { if (m >>= 1) { Mint prod = 1U; for (int h = 0, i0 = 0; i0 < n; i0 += (m << 1)) { for (int i = i0; i < i0 + m; ++i) { const unsigned x = (prod * as[i + m]).x; // < MO as[i + m].x = as[i].x + MO - x; // < 4 MO as[i].x += x; // < 4 MO } prod *= FFT_RATIOS[__builtin_ctz(++h)]; } } if (m >>= 1) { Mint prod = 1U; for (int h = 0, i0 = 0; i0 < n; i0 += (m << 1)) { for (int i = i0; i < i0 + m; ++i) { const unsigned x = (prod * as[i + m]).x; // < MO as[i].x = (as[i].x >= MO2) ? (as[i].x - MO2) : as[i].x; // < 2 MO as[i + m].x = as[i].x + MO - x; // < 3 MO as[i].x += x; // < 3 MO } prod *= FFT_RATIOS[__builtin_ctz(++h)]; } } } for (int i = 0; i < n; ++i) { as[i].x = (as[i].x >= MO2) ? (as[i].x - MO2) : as[i].x; // < 2 MO as[i].x = (as[i].x >= MO) ? (as[i].x - MO) : as[i].x; // < MO } } // as[i] <- (1/n) \sum_j \zeta^(-ij) as[rev(j)] void invFft(Mint *as, int n) { assert(!(n & (n - 1))); assert(1 <= n); assert(n <= 1 << FFT_MAX); int m = 1; if (m < n >> 1) { Mint prod = 1U; for (int h = 0, i0 = 0; i0 < n; i0 += (m << 1)) { for (int i = i0; i < i0 + m; ++i) { const unsigned long long y = as[i].x + MO - as[i + m].x; // < 2 MO as[i].x += as[i + m].x; // < 2 MO as[i + m].x = (prod.x * y) % MO; // < MO } prod *= INV_FFT_RATIOS[__builtin_ctz(++h)]; } m <<= 1; } for (; m < n >> 1; m <<= 1) { Mint prod = 1U; for (int h = 0, i0 = 0; i0 < n; i0 += (m << 1)) { for (int i = i0; i < i0 + (m >> 1); ++i) { const unsigned long long y = as[i].x + MO2 - as[i + m].x; // < 4 MO as[i].x += as[i + m].x; // < 4 MO as[i].x = (as[i].x >= MO2) ? (as[i].x - MO2) : as[i].x; // < 2 MO as[i + m].x = (prod.x * y) % MO; // < MO } for (int i = i0 + (m >> 1); i < i0 + m; ++i) { const unsigned long long y = as[i].x + MO - as[i + m].x; // < 2 MO as[i].x += as[i + m].x; // < 2 MO as[i + m].x = (prod.x * y) % MO; // < MO } prod *= INV_FFT_RATIOS[__builtin_ctz(++h)]; } } if (m < n) { for (int i = 0; i < m; ++i) { const unsigned y = as[i].x + MO2 - as[i + m].x; // < 4 MO as[i].x += as[i + m].x; // < 4 MO as[i + m].x = y; // < 4 MO } } const Mint invN = Mint(n).inv(); for (int i = 0; i < n; ++i) { as[i] *= invN; } } void fft(vector<Mint> &as) { fft(as.data(), as.size()); } void invFft(vector<Mint> &as) { invFft(as.data(), as.size()); } vector<Mint> convolve(vector<Mint> as, vector<Mint> bs) { if (as.empty() || bs.empty()) return {}; const int len = as.size() + bs.size() - 1; int n = 1; for (; n < len; n <<= 1) {} as.resize(n); fft(as); bs.resize(n); fft(bs); for (int i = 0; i < n; ++i) as[i] *= bs[i]; invFft(as); as.resize(len); return as; } vector<Mint> square(vector<Mint> as) { if (as.empty()) return {}; const int len = as.size() + as.size() - 1; int n = 1; for (; n < len; n <<= 1) {} as.resize(n); fft(as); for (int i = 0; i < n; ++i) as[i] *= as[i]; invFft(as); as.resize(len); return as; } //////////////////////////////////////////////////////////////////////////////// /* [0, n] * [0, n - m + 1] [ a[0] ] [ ... a[0] ] [ a[m-1] ... ] [ a[m-1] ] [ ... ] [ a[0] ] [ ... ] [ a[m-1] ] [x^j] (rev(a) b) m - 1 <= j <= n - 1 */ vector<Mint> middle(vector<Mint> as, vector<Mint> bs) { const int m = as.size(); const int n = bs.size(); assert(m <= n); int nn = 1; for (; nn < n; nn <<= 1) {} reverse(as.begin(), as.end()); as.resize(nn, 0); fft(as); bs.resize(nn, 0); fft(bs); for (int i = 0; i < nn; ++i) { bs[i] *= as[i]; } invFft(bs); bs.resize(n); bs.erase(bs.begin(), bs.begin() + (m - 1)); return bs; } //////////////////////////////////////////////////////////////////////////////// // SA-IS // String: string, vector<int>, vector<long long> // if sigma <= n, O(n) time, O(n) space // if sigma > n, O(n log n) time, O(n) space template <class String> vector<int> suffixArrayRec(const String &as) { const int n = as.size(); if (n == 0) return {}; const auto minmaxA = minmax_element(as.begin(), as.end()); const auto minA = *minmaxA.first, maxA = *minmaxA.second; if (static_cast<unsigned long long>(maxA) - minA >= static_cast<unsigned long long>(n)) { vector<int> us(n); for (int u = 0; u < n; ++u) us[u] = u; std::sort(us.begin(), us.end(), [&](int u, int v) -> bool { return (as[u] < as[v]); }); int b = 0; vector<int> bs(n, 0); for (int i = 1; i < n; ++i) { if (as[us[i - 1]] != as[us[i]]) ++b; bs[us[i]] = b; } return suffixArrayRec(bs); } const int sigma = maxA - minA + 1; vector<int> pt(sigma + 1, 0), poss(sigma); for (int u = 0; u < n; ++u) ++pt[as[u] - minA + 1]; for (int a = 0; a < sigma; ++a) pt[a + 1] += pt[a]; // cmp[u] := (as[u, n) < as[u + 1, n)) vector<bool> cmp(n); cmp[n - 1] = false; for (int u = n - 1; --u >= 0; ) { cmp[u] = (as[u] != as[u + 1]) ? (as[u] < as[u + 1]) : cmp[u + 1]; } // ><, nn - id (0 <= id < n) int nn = 0; vector<int> ids(n, 0); int last = n; vector<int> nxt(n, 0); // put ><, from the tail of each bucket vector<int> us(n, 0); memcpy(poss.data(), pt.data() + 1, sigma * sizeof(int)); for (int u = n - 1; --u >= 1; ) if (!cmp[u - 1] && cmp[u]) { ids[u] = ++nn; nxt[u] = last; last = u; us[--poss[as[u] - minA]] = u; } // follow > backwards, from the head of each bucket memcpy(poss.data(), pt.data(), sigma * sizeof(int)); us[poss[as[n - 1] - minA]++] = n - 1; for (int i = 0; i < n; ++i) { const int u = us[i]; if (u && !cmp[u - 1]) us[poss[as[u - 1] - minA]++] = u - 1; } // follow < backwards, from the tail of each bucket memcpy(poss.data(), pt.data() + 1, sigma * sizeof(int)); for (int i = n; --i >= 0; ) { const int u = us[i]; if (u && cmp[u - 1]) us[--poss[as[u - 1] - minA]] = u - 1; } if (nn) { int vsLen = 0; vector<int> vs(nn); for (const int u : us) if (ids[u]) vs[vsLen++] = u; int b = 0; vector<int> bs(nn, 0); for (int j = 1; j < nn; ++j) { // as[v1, w1] (< or =) as[v0, w0] int v1 = vs[j - 1], v0 = vs[j]; const int w1 = nxt[v1], w0 = nxt[v0]; if (w1 - v1 != w0 - v0) { ++b; } else { for (; ; ++v1, ++v0) { if (v1 == n) { ++b; break; } if (as[v1] != as[v0]) { ++b; break; } if (v1 == w1) break; } } bs[nn - ids[vs[j]]] = b; } for (int u = 0; u < n; ++u) if (ids[u]) vs[nn - ids[u]] = u; const auto sub = suffixArrayRec(bs); // put ><, from the tail of each bucket memset(us.data(), 0, n * sizeof(int)); memcpy(poss.data(), pt.data() + 1, sigma * sizeof(int)); for (int j = nn; --j >= 0; ) { const int u = vs[sub[j]]; us[--poss[as[u] - minA]] = u; } // follow > backwards, from the head of each bucket memcpy(poss.data(), pt.data(), sigma * sizeof(int)); us[poss[as[n - 1] - minA]++] = n - 1; for (int i = 0; i < n; ++i) { const int u = us[i]; if (u && !cmp[u - 1]) us[poss[as[u - 1] - minA]++] = u - 1; } // follow < backwards, from the tail of each bucket memcpy(poss.data(), pt.data() + 1, sigma * sizeof(int)); for (int i = n; --i >= 0; ) { const int u = us[i]; if (u && cmp[u - 1]) us[--poss[as[u - 1] - minA]] = u - 1; } } return us; } // us[i]: i-th suffix // su[u]: index of as[u, n) // hs[i]: longest common prefix of as[us[i - 1], n) and as[us[i], n) struct SuffixArray { int n; bool rmq; vector<int> us, su, hs, bsr; SuffixArray() : n(0), rmq(false) {} SuffixArray(const string &as, bool rmq_) : rmq(rmq_) { build(as); } SuffixArray(const vector<int> &as, bool rmq_) : rmq(rmq_) { build(as); } SuffixArray(const vector<long long> &as, bool rmq_) : rmq(rmq_) { build(as); } template <class String> void build(const String &as) { n = as.size(); us = suffixArrayRec(as); su.resize(n + 1); for (int i = 0; i < n; ++i) su[us[i]] = i; su[n] = -1; hs.assign(n, 0); for (int h = 0, u = 0; u < n; ++u) if (su[u]) { for (int v = us[su[u] - 1]; v + h < n && as[v + h] == as[u + h]; ++h) {} hs[su[u]] = h; if (h) --h; } if (rmq) { const int logN = n ? (31 - __builtin_clz(n)) : 0; hs.resize((logN + 1) * n - (1 << logN) + 1); for (int e = 0; e < logN; ++e) { int *hes = hs.data() + e * n; for (int i = 0; i <= n - (1 << (e + 1)); ++i) { hes[n + i] = min(hes[i], hes[i + (1 << e)]); } } bsr.resize(n + 1); bsr[0] = -1; for (int i = 1; i <= n; ++i) bsr[i] = bsr[i >> 1] + 1; } } // Returns longest common prefix of as[u, n) and as[v, n). // 0 <= u, v <= n // Assumes rmq. inline int lcp(int u, int v) const { if (u == v) return n - u; int i = su[u], j = su[v]; if (i > j) swap(i, j); const int e = bsr[j - i]; return min(hs[e * n + i + 1], hs[e * n + j + 1 - (1 << e)]); } }; //////////////////////////////////////////////////////////////////////////////// int N, M; char T[500'010], S[500'010]; string T0, U; SuffixArray sa; int cmp(int k0, int k1) { if (k0 == k1) return 0; if (k0 > k1) return -cmp(k1, k0); if (k0 + M <= k1) { { const int res = sa.lcp(N + 1 + 0, k0); if (res < M) return (S[res] < T0[k0 + res]) ? -1 : +1; } { const int res = sa.lcp(k1, N + 1 + 0); if (res < M) return (T0[k1 + res] < S[res]) ? -1 : +1; } } else { { const int res = sa.lcp(N + 1 + 0, k0); if (res < k1 - k0) return (S[res] < T0[k0 + res]) ? -1 : +1; } { const int res = sa.lcp(N + 1 + (k1 - k0), N + 1 + 0); if (res < k0 + M - k1) return (S[(k1 - k0) + res] < S[res]) ? -1 : +1; } { const int res = sa.lcp(k0 + M, N + 1 + (k0 + M - k1)); if (res < k1 - k0) return (T0[(k0 + M) + res] < S[(k0 + M - k1) + res]) ? -1 : +1; } } return 0; } int main() { for (int numCases; ~scanf("%d", &numCases); ) { for (int caseId = 1; caseId <= numCases; ++caseId) { scanf("%d%d", &N, &M); scanf("%s", T); scanf("%s", S); vector<Mint> fss[3], gss[3]; for (int h = 0; h < 3; ++h) { fss[h].assign(M, 0); gss[h].assign(N, 0); } for (int i = 0; i < M; ++i) if (S[i] != '?') { const int a = S[i] - 'a'; fss[0][i] = 1; fss[1][i] = a; fss[2][i] = a*a; } for (int i = 0; i < N; ++i) if (T[i] != '?') { const int a = T[i] - 'a'; gss[0][i] = 1; gss[1][i] = a; gss[2][i] = a*a; } const auto prod02 = middle(fss[0], gss[2]); const auto prod11 = middle(fss[1], gss[1]); const auto prod20 = middle(fss[2], gss[0]); vector<Mint> hs(N - M + 1, 0); for (int i = 0; i <= N - M; ++i) { hs[i] += prod02[i]; hs[i] -= 2 * prod11[i]; hs[i] += prod20[i]; } // cerr<<"hs = "<<hs<<endl; T0 = T; for (int i = 0; i < N; ++i) if (T[i] == '?') { T0[i] = 'a'; } U = T0 + '`' + S; sa = SuffixArray(U, true); int km = -1; for (int k = 0; k <= N - M; ++k) if (!hs[k]) { if (!~km || cmp(km, k) > 0) { km = k; } } if (~km) { string ans = T0; for (int i = 0; i < M; ++i) { ans[km + i] = S[i]; } puts(ans.c_str()); } else { puts("-1"); } } #ifndef LOCAL break; #endif } return 0; }