結果

問題 No.2231 Surprising Flash!
ユーザー KudeKude
提出日時 2023-02-24 23:28:17
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 827 ms / 4,000 ms
コード長 4,621 bytes
コンパイル時間 4,507 ms
コンパイル使用メモリ 265,052 KB
最終ジャッジ日時 2025-02-10 22:49:32
ジャッジサーバーID
(参考情報)
judge4 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 1
other AC * 44
権限があれば一括ダウンロードができます

ソースコード

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';
}
}
הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
0