結果

問題 No.2231 Surprising Flash!
ユーザー KudeKude
提出日時 2023-02-24 23:28:17
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 839 ms / 4,000 ms
コード長 4,621 bytes
コンパイル時間 4,586 ms
コンパイル使用メモリ 273,972 KB
実行使用メモリ 131,024 KB
最終ジャッジ日時 2023-09-03 02:55:28
合計ジャッジ時間 30,407 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 3 ms
4,380 KB
testcase_02 AC 27 ms
4,376 KB
testcase_03 AC 181 ms
4,380 KB
testcase_04 AC 16 ms
4,376 KB
testcase_05 AC 40 ms
4,380 KB
testcase_06 AC 54 ms
4,376 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 13 ms
4,380 KB
testcase_09 AC 2 ms
4,380 KB
testcase_10 AC 12 ms
4,660 KB
testcase_11 AC 772 ms
99,752 KB
testcase_12 AC 811 ms
129,096 KB
testcase_13 AC 816 ms
131,024 KB
testcase_14 AC 746 ms
84,652 KB
testcase_15 AC 776 ms
100,084 KB
testcase_16 AC 780 ms
7,524 KB
testcase_17 AC 817 ms
11,956 KB
testcase_18 AC 792 ms
83,724 KB
testcase_19 AC 789 ms
83,536 KB
testcase_20 AC 794 ms
83,672 KB
testcase_21 AC 796 ms
83,700 KB
testcase_22 AC 791 ms
83,680 KB
testcase_23 AC 791 ms
83,796 KB
testcase_24 AC 792 ms
83,472 KB
testcase_25 AC 788 ms
83,340 KB
testcase_26 AC 787 ms
83,528 KB
testcase_27 AC 789 ms
83,520 KB
testcase_28 AC 791 ms
83,424 KB
testcase_29 AC 837 ms
98,104 KB
testcase_30 AC 828 ms
98,092 KB
testcase_31 AC 839 ms
98,172 KB
testcase_32 AC 836 ms
98,228 KB
testcase_33 AC 830 ms
98,196 KB
testcase_34 AC 770 ms
99,800 KB
testcase_35 AC 767 ms
99,888 KB
testcase_36 AC 770 ms
99,864 KB
testcase_37 AC 772 ms
99,880 KB
testcase_38 AC 773 ms
99,688 KB
testcase_39 AC 357 ms
11,724 KB
testcase_40 AC 253 ms
11,708 KB
testcase_41 AC 1 ms
4,380 KB
testcase_42 AC 1 ms
4,376 KB
testcase_43 AC 2 ms
4,380 KB
testcase_44 AC 47 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
namespace {
#pragma GCC diagnostic ignored "-Wunused-function"
#include<atcoder/all>
#pragma GCC diagnostic warning "-Wunused-function"
using namespace std;
using namespace atcoder;
#define rep(i,n) for(int i = 0; i < (int)(n); i++)
#define rrep(i,n) for(int i = (int)(n) - 1; i >= 0; i--)
#define all(x) begin(x), end(x)
#define rall(x) rbegin(x), rend(x)
template<class T> bool chmax(T& a, const T& b) { if (a < b) { a = b; return true; } else return false; }
template<class T> bool chmin(T& a, const T& b) { if (b < a) { a = b; return true; } else return false; }
using ll = long long;
using P = pair<int,int>;
using VI = vector<int>;
using VVI = vector<VI>;
using VL = vector<ll>;
using VVL = vector<VL>;


template <class S, S (*op)(S, S)>
struct DisjointSparseTable {
  const int n;
  const vector<unsigned char> msb;
  const vector<vector<S>> d;
  DisjointSparseTable(vector<S> a) : n(a.size()), msb(build_msb_table(n)), d(build_table(move(a))) {}

  vector<unsigned char> build_msb_table(int n) {
    if (n <= 1) return {};
    unsigned char k_max = 32 - __builtin_clz(n - 1);
    vector<unsigned char> res(1 << k_max);
    unsigned char* p = res.data();
    for (unsigned char k = 0; k < k_max; k++) {
      memset(p + (1U << k), k, 1U << k);
    }
    return res;
  }
  vector<vector<S>> build_table(vector<S> a) {
    const int n = a.size();
    vector<vector<S>> res(1);
    if (n >= 2) {
      const int i_max = n - 1, k_max = 32 - __builtin_clz(i_max);
      res.resize(k_max);
      for (int k = 1; k < k_max; k++) {
        vector<S> t(i_max >> k & 1 ? n : i_max & ~0U << k);
        for (int c = 1 << k; c < n; c += 1 << (k + 1)) {
          int l = c - (1 << k);
          int r = min(n, c + (1 << k));
          t[c - 1] = a[c - 1];
          for (int i = c - 2; i >= l; i--) t[i] = op(a[i], t[i + 1]);
          t[c] = a[c];
          for (int i = c + 1; i < r; i++) t[i] = op(t[i - 1], a[i]);
        }
        res[k] = move(t);
      }
    }
    res[0] = move(a);
    return res;
  }

  S query(int l, int r) { return query_closed(l, r - 1); }
  S query_closed(int l, int r) {
    assert(0 <= l && l <= r && r < n);
    if (l == r) return d[0][l];
    auto k = msb[l ^ r];
    return op(d[k][l], d[k][r]);
  }
};

int op(int x, int y) { return min(x, y); }


} int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int tt;
  cin >> tt;
  while(tt--) {
    int n, m;
    cin >> n >> m;
    string s, t;
    cin >> s >> t;
    reverse(all(t));
    VL a(n), b(m);
    rep(i, n) a[i] = s[i] == '?' ? 0 : s[i] - 'a' + 1;
    rep(j, m) b[j] = t[j] - 'a' + 1;
    VL a2(n), b2(m);
    rep(i, n) a2[i] = a[i] * a[i];
    rep(j, m) b2[j] = b[j] * b[j];
    VL f1 = convolution_ll(a2, b);
    VL f2 = convolution_ll(a, b2);
    VL s3(n + 1);
    rep(i, n) s3[i + 1] = s3[i] + a[i] * a[i] * a[i];
    vector<char> d(n);
    rep(i, n - m + 1) {
      int p = m - 1 + i;
      ll x = s3[i + m] - s3[i] - 2 * f1[p] + f2[p];
      d[i] = x == 0;
    }
    reverse(all(t));
    rep(i, n) if (s[i] == '?') s[i] = 'a';
    string st = s + '$' + t;
    int nm = n + m + 1;
    assert(st.size() == nm);
    VI sa = suffix_array(st);
    VI sa_inv(nm);
    for (int i = 0; i < nm; i++) sa_inv[sa[i]] = i;
    DisjointSparseTable<int, op> dst(lcp_array(st, sa));
    auto lcp = [&](int i, int j) {
      if (i == j) return n - i;
      int pi = sa_inv[i], pj = sa_inv[j];
      if (pi > pj) swap(pi, pj);
      return dst.query(pi, pj);
    };
    VI idx;
    rep(i, n) if (d[i]) idx.emplace_back(i);
    if (idx.empty()) {
      cout << -1 << '\n';
      continue;
    }
    auto cmp_sub = [&](int i, int j) {
      assert(i < j);
      // s[n+1, n+m+1) + s[i+m, j+m)
      // s[i, j) + s[n+1, n+m+1)
      int pos1[2]{n + 1, i + m};
      int len1[2]{m, j - i};
      int pos2[2]{i, n + 1};
      int len2[2]{j - i, m};
      int p1 = 0, p2 = 0;
      int l1 = 0, l2 = 0;
      while(p1 != 2) {
        int l = lcp(pos1[p1] + l1, pos2[p2] + l2);
        if (l1 + l < len1[p1] && l2 + l < len2[p2]) {
          int q1 = pos1[p1] + l1 + l;
          int q2 = pos2[p2] + l2 + l;
          return st[q1] < st[q2] ? -1 : 1;
        }
        chmin(l, min(len1[p1] - l1, len2[p2] - l2));
        l1 += l;
        l2 += l;
        if (l1 >= len1[p1]) p1++, l1 = 0;
        if (l2 >= len2[p2]) p2++, l2 = 0;
      }
      return 0;
    };
    int i0 = *min_element(all(idx), [&](int i, int j) {
      bool flip = i > j;
      if (flip) swap(i, j);
      int res = cmp_sub(i, j);
      if (flip) res = -res;
      return res < 0;
    });
    rep(j, m) s[i0 + j] = t[j];
    cout << s << '\n';
  }
}
0