結果

問題 No.2231 Surprising Flash!
ユーザー Ricky_ponRicky_pon
提出日時 2023-06-14 01:37:14
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 7,744 bytes
コンパイル時間 2,979 ms
コンパイル使用メモリ 175,852 KB
実行使用メモリ 89,660 KB
最終ジャッジ日時 2024-06-22 13:05:04
合計ジャッジ時間 35,467 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,816 KB
testcase_01 AC 4 ms
6,944 KB
testcase_02 WA -
testcase_03 AC 400 ms
6,944 KB
testcase_04 AC 22 ms
6,940 KB
testcase_05 WA -
testcase_06 WA -
testcase_07 AC 3 ms
6,944 KB
testcase_08 AC 16 ms
6,940 KB
testcase_09 AC 2 ms
6,944 KB
testcase_10 AC 16 ms
6,940 KB
testcase_11 AC 1,058 ms
67,712 KB
testcase_12 AC 1,096 ms
89,660 KB
testcase_13 AC 1,084 ms
89,336 KB
testcase_14 WA -
testcase_15 AC 1,058 ms
67,600 KB
testcase_16 AC 1,069 ms
6,944 KB
testcase_17 AC 1,100 ms
9,988 KB
testcase_18 AC 1,023 ms
58,012 KB
testcase_19 AC 1,021 ms
57,980 KB
testcase_20 AC 1,018 ms
58,144 KB
testcase_21 AC 1,019 ms
57,992 KB
testcase_22 AC 1,024 ms
58,040 KB
testcase_23 AC 1,021 ms
58,096 KB
testcase_24 AC 1,013 ms
57,956 KB
testcase_25 AC 1,020 ms
57,928 KB
testcase_26 AC 1,020 ms
57,980 KB
testcase_27 AC 1,018 ms
58,032 KB
testcase_28 AC 1,019 ms
58,056 KB
testcase_29 AC 1,045 ms
67,724 KB
testcase_30 AC 1,050 ms
67,720 KB
testcase_31 AC 1,051 ms
67,660 KB
testcase_32 AC 1,047 ms
67,780 KB
testcase_33 AC 1,052 ms
67,716 KB
testcase_34 AC 1,047 ms
67,612 KB
testcase_35 AC 1,048 ms
67,600 KB
testcase_36 AC 1,051 ms
67,712 KB
testcase_37 AC 1,056 ms
67,724 KB
testcase_38 AC 1,048 ms
67,704 KB
testcase_39 AC 484 ms
9,440 KB
testcase_40 AC 328 ms
9,728 KB
testcase_41 WA -
testcase_42 WA -
testcase_43 WA -
testcase_44 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <string.h>

#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <cfloat>
#include <chrono>
#include <ciso646>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <optional>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

#define REP_OVERLOAD(arg1, arg2, arg3, arg4, NAME, ...) NAME
#define REP3(i, l, r, s)                                               \
    for (int i = int(l), rep3_r = int(r), rep3_s = int(s); i < rep3_r; \
         i += rep3_s)
#define REP2(i, l, r) REP3(i, l, r, 1)
#define REP1(i, n) REP2(i, 0, n)
#define rep(...) REP_OVERLOAD(__VA_ARGS__, REP3, REP2, REP1, )(__VA_ARGS__)
#define repin(i, l, r) for (int i = int(l), repin_r = int(r); i <= repin_r; ++i)

#define RREP_OVERLOAD(arg1, arg2, arg3, arg4, NAME, ...) NAME
#define RREP3(i, l, r, s)                                                      \
    for (int i = int(r) - 1, rrep3_l = int(l), rrep3_s = int(s); i >= rrep3_l; \
         i -= rrep3_s)
#define RREP2(i, l, r) RREP3(i, l, r, 1)
#define RREP1(i, n) RREP2(i, 0, n)
#define rrep(...) RREP_OVERLOAD(__VA_ARGS__, RREP3, RREP2, RREP1, )(__VA_ARGS__)
#define rrepin(i, l, r) \
    for (int i = int(r), rrepin_l = int(l); i >= rrepin_l; --i)


#include <algorithm>
#include <cassert>
#include <vector>

namespace rklib {

// a <- max(a, b)
template <class T>
bool chmax(T &a, const T &b) {
    if (a < b) {
        a = b;
        return true;
    }
    return false;
}

// a <- min(a, b)
template <class T>
bool chmin(T &a, const T &b) {
    if (a > b) {
        a = b;
        return true;
    }
    return false;
}

// if a < 0: a <- b
// else: a <- min(a, b)
template <class T>
bool chmin_non_negative(T &a, const T &b) {
    if (a < 0 || a > b) {
        a = b;
        return true;
    }
    return false;
}

// floor(num / den)
template <class T>
T div_floor(T num, T den) {
    if (den < 0) num = -num, den = -den;
    return num >= 0 ? num / den : (num + 1) / den - 1;
}

// ceil(num / den)
template <class T>
T div_ceil(T num, T den) {
    if (den < 0) num = -num, den = -den;
    return num <= 0 ? num / den : (num - 1) / den + 1;
}

namespace internal {

template <class T>
T remainder_count(T r, T b, T m) {
    return r / m * b + std::min(b, r % m);
}

}  // namespace internal

// Number of integer x s.t.
// - x in [l, r)
// - x mod m in [a, b)
template <class T>
T remainder_count(T l, T r, T a, T b, T m) {
    assert(l >= 0);
    assert(m >= 1);

    if (l >= r || a >= b) return 0;
    if (m <= a || b < 0) return 0;
    chmax(a, T(0));
    chmin(b, m);

    auto res = internal::remainder_count(r, b, m);
    if (l >= 1) res -= internal::remainder_count(l, b, m);
    if (a >= 1) res -= internal::remainder_count(r, a, m);
    if (l >= 1 && a >= 1) res += internal::remainder_count(l, a, m);

    return res;
}

}  // namespace rklib


using namespace std;
using namespace rklib;

using lint = long long;
using pii = pair<int, int>;
using pll = pair<lint, lint>;

#include <atcoder/convolution>

#include <string>
#include <vector>

namespace rklib {

struct RollingHash {
   private:
    using ulint = unsigned long long;
    const ulint hash_mod = (1ULL << 61ULL) - 1, hash_base = 1'000'000'007;
    const ulint mask_30bit = (1ULL << 30ULL) - 1,
                mask_31bit = (mask_30bit << 1ULL) + 1;
    const ulint num = hash_mod << 2LL;
    std::vector<ulint> pow_table, hash;
    int n;

    ulint calc_mul31(ulint a, ulint b) {
        ulint au = a >> 31ULL, ad = a & mask_31bit;
        ulint mid = au * b, midu = mid >> 30ULL, midd = mid & mask_30bit;
        return midu + (midd << 31ULL) + ad * b;
    }

    ulint calc_mul61(ulint a, ulint b) {
        ulint au = a >> 31ULL, ad = a & mask_31bit;
        ulint bu = b >> 31ULL, bd = b & mask_31bit;
        ulint mid = au * bd + ad * bu, midu = mid >> 30ULL,
              midd = mid & mask_30bit;
        return 2 * au * bu + midu + (midd << 31ULL) + ad * bd;
    }

    ulint calc_mod(ulint x) {
        x = (x & hash_mod) + (x >> 61ULL);
        if (x > hash_mod) x -= hash_mod;
        return x;
    }

   public:
    RollingHash(std::string &s) {
        n = s.size();
        pow_table.resize(n + 1);
        hash.resize(n + 1);
        pow_table[0] = 1ULL;
        for (int i = 0; i < n; ++i)
            pow_table[i + 1] = calc_mod(calc_mul61(pow_table[i], hash_base));
        hash[0] = 0ULL;
        for (int i = 0; i < n; ++i)
            hash[i + 1] = calc_mod(calc_mul61(hash[i], hash_base) + s[i]);
    }

    ulint slice(int l, int r) {
        return calc_mod(hash[r] + num - calc_mul61(hash[l], pow_table[r - l]));
    }
};

}  // namespace rklib


vector<int> wildcard_matching(string s, string t) {
    int n = s.size(), m = t.size();
    vector<vector<lint>> pow_s(4, vector<lint>(n, 1));
    vector<vector<lint>> pow_t(4, vector<lint>(m, 1));
    rep(k, 1, 4) rep(i, n) {
        pow_s[k][i] = pow_s[k - 1][i] * (s[i] == '?' ? 0 : s[i] - 'a' + 1);
    }
    reverse(t.begin(), t.end());
    rep(k, 1, 4) rep(i, m) {
        pow_t[k][i] = pow_t[k - 1][i] * (t[i] == '?' ? 0 : t[i] - 'a' + 1);
    }

    auto v1 = atcoder::convolution_ll(pow_s[3], pow_t[1]);
    auto v2 = atcoder::convolution_ll(pow_s[2], pow_t[2]);
    auto v3 = atcoder::convolution_ll(pow_s[1], pow_t[3]);

    vector<int> res;
    repin(i, m - 1, n - 1) {
        auto tmp = v1[i] - v2[i] * 2 + v3[i];
        if (tmp == 0) res.push_back(i - (m - 1));
    }
    return res;
}

void solve() {
    int n, m;
    string s, t;
    cin >> n >> m >> s >> t;

    auto pos = wildcard_matching(s, t);
    if (pos.empty()) {
        cout << "-1\n";
        return;
    }

    rep(i, n) if (s[i] == '?') s[i] = 'a';

    auto lcp = [](RollingHash &rs, RollingHash &rt, int l1, int l2, int len) {
        if (rs.slice(l1, l1 + len) == rt.slice(l2, l2 + len)) return len;
        int low = 0, high = len;
        while (high - low > 1) {
            int mid = (high + low) / 2;
            (rs.slice(l1, l1 + mid) == rt.slice(l2, l2 + mid) ? low : high) =
                mid;
        }
        return low;
    };

    RollingHash rs(s), rt(t);
    auto p =
        *min_element(pos.begin(), pos.end(), [&](const int &i, const int &j) {
            int a = i, b = j;
            if (a > b) swap(a, b);

            if (b <= a + m) {
                int len = lcp(rt, rs, 0, a, b - a);
                if (len < b - a) {
                    return t[len] < s[a + len] ? a : b;
                }
                len = lcp(rt, rt, b - a, 0, a + m - b);
                if (len < a + m - b) {
                    return t[b - a + len] < t[len] ? a : b;
                }
                len = lcp(rs, rt, 0, a + m - b, b - a);
                if (len < b - a) {
                    return s[len] < t[a + m - b + len] ? a : b;
                }
                return a;
            } else {
                int len = lcp(rt, rs, 0, a, m);
                if (len < m) {
                    return t[len] < s[a + len] ? a : b;
                }
                len = lcp(rs, rs, 0, a + m, b - a - m);
                if (len < b - a - m) {
                    return s[len] < s[a + m + len] ? a : b;
                }
                len = lcp(rs, rt, b - a - m, 0, m);
                if (len < m) {
                    return s[len + b - a - m] < t[len] ? a : b;
                }
                return a;
            }
        });

    rep(i, p, p + m) s[i] = t[i - p];
    cout << s << "\n";
}

int main() {
    int t;
    cin >> t;
    rep(tt, t) solve();
}
0