結果

問題 No.2231 Surprising Flash!
ユーザー tokusakuraitokusakurai
提出日時 2023-02-25 10:16:25
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 17,040 bytes
コンパイル時間 3,145 ms
コンパイル使用メモリ 228,656 KB
実行使用メモリ 87,356 KB
最終ジャッジ日時 2024-09-13 10:00:58
合計ジャッジ時間 26,951 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 AC 3 ms
5,376 KB
testcase_02 AC 36 ms
5,376 KB
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 AC 9 ms
5,376 KB
testcase_09 WA -
testcase_10 AC 9 ms
5,376 KB
testcase_11 AC 650 ms
87,156 KB
testcase_12 AC 674 ms
82,704 KB
testcase_13 AC 695 ms
82,708 KB
testcase_14 WA -
testcase_15 AC 594 ms
87,356 KB
testcase_16 WA -
testcase_17 WA -
testcase_18 AC 722 ms
83,068 KB
testcase_19 AC 717 ms
83,092 KB
testcase_20 AC 707 ms
83,168 KB
testcase_21 AC 699 ms
83,092 KB
testcase_22 AC 695 ms
83,000 KB
testcase_23 AC 705 ms
83,124 KB
testcase_24 AC 734 ms
82,896 KB
testcase_25 AC 729 ms
83,076 KB
testcase_26 AC 730 ms
82,996 KB
testcase_27 AC 724 ms
82,884 KB
testcase_28 AC 743 ms
83,128 KB
testcase_29 AC 902 ms
87,280 KB
testcase_30 AC 918 ms
87,284 KB
testcase_31 AC 916 ms
87,136 KB
testcase_32 AC 883 ms
87,264 KB
testcase_33 AC 896 ms
87,276 KB
testcase_34 AC 583 ms
87,192 KB
testcase_35 AC 589 ms
87,268 KB
testcase_36 AC 588 ms
87,280 KB
testcase_37 AC 573 ms
87,052 KB
testcase_38 AC 581 ms
87,200 KB
testcase_39 AC 272 ms
13,216 KB
testcase_40 AC 190 ms
13,284 KB
testcase_41 AC 2 ms
5,376 KB
testcase_42 WA -
testcase_43 WA -
testcase_44 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define per(i, n) for (int i = (n)-1; i >= 0; i--)
#define rep2(i, l, r) for (int i = (l); i < (r); i++)
#define per2(i, l, r) for (int i = (r)-1; i >= (l); i--)
#define each(e, v) for (auto &e : v)
#define MM << " " <<
#define pb push_back
#define eb emplace_back
#define all(x) begin(x), end(x)
#define rall(x) rbegin(x), rend(x)
#define sz(x) (int)x.size()
using ll = long long;
using pii = pair<int, int>;
using pil = pair<int, ll>;
using pli = pair<ll, int>;
using pll = pair<ll, ll>;

template <typename T>
using minheap = priority_queue<T, vector<T>, greater<T>>;

template <typename T>
using maxheap = priority_queue<T>;

template <typename T>
bool chmax(T &x, const T &y) {
    return (x < y) ? (x = y, true) : false;
}

template <typename T>
bool chmin(T &x, const T &y) {
    return (x > y) ? (x = y, true) : false;
}

template <typename T>
int flg(T x, int i) {
    return (x >> i) & 1;
}

template <typename T>
void print(const vector<T> &v, T x = 0) {
    int n = v.size();
    for (int i = 0; i < n; i++) cout << v[i] + x << (i == n - 1 ? '\n' : ' ');
    if (v.empty()) cout << '\n';
}

template <typename T>
void printn(const vector<T> &v, T x = 0) {
    int n = v.size();
    for (int i = 0; i < n; i++) cout << v[i] + x << '\n';
}

template <typename T>
int lb(const vector<T> &v, T x) {
    return lower_bound(begin(v), end(v), x) - begin(v);
}

template <typename T>
int ub(const vector<T> &v, T x) {
    return upper_bound(begin(v), end(v), x) - begin(v);
}

template <typename T>
void rearrange(vector<T> &v) {
    sort(begin(v), end(v));
    v.erase(unique(begin(v), end(v)), end(v));
}

template <typename T>
vector<int> id_sort(const vector<T> &v, bool greater = false) {
    int n = v.size();
    vector<int> ret(n);
    iota(begin(ret), end(ret), 0);
    sort(begin(ret), end(ret), [&](int i, int j) { return greater ? v[i] > v[j] : v[i] < v[j]; });
    return ret;
}

template <typename S, typename T>
pair<S, T> operator+(const pair<S, T> &p, const pair<S, T> &q) {
    return make_pair(p.first + q.first, p.second + q.second);
}

template <typename S, typename T>
pair<S, T> operator-(const pair<S, T> &p, const pair<S, T> &q) {
    return make_pair(p.first - q.first, p.second - q.second);
}

template <typename S, typename T>
istream &operator>>(istream &is, pair<S, T> &p) {
    S a;
    T b;
    is >> a >> b;
    p = make_pair(a, b);
    return is;
}

template <typename S, typename T>
ostream &operator<<(ostream &os, const pair<S, T> &p) {
    return os << p.first << ' ' << p.second;
}

struct io_setup {
    io_setup() {
        ios_base::sync_with_stdio(false);
        cin.tie(NULL);
        cout << fixed << setprecision(15);
    }
} io_setup;

const int inf = (1 << 30) - 1;
const ll INF = (1LL << 60) - 1;
// const int MOD = 1000000007;
const int MOD = 998244353;

template <typename T>
struct Fast_Fourier_Transform {
    using comp = complex<double>;
    static double pi;
    static vector<comp> r, ir;

    Fast_Fourier_Transform() {}

    static void init() {
        if (!r.empty()) return;
        r.resize(30), ir.resize(30);
        for (int i = 0; i < 30; i++) {
            r[i] = -polar(1.0, pi / (1 << (i + 1)));   // r[i] := 1 の 2^(i+2) 乗根
            ir[i] = -polar(1.0, -pi / (1 << (i + 1))); // ir[i] := 1/r[i]
        }
    }

    static vector<comp> to_comp(vector<T> a) {
        vector<comp> ret(a.size());
        for (int i = 0; i < (int)a.size(); i++) ret[i] = comp(a[i], 0.0);
        return ret;
    }

    static vector<T> to_T(vector<comp> a) {
        vector<T> ret(a.size(), 0);
        for (int i = 0; i < (int)a.size(); i++) ret[i] = a[i].real() + 0.1; // 整数の場合、誤差をケア
        // for(int i = 0; i < (int)a.size(); i++) ret[i] = a[i].real(); // 小数の場合
        return ret;
    }

    static void fft(vector<comp> &a) {
        init();
        int n = a.size();
        assert((n & (n - 1)) == 0);
        for (int k = n; k >>= 1;) {
            comp w = 1;
            for (int s = 0, t = 0; s < n; s += 2 * k) {
                for (int i = s, j = s + k; i < s + k; i++, j++) {
                    comp x = a[i], y = w * a[j];
                    a[i] = x + y, a[j] = x - y;
                }
                w *= r[__builtin_ctz(++t)];
            }
        }
    }

    static void ifft(vector<comp> &a) {
        init();
        int n = a.size();
        assert((n & (n - 1)) == 0);
        a.resize(n);
        for (int k = 1; k < n; k <<= 1) {
            comp w = 1;
            for (int s = 0, t = 0; s < n; s += 2 * k) {
                for (int i = s, j = s + k; i < s + k; i++, j++) {
                    comp x = a[i], y = a[j];
                    a[i] = x + y, a[j] = w * (x - y);
                }
                w *= ir[__builtin_ctz(++t)];
            }
        }
        for (auto &e : a) e /= n;
    }

    static vector<T> convolve(vector<T> a, vector<T> b) {
        int k = (int)a.size() + (int)b.size() - 1, n = 1;
        while (n < k) n <<= 1;
        vector<comp> A = to_comp(a), B = to_comp(b);
        A.resize(n), B.resize(n);
        fft(A), fft(B);
        for (int i = 0; i < n; i++) A[i] *= B[i];
        ifft(A);
        vector<T> c = to_T(A);
        c.resize(k);
        return c;
    }
};

template <typename T>
double Fast_Fourier_Transform<T>::pi = acos(-1.0);

template <typename T>
vector<complex<double>> Fast_Fourier_Transform<T>::r = vector<complex<double>>();

template <typename T>
vector<complex<double>> Fast_Fourier_Transform<T>::ir = vector<complex<double>>();

using FFT = Fast_Fourier_Transform<int>;

struct Suffix_Array {
    vector<int> sa;
    const string s;
    const int n;

    Suffix_Array(const string &s) : s(s), n(s.size()) {
        sa.resize(n);
        iota(begin(sa), end(sa), 0);
        sort(begin(sa), end(sa), [&](int a, int b) { return s[a] == s[b] ? a > b : s[a] < s[b]; });
        vector<int> rank(n), c(begin(s), end(s)), cnt(n);
        for (int len = 1; len < n; len <<= 1) {
            for (int i = 0; i < n; i++) {
                if (i == 0 || c[sa[i - 1]] != c[sa[i]])
                    rank[sa[i]] = i;
                else {
                    if (sa[i - 1] + len >= n || c[sa[i - 1] + len / 2] != c[sa[i] + len / 2]) {
                        rank[sa[i]] = i;
                    } else {
                        rank[sa[i]] = rank[sa[i - 1]];
                    }
                }
            }
            iota(begin(cnt), end(cnt), 0);
            copy(begin(sa), end(sa), c.begin());
            for (int i = 0; i < n; i++) {
                int j = c[i] - len;
                if (j >= 0) sa[cnt[rank[j]]++] = j;
            }
            swap(rank, c);
        }
    }

    int operator[](int i) const { return sa[i]; }

    int size() const { return n; }

    bool compare_substr(const string &t, int si = 0, int ti = 0) const {
        int m = t.size();
        while (si < n && ti < m) {
            if (s[si] != t[ti]) return s[si] < t[ti];
            si++, ti++;
        }
        return si == n && ti < m;
    }

    // 辞書順で t 以降となるもので最初の接尾辞
    int lower_bound(const string &t) const {
        int l = -1, r = n;
        while (r - l > 1) {
            int m = (l + r) / 2;
            (compare_substr(t, sa[m]) ? l : r) = m;
        }
        return r;
    }

    int upper_bound(string t) const {
        t.back()++;
        return lower_bound(t);
    }
};

struct Longest_Common_Prefix_Array {
    vector<int> rank, lcp;
    const Suffix_Array sa;
    const int n;

    Longest_Common_Prefix_Array(const Suffix_Array &sa) : sa(sa), n(sa.size()) {
        rank.resize(n), lcp.resize(n - 1);
        for (int i = 0; i < n; i++) rank[sa[i]] = i;
        int h = 0;
        for (int i = 0; i < n; i++) {
            if (rank[i] + 1 < n) {
                int j = sa[rank[i] + 1];
                while (max(i, j) + h < n && sa.s[i + h] == sa.s[j + h]) h++;
                lcp[rank[i]] = h;
                if (h > 0) h--;
            }
        }
    }

    int operator[](int i) const { return lcp[i]; }
};

template <typename Monoid>
struct Segment_Tree {
    using M = typename Monoid::V;
    int n, m;
    vector<M> seg;

    // f(f(a,b),c) = f(a,f(b,c)), f(e1,a) = f(a,e1) = a

    Segment_Tree(const vector<M> &v) : n(v.size()) {
        m = 1;
        while (m < n) m <<= 1;
        seg.assign(2 * m, Monoid::id);
        copy(begin(v), end(v), begin(seg) + m);
        for (int i = m - 1; i > 0; i--) seg[i] = Monoid::merge(seg[2 * i], seg[2 * i + 1]);
    }

    Segment_Tree(int n, const M &x) : Segment_Tree(vector<M>(n, x)) {}

    void update(int i, const M &x, bool apply = false) {
        seg[i + m] = apply ? Monoid::merge(seg[i + m], x) : x;
        i += m;
        while (i >>= 1) seg[i] = Monoid::merge(seg[2 * i], seg[2 * i + 1]);
    }

    M query(int l, int r) const {
        l = max(l, 0), r = min(r, n);
        M L = Monoid::id, R = Monoid::id;
        l += m, r += m;
        while (l < r) {
            if (l & 1) L = Monoid::merge(L, seg[l++]);
            if (r & 1) R = Monoid::merge(seg[--r], R);
            l >>= 1, r >>= 1;
        }
        return Monoid::merge(L, R);
    }

    M operator[](int i) const { return seg[i + m]; }

    template <typename C>
    int find_subtree(int i, const C &check, M &x, int type) const {
        while (i < m) {
            M nxt = type ? Monoid::merge(seg[2 * i + type], x) : Monoid::merge(x, seg[2 * i + type]);
            if (check(nxt)) {
                i = 2 * i + type;
            } else {
                x = nxt;
                i = 2 * i + (type ^ 1);
            }
        }
        return i - m;
    }

    // check(区間 [l,r] での演算結果) を満たす最小の r (存在しなければ n)
    template <typename C>
    int find_first(int l, const C &check) const {
        M L = Monoid::id;
        int a = l + m, b = 2 * m;
        while (a < b) {
            if (a & 1) {
                M nxt = Monoid::merge(L, seg[a]);
                if (check(nxt)) return find_subtree(a, check, L, 0);
                L = nxt;
                a++;
            }
            a >>= 1, b >>= 1;
        }
        return n;
    }

    // check((区間 [l,r) での演算結果)) を満たす最大の l (存在しなければ -1)
    template <typename C>
    int find_last(int r, const C &check) const {
        M R = Monoid::id;
        int a = m, b = r + m;
        while (a < b) {
            if ((b & 1) || a == 1) {
                M nxt = Monoid::merge(seg[--b], R);
                if (check(nxt)) return find_subtree(b, check, R, 1);
                R = nxt;
            }
            a >>= 1, b >>= 1;
        }
        return -1;
    }
};

// sum
template <typename T>
struct Plus_Monoid {
    using V = T;
    static constexpr V merge(V l, V r) { return l + r; };
    static const V id;
};

template <typename T>
const T Plus_Monoid<T>::id = 0;

// min
template <typename T>
struct Min_Monoid {
    using V = T;
    static constexpr V merge(V l, V r) { return min(l, r); };
    static const V id;
};

template <typename T>
const T Min_Monoid<T>::id = numeric_limits<T>::max();

// max
template <typename T>
struct Max_Monoid {
    using V = T;
    static constexpr V merge(V l, V r) { return max(l, r); };
    static const V id;
};

template <typename T>
const T Max_Monoid<T>::id = numeric_limits<T>::min();

// min count (T:最大値の型、S:個数の型)
template <typename T, typename S>
struct Min_Count_Monoid {
    using V = pair<T, S>;
    static constexpr V merge(V l, V r) {
        if (l.first < r.first) return l;
        if (l.first > r.first) return r;
        return V(l.first, l.second + r.second);
    }
    static const V id;
};

template <typename T, typename S>
const pair<T, S> Min_Count_Monoid<T, S>::id = make_pair(numeric_limits<T>::max(), 0);

// max count (T:最大値の型、S:個数の型)
template <typename T, typename S>
struct Max_Count_Monoid {
    using V = pair<T, S>;
    static constexpr V merge(V l, V r) {
        if (l.first > r.first) return l;
        if (l.first < r.first) return r;
        return V(l.first, l.second + r.second);
    }
    static const V id;
};

template <typename T, typename S>
const pair<T, S> Max_Count_Monoid<T, S>::id = make_pair(numeric_limits<T>::min(), 0);

// 一次関数 ax+b の合成 (左から順に作用)
template <typename T>
struct Affine_Monoid {
    using V = pair<T, T>;
    static constexpr V merge(V l, V r) { return V(l.first * r.first, l.second * r.first + r.second); };
    static const V id;
};

template <typename T>
const pair<T, T> Affine_Monoid<T>::id = make_pair(1, 0);

// モノイドの直積
template <typename Monoid_1, typename Monoid_2>
struct Cartesian_Product_Monoid {
    using V1 = typename Monoid_1::V;
    using V2 = typename Monoid_2::V;
    using V = pair<V1, V2>;
    static constexpr V merge(V l, V r) { return V(Monoid_1::merge(l.first, r.first), Monoid_2::merge(l.second, r.second)); }
    static const V id;
};

template <typename Monoid_1, typename Monoid_2>
const pair<typename Monoid_1::V, typename Monoid_2::V> Cartesian_Product_Monoid<Monoid_1, Monoid_2>::id = make_pair(Monoid_1::id, Monoid_2::id);

// range add range min
template <typename T>
struct Min_Plus_Acted_Monoid {
    using Monoid = Min_Monoid<T>;
    using Operator = Plus_Monoid<T>;
    using M = T;
    using O = T;
    static constexpr M merge(M l, O r) { return l + r; };
};

// range add range max
template <typename T>
struct Max_Plus_Acted_Monoid {
    using Monoid = Max_Monoid<T>;
    using Operator = Plus_Monoid<T>;
    using M = T;
    using O = T;
    static constexpr M merge(M l, O r) { return l + r; };
};

// range add range min count (T:最小値の型、S:個数の型)
template <typename T, typename S>
struct Min_Count_Add_Acted_Monoid {
    using Monoid = Min_Count_Monoid<T, S>;
    using Operator = Plus_Monoid<T>;
    using M = pair<T, S>;
    using O = T;
    static constexpr M merge(M l, O r) { return make_pair(l.first + r, l.second); };
};

// range add range max count (T:最大値の型、S:個数の型)
template <typename T, typename S>
struct Max_Count_Add_Acted_Monoid {
    using Monoid = Max_Count_Monoid<T, S>;
    using Operator = Plus_Monoid<T>;
    using M = pair<T, S>;
    using O = T;
    static constexpr M merge(M l, O r) { return make_pair(l.first + r, l.second); };
};

// range affine range sum
template <typename T>
struct Plus_Affine_Acted_Monoid {
    using Monoid = Cartesian_Product_Monoid<Plus_Monoid<T>, Plus_Monoid<T>>;
    using Operator = Affine_Monoid<T>;
    using M = pair<T, T>;
    using O = pair<T, T>;
    static constexpr M merge(M l, O r) { return M(r.first * l.first + r.second * l.second, l.second); };
};

void solve() {
    int N, M;
    string S, T;
    cin >> N >> M >> S >> T;

    vector<int> f0(N), f1(N), f2(N), g0(M), g1(M), g2(M);

    rep(i, N) {
        int c = (S[i] == '?' ? 0 : S[i] - 'a' + 1);
        f0[i] = (c == 0 ? 0 : 1);
        f1[i] = f0[i] * c;
        f2[i] = f1[i] * c;
    }
    rep(i, M) {
        int c = (T[i] == '?' ? 0 : T[i] - 'a' + 1);
        g0[M - 1 - i] = (c == 0 ? 0 : 1);
        g1[M - 1 - i] = g0[M - 1 - i] * c;
        g2[M - 1 - i] = g1[M - 1 - i] * c;
    }

    auto h0 = FFT::convolve(f0, g2);
    auto h1 = FFT::convolve(f1, g1);
    auto h2 = FFT::convolve(f2, g0);

    vector<int> ng(N - M + 1, 0);
    rep(i, N - M + 1) {
        ng[i] = h0[i + M - 1] - 2 * h1[i + M - 1] + h2[i + M - 1]; //
    }

    rep(i, N) {
        if (S[i] == '?') S[i] = 'a';
    }
    string X = S + '$' + T;

    Suffix_Array sa(X);
    vector<int> rank(N + M + 1);
    rep(i, N + M + 1) rank[sa[i]] = i;
    Longest_Common_Prefix_Array lcp(sa);

    vector<int> v(N + M);
    rep(i, N + M) v[i] = lcp[i];
    Segment_Tree<Min_Monoid<int>> seg(v);

    auto comp = [&](int i, int j) {
        if (i > j) swap(i, j);
        int d = i - j;
        if (d >= M) {
            int l = rank[i], r = rank[N + 1];
            if (l > r) swap(l, r);
            return (seg.query(l, r) == M ? i : j);
        }
        {
            int l = rank[i], r = rank[N + 1];
            if (l > r) swap(l, r);
            int x = seg.query(l, r);
            if (x < d) {
                if (T[x] < S[i + x]) return i;
                return j;
            }
        }
        int l = rank[N + 1 + d], r = rank[N + 1];
        if (l > r) swap(l, r);
        int x = seg.query(l, r);
        if (x < M - d) {
            if (T[d + x] < T[x]) return i;
            return j;
        }
        return i;
    };

    int id = -1;

    rep(i, N - M + 1) {
        if (ng[i] == 0) {
            if (id == -1) {
                id = i;
            } else {
                id = comp(id, i);
            }
        }
    }

    if (id == -1) {
        cout << "-1\n";
        return;
    }
    rep(j, M) S[id + j] = T[j];

    cout << S << '\n';
}

int main() {
    int T;
    cin >> T;

    while (T--) solve();
}
0