結果

問題 No.2231 Surprising Flash!
ユーザー Ricky_ponRicky_pon
提出日時 2023-06-14 01:32:06
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 7,736 bytes
コンパイル時間 2,878 ms
コンパイル使用メモリ 176,096 KB
実行使用メモリ 89,528 KB
最終ジャッジ日時 2024-06-22 12:56:08
合計ジャッジ時間 36,724 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,812 KB
testcase_01 WA -
testcase_02 WA -
testcase_03 AC 400 ms
6,944 KB
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 AC 16 ms
6,944 KB
testcase_09 AC 2 ms
6,944 KB
testcase_10 AC 15 ms
6,944 KB
testcase_11 AC 1,059 ms
67,596 KB
testcase_12 AC 1,096 ms
89,528 KB
testcase_13 WA -
testcase_14 WA -
testcase_15 AC 1,063 ms
67,596 KB
testcase_16 AC 1,073 ms
6,940 KB
testcase_17 AC 1,108 ms
9,860 KB
testcase_18 AC 1,028 ms
58,008 KB
testcase_19 AC 1,030 ms
57,936 KB
testcase_20 AC 1,019 ms
58,136 KB
testcase_21 AC 1,026 ms
58,064 KB
testcase_22 AC 1,031 ms
58,164 KB
testcase_23 AC 1,027 ms
58,088 KB
testcase_24 AC 1,027 ms
58,148 KB
testcase_25 AC 1,039 ms
58,088 KB
testcase_26 AC 1,021 ms
58,060 KB
testcase_27 AC 1,024 ms
57,984 KB
testcase_28 AC 1,028 ms
58,088 KB
testcase_29 AC 1,054 ms
67,600 KB
testcase_30 AC 1,048 ms
67,596 KB
testcase_31 AC 1,051 ms
67,728 KB
testcase_32 AC 1,048 ms
67,680 KB
testcase_33 AC 1,051 ms
67,704 KB
testcase_34 WA -
testcase_35 WA -
testcase_36 WA -
testcase_37 WA -
testcase_38 WA -
testcase_39 AC 487 ms
9,568 KB
testcase_40 AC 331 ms
9,604 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');
    }
    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');
    }

    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 =
        *max_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