結果

問題 No.2231 Surprising Flash!
ユーザー AngrySadEightAngrySadEight
提出日時 2023-02-14 03:24:38
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
(最新)
AC  
(最初)
実行時間 -
コード長 4,083 bytes
コンパイル時間 2,966 ms
コンパイル使用メモリ 135,852 KB
実行使用メモリ 88,480 KB
最終ジャッジ日時 2023-10-11 08:19:14
合計ジャッジ時間 40,480 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,372 KB
testcase_01 AC 4 ms
4,368 KB
testcase_02 AC 39 ms
4,372 KB
testcase_03 AC 406 ms
4,372 KB
testcase_04 AC 23 ms
4,368 KB
testcase_05 AC 57 ms
4,376 KB
testcase_06 AC 79 ms
4,368 KB
testcase_07 AC 3 ms
4,372 KB
testcase_08 AC 15 ms
4,368 KB
testcase_09 AC 1 ms
4,372 KB
testcase_10 AC 16 ms
4,484 KB
testcase_11 AC 1,114 ms
66,896 KB
testcase_12 AC 1,123 ms
88,232 KB
testcase_13 AC 1,126 ms
88,480 KB
testcase_14 AC 1,033 ms
57,284 KB
testcase_15 AC 1,092 ms
66,956 KB
testcase_16 AC 1,053 ms
6,436 KB
testcase_17 AC 1,079 ms
9,800 KB
testcase_18 AC 1,078 ms
57,356 KB
testcase_19 AC 1,095 ms
57,412 KB
testcase_20 AC 1,085 ms
57,288 KB
testcase_21 AC 1,074 ms
57,300 KB
testcase_22 AC 1,073 ms
57,408 KB
testcase_23 AC 1,077 ms
57,412 KB
testcase_24 AC 1,079 ms
57,540 KB
testcase_25 AC 1,074 ms
57,400 KB
testcase_26 AC 1,063 ms
57,416 KB
testcase_27 AC 1,062 ms
57,424 KB
testcase_28 AC 1,055 ms
57,584 KB
testcase_29 AC 1,082 ms
66,904 KB
testcase_30 AC 1,109 ms
66,960 KB
testcase_31 AC 1,125 ms
67,160 KB
testcase_32 AC 1,106 ms
67,176 KB
testcase_33 AC 1,092 ms
67,028 KB
testcase_34 AC 1,030 ms
66,900 KB
testcase_35 AC 1,028 ms
67,172 KB
testcase_36 AC 1,045 ms
67,224 KB
testcase_37 AC 1,073 ms
67,036 KB
testcase_38 AC 1,040 ms
67,168 KB
testcase_39 AC 454 ms
9,208 KB
testcase_40 AC 294 ms
9,284 KB
testcase_41 AC 2 ms
4,368 KB
testcase_42 WA -
testcase_43 WA -
testcase_44 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <string>
#include <vector>
#include <atcoder/string>
#include <atcoder/convolution>
#include <atcoder/segtree>
using namespace std;
using namespace atcoder;
typedef long long ll;

template<typename T> void debug(vector<T> &v){
    for (ll i = 0; i < v.size(); i++){
        cerr << v[i] << " ";
    }
    cerr << endl;
}

ll ctol(char c){
    ll ret;
    if (c == '?') ret = 0;
    else{
        ret = (ll)(c - 'a' + 1);
    }
    return ret;
}

vector<ll> wild_card_matching(string &S, string &T){
    ll len_S = S.size();
    ll len_T = T.size();
    vector<vector<ll>> pow_S(4, vector<ll>(len_S));
    vector<vector<ll>> pow_T(4, vector<ll>(len_T));
    for (ll i = 0; i < len_S; i++){
        pow_S[1][i] = ctol(S[i]);
        for (ll j = 1; j < 3; j++){
            pow_S[j + 1][i] = pow_S[j][i] * pow_S[1][i];
        }
    }
    for (ll i = 0; i < len_T; i++){
        pow_T[1][i] = ctol(T[i]);
        for (ll j = 1; j < 3; j++){
            pow_T[j + 1][i] = pow_T[j][i] * pow_T[1][i];
        }
    }
    for (ll i = 0; i < 4; i++){
        reverse(pow_T[i].begin(), pow_T[i].end());
    }
    vector<ll> vec_conv1 = convolution_ll(pow_S[3], pow_T[1]);
    vector<ll> vec_conv2 = convolution_ll(pow_S[2], pow_T[2]);
    vector<ll> vec_conv3 = convolution_ll(pow_S[1], pow_T[3]);

    vector<ll> ret(0);
    for (ll i = 0; i < len_S - len_T + 1; i++){
        if (vec_conv1[len_T - 1 + i] - 2 *  vec_conv2[len_T - 1 + i] + vec_conv3[len_T - 1 + i] == 0){
            ret.push_back(i);
        }
    }
    return ret;
}

ll op(ll a, ll b){
    return min(a, b);
}

ll e(){
    return 1e9;
}

int main(){
    ll T;
    cin >> T;
    while(T--){
        ll N, M;
        cin >> N >> M;
        string S1;
        cin >> S1;
        string S2;
        cin >> S2;

        vector<ll> wcm = wild_card_matching(S1, S2);
        if (wcm.size() == 0){
            cout << -1 << endl;
            continue;
        }

        //debug(wcm);

        string S3 = "";
        for (ll i = 0; i < N; i++){
            if (S1[i] == '?') S3.push_back('a');
            else S3.push_back(S1[i]);
        }

        //S3 + S2 の LCP を求める
        vector<int> sa = suffix_array(S3 + S2);
        vector<int> lcp = lcp_array(S3 + S2, sa);
        vector<int> rev_sa(N + M);
        for (int i = 0; i < N + M; i++){
            rev_sa[sa[i]] = i;
        }
        segtree<ll, op, e> seg(N + M);
        for (ll i = 0; i < N + M; i++){
            seg.set(i, lcp[i]);
        }
        ll idx = 0;
        for (ll i = 1; i < wcm.size(); i++){
            ll left, right;
            ll diffs = wcm[i] - wcm[idx];
            if (diffs >= M){
                idx = i;
                continue;
            }
            left = min(rev_sa[wcm[idx]], rev_sa[N]);
            right = max(rev_sa[wcm[idx]], rev_sa[N]);
            ll lcp1 = seg.prod(left, right);
            if (lcp1 < diffs){
                if (S3[wcm[i] + lcp1] < S2[lcp1]){
                    idx = i;
                    continue;
                }
                else{
                    continue;
                }
            }
            left = min(rev_sa[N], rev_sa[N + diffs]);
            right = max(rev_sa[N], rev_sa[N + diffs]);
            ll lcp2 = seg.prod(left, right);
            if (lcp2 < M - diffs){
                if (S2[lcp2] < S2[diffs + lcp2]){
                    idx = i;
                    continue;
                }
                else{
                    continue;
                }
            }
            left = min(rev_sa[wcm[idx] + M], rev_sa[N + M - diffs]);
            right = max(rev_sa[wcm[idx] + M], rev_sa[N + M - diffs]);
            ll lcp3 = seg.prod(left, right);
            if (lcp3 < diffs){
                if (S2[M - diffs + lcp3] < S3[wcm[idx] + M + lcp3]){
                    idx = i;
                    continue;
                }
                else{
                    continue;
                }
            }
        }
        cout << S3.substr(0, wcm[idx]) << S2 << S3.substr(wcm[idx] + M, N - wcm[idx]) << endl;
    }
}
0