結果

問題 No.2231 Surprising Flash!
ユーザー apricityapricity
提出日時 2023-05-04 09:09:51
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 858 ms / 4,000 ms
コード長 6,539 bytes
コンパイル時間 3,720 ms
コンパイル使用メモリ 186,488 KB
実行使用メモリ 137,428 KB
最終ジャッジ日時 2023-09-03 02:58:51
合計ジャッジ時間 29,864 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 26 ms
4,376 KB
testcase_03 AC 195 ms
4,380 KB
testcase_04 AC 16 ms
4,380 KB
testcase_05 AC 41 ms
4,376 KB
testcase_06 AC 55 ms
4,376 KB
testcase_07 AC 3 ms
4,376 KB
testcase_08 AC 11 ms
4,380 KB
testcase_09 AC 2 ms
4,376 KB
testcase_10 AC 12 ms
4,652 KB
testcase_11 AC 773 ms
106,120 KB
testcase_12 AC 820 ms
137,292 KB
testcase_13 AC 839 ms
137,428 KB
testcase_14 AC 760 ms
88,296 KB
testcase_15 AC 776 ms
106,152 KB
testcase_16 AC 762 ms
7,844 KB
testcase_17 AC 813 ms
12,636 KB
testcase_18 AC 809 ms
89,468 KB
testcase_19 AC 803 ms
89,300 KB
testcase_20 AC 805 ms
89,224 KB
testcase_21 AC 808 ms
89,236 KB
testcase_22 AC 812 ms
89,524 KB
testcase_23 AC 808 ms
89,428 KB
testcase_24 AC 808 ms
88,784 KB
testcase_25 AC 806 ms
88,956 KB
testcase_26 AC 809 ms
88,812 KB
testcase_27 AC 809 ms
88,972 KB
testcase_28 AC 805 ms
88,928 KB
testcase_29 AC 854 ms
107,872 KB
testcase_30 AC 858 ms
108,028 KB
testcase_31 AC 857 ms
107,672 KB
testcase_32 AC 854 ms
107,684 KB
testcase_33 AC 850 ms
107,772 KB
testcase_34 AC 770 ms
105,036 KB
testcase_35 AC 771 ms
104,976 KB
testcase_36 AC 774 ms
104,980 KB
testcase_37 AC 778 ms
104,804 KB
testcase_38 AC 769 ms
104,904 KB
testcase_39 AC 326 ms
7,948 KB
testcase_40 AC 220 ms
7,864 KB
testcase_41 AC 2 ms
4,376 KB
testcase_42 AC 2 ms
4,380 KB
testcase_43 AC 2 ms
4,376 KB
testcase_44 AC 36 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
#include<numeric>
#include<cmath>
#include<utility>
#include<tuple>
#include<cstdint>
#include<cstdio>
#include<iomanip>
#include<map>
#include<queue>
#include<set>
#include<stack>
#include<deque>
#include<unordered_map>
#include<unordered_set>
#include<bitset>
#include<cctype>
#include<chrono>
#include<random>
#include<cassert>
#include<cstddef>
#include<iterator>
#include<string_view>
#include<type_traits>

#ifdef LOCAL
#  include "debug_print.hpp"
#  define debug(...) debug_print::multi_print(#__VA_ARGS__, __VA_ARGS__)
#else
#  define debug(...) (static_cast<void>(0))
#endif

using namespace std;
#define rep(i,n) for(int i=0; i<(n); i++)
#define rrep(i,n) for(int i=(n)-1; i>=0; i--)
#define FOR(i,a,b) for(int i=(a); i<(b); i++)
#define RFOR(i,a,b) for(int i=(b-1); i>=(a); i--)
#define ALL(v) v.begin(), v.end()
#define RALL(v) v.rbegin(), v.rend()
#define UNIQUE(v) v.erase( unique(v.begin(), v.end()), v.end() );
#define pb push_back
using ll = long long;
using D = double;
using LD = long double;
using P = pair<int, int>;
template<typename T> using PQ = priority_queue<T,vector<T>>;
template<typename T> using minPQ = priority_queue<T, vector<T>, greater<T>>;
template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; }
void yesno(bool flag) {cout << (flag?"Yes":"No") << "\n";}

template<typename T, typename U>
ostream &operator<<(ostream &os, const pair<T, U> &p) {
    os << p.first << " " << p.second;
    return os;
}
template<typename T, typename U>
istream &operator>>(istream &is, pair<T, U> &p) {
    is >> p.first >> p.second;
    return is;
}

template<typename T>
ostream &operator<<(ostream &os, const vector<T> &v) {
    int s = (int)v.size();
    for (int i = 0; i < s; i++) os << (i ? " " : "") << v[i];
    return os;
}
template<typename T>
istream &operator>>(istream &is, vector<T> &v) {
    for (auto &x : v) is >> x;
    return is;
}
void in() {}
template<typename T, class... U>
void in(T &t, U &...u) {
    cin >> t;
    in(u...);
}
void out() { cout << "\n"; }
template<typename T, class... U, char sep = ' '>
void out(const T &t, const U &...u) {
    cout << t;
    if (sizeof...(u)) cout << sep;
    out(u...);
}
void outr() {}
template<typename T, class... U, char sep = ' '>
void outr(const T &t, const U &...u) {
    cout << t;
    outr(u...);
}

#include "atcoder/convolution.hpp"
#include "atcoder/string.hpp"

/**
 * @brief Sparse-Table(スパーステーブル)
 * @docs docs/sparse-table.md
 */
template< typename T, typename F >
struct SparseTable {
  F f;
  vector< vector< T > > st;
  vector< int > lookup;

  SparseTable() = default;

  explicit SparseTable(const vector< T > &v, const F &f) : f(f) {
    const int n = (int) v.size();
    const int b = 32 - __builtin_clz(n);
    st.assign(b, vector< T >(n));
    for(int i = 0; i < v.size(); i++) {
      st[0][i] = v[i];
    }
    for(int i = 1; i < b; i++) {
      for(int j = 0; j + (1 << i) <= n; j++) {
        st[i][j] = f(st[i - 1][j], st[i - 1][j + (1 << (i - 1))]);
      }
    }
    lookup.resize(v.size() + 1);
    for(int i = 2; i < lookup.size(); i++) {
      lookup[i] = lookup[i >> 1] + 1;
    }
  }

  inline T fold(int l, int r) const {
    int b = lookup[r - l];
    return f(st[b][l], st[b][r - (1 << b)]);
  }
};

template< typename T, typename F >
SparseTable< T, F > get_sparse_table(const vector< T > &v, const F &f) {
  return SparseTable< T, F >(v, f);
}

void solve(){
    int n,m; string s,t; in(n,m,s,t);
    vector<ll> x3_cs(n+1),x_rev(n),x2_rev(n),y(m),y2(m);
    rep(i,n) {
        int val = (s[i] == '?' ? 0 : (s[i]-'a')+1);
        x3_cs[i+1] = x3_cs[i] + val*val*val;
        x_rev[n-1-i] = val;
        x2_rev[n-1-i] = val*val;

    }
    rep(i,m) {
        int val = (t[i]-'a')+1;
        y[i] = val;
        y2[i] = val*val;
    }

    vector<int> pos_match;
    auto xxy = atcoder::convolution_ll(x2_rev, y);
    auto xyy = atcoder::convolution_ll(x_rev, y2);
    rep(i,n-m+1) {
        ll sum = x3_cs[i+m] - x3_cs[i] - 2 * xxy[n-1-i] + xyy[n-1-i];
        if (sum == 0) pos_match.pb(i);
    }

    if (pos_match.empty()) {
        out("-1");
        return;
    }

    string sr = s;
    rep(i,n) if(sr[i] == '?') sr[i] = 'a';
    auto ztt = atcoder::z_algorithm(t);
    string u = t+sr;
    auto sa = atcoder::suffix_array(u);
    auto lc = atcoder::lcp_array(u, sa);
    vector<int> sa_inv(n+m);
    rep(i,n+m) sa_inv[sa[i]] = i;
    auto spt = get_sparse_table(lc, [](auto a, auto b){return min(a,b);});

    int idx = pos_match[0];
    FOR(i,1,pos_match.size()) {
        int pos = pos_match[i];
        if (idx+m <= pos) {
            int p = sa_inv[0], q = sa_inv[m+idx];
            if (p > q) swap(p,q);
            int len = spt.fold(p,q);
            if (len < m) {
                char a = t[len], b = sr[idx+len];
                if (a > b) {
                    idx = pos;
                }
                continue;
            }

            p = sa_inv[0], q = sa_inv[m+pos];
            if (p > q) swap(p,q);
            len = spt.fold(p,q);
            if (len < m) {
                char a = t[len], b = sr[idx+len];
                if (a > b) {
                    idx = pos;
                }
                continue;
            }
        }
        else {
            int p = sa_inv[0], q = sa_inv[m+idx];
            if (p > q) swap(p,q);
            int len = spt.fold(p,q);
            if (len < pos-idx) {
                char a = t[len], b = sr[idx+len];
                if (a > b) {
                    idx = pos;
                }
                continue;
            }

            len = ztt[pos-idx];
            if (len < m-pos+idx) {
                char a = t[len+pos-idx], b = t[len];
                if (a > b) {
                    idx = pos;
                }
                continue;
            }

            p = sa_inv[idx+m+m], q = sa_inv[m-pos+idx];
            if (p > q) swap(p,q);
            len = spt.fold(p,q);
            if (len < pos-idx) {
                char a = sr[len+idx+m], b = t[m-pos+idx+len];
                if (a > b) {
                    idx = pos;
                }
                continue;
            }
        }
    }

    string ans = sr;
    rep(i,m) ans[i+idx] = t[i];
    out(ans);
}

int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    int ntcs; in(ntcs);
    while(ntcs--) {
        solve();
    }
}
0