結果

問題 No.2231 Surprising Flash!
ユーザー milanis48663220milanis48663220
提出日時 2023-02-24 23:13:16
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 3,391 bytes
コンパイル時間 1,292 ms
コンパイル使用メモリ 123,316 KB
実行使用メモリ 14,476 KB
最終ジャッジ日時 2023-10-11 08:55:29
合計ジャッジ時間 4,232 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 AC 3 ms
4,352 KB
testcase_02 AC 3 ms
4,348 KB
testcase_03 AC 166 ms
4,356 KB
testcase_04 AC 3 ms
4,352 KB
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 AC 1 ms
4,356 KB
testcase_09 WA -
testcase_10 AC 2 ms
4,352 KB
testcase_11 AC 15 ms
14,332 KB
testcase_12 AC 16 ms
14,392 KB
testcase_13 WA -
testcase_14 WA -
testcase_15 AC 14 ms
14,356 KB
testcase_16 AC 12 ms
4,348 KB
testcase_17 AC 11 ms
4,488 KB
testcase_18 AC 12 ms
12,256 KB
testcase_19 AC 13 ms
12,328 KB
testcase_20 AC 13 ms
12,472 KB
testcase_21 AC 12 ms
12,292 KB
testcase_22 AC 13 ms
12,332 KB
testcase_23 AC 13 ms
12,276 KB
testcase_24 AC 13 ms
12,280 KB
testcase_25 AC 13 ms
12,260 KB
testcase_26 AC 13 ms
12,264 KB
testcase_27 AC 12 ms
12,288 KB
testcase_28 WA -
testcase_29 AC 14 ms
13,332 KB
testcase_30 AC 14 ms
13,396 KB
testcase_31 AC 14 ms
13,376 KB
testcase_32 AC 13 ms
13,392 KB
testcase_33 AC 14 ms
13,392 KB
testcase_34 AC 13 ms
13,320 KB
testcase_35 AC 12 ms
13,328 KB
testcase_36 AC 12 ms
13,536 KB
testcase_37 AC 13 ms
13,320 KB
testcase_38 AC 12 ms
13,348 KB
testcase_39 AC 3 ms
4,352 KB
testcase_40 AC 3 ms
4,352 KB
testcase_41 WA -
testcase_42 AC 1 ms
4,352 KB
testcase_43 AC 2 ms
4,348 KB
testcase_44 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <algorithm>
#include <iomanip>
#include <vector>
#include <queue>
#include <deque>
#include <set>
#include <map>
#include <tuple>
#include <cmath>
#include <numeric>
#include <functional>
#include <cassert>

#define debug_value(x) cerr << "line" << __LINE__ << ":<" << __func__ << ">:" << #x << "=" << x << endl;
#define debug(x) cerr << "line" << __LINE__ << ":<" << __func__ << ">:" << x << endl;

template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }

using namespace std;
typedef long long ll;

template<typename T>
vector<vector<T>> vec2d(int n, int m, T v){
    return vector<vector<T>>(n, vector<T>(m, v));
}

template<typename T>
vector<vector<vector<T>>> vec3d(int n, int m, int k, T v){
    return vector<vector<vector<T>>>(n, vector<vector<T>>(m, vector<T>(k, v)));
}

template<typename T>
void print_vector(vector<T> v, char delimiter=' '){
    if(v.empty()) {
        cout << endl;
        return;
    }
    for(int i = 0; i+1 < v.size(); i++) cout << v[i] << delimiter;
    cout << v.back() << endl;
}

template<typename T>
class Cumsum{
    public:
    Cumsum(vector<T> v): v(v){
        n = v.size();
        v_cumsum = vector<T>(n+1, T(0));
        for(int i = 0; i < n; i++) v_cumsum[i+1] = v_cumsum[i]+v[i];
    }
    /**
     * v[l] + ... + v[r-1]
     */ 
    T sum(int l, int r){
        if(r <= l) return T(0);
        if(r > n) r = n;
        if(l < 0) l = 0;
        return v_cumsum[r]-v_cumsum[l];
    }
    private:
    int n;
    vector<T> v;
    vector<T> v_cumsum;
};

class KMP
{
public:
    string pattern;
    int plen;
    vector<int> table;
    KMP(const string s) : pattern(s), plen((int)pattern.size()), table(plen+1){
        table[0] = -1;
        int j = -1;
        for(int i = 0; i < plen; i++){
            while(j >= 0 && pattern[i] != pattern[j]) j = table[j];
            if(pattern[i+1] == pattern[++j]) table[i+1] = table[j];
            else table[i+1] = j;
        }
    }
    void search(const string& text, vector<int>& res){
        int head = 0, j = 0, tlen = (int)text.size();
        while(head + j < tlen){
            if(pattern[j] == text[head + j] || text[head+j] == '?'){
                if(++j != plen) continue;
                res.push_back(head);
            }
            head += j - table[j], j = max(table[j], 0);
        }
    }
};


void solve(){
    int n, m; cin >> n >> m;
    string s, t; cin >> s >> t;
    KMP kmp(t);
    vector<int> v;
    kmp.search(s, v);
    if(v.empty()){
        cout << -1 << endl;
        return;
    }
    vector<int> cnt(n);
    for(int i = 0; i < n; i++){
        if(s[i] == '?') cnt[i] = 1;
    }
    auto cumsum = Cumsum<int>(cnt);
        string ans = s;
    for(int i: v){
        if(cumsum.sum(i, i+m) == 0){
            for(int j = 0; j < n; j++) {
                if(ans[j] == '?') ans[j] = 'a';
            }
            cout << ans << endl;
            return;
        }
    }
    int i = v.back();
    for(int j = i; j < i+m; j++){
        ans[j] = t[j-i];
    }
    for(int j = 0; j < n; j++) {
        if(ans[j] == '?') ans[j] = 'a';
    }
    cout << ans << endl;
}

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout << setprecision(10) << fixed;
    int t; cin >> t;
    while(t--) solve();
}
0