結果
問題 | No.2231 Surprising Flash! |
ユーザー | Kude |
提出日時 | 2023-02-24 23:19:44 |
言語 | C++17 (gcc 12.3.0 + boost 1.83.0) |
結果 |
RE
|
実行時間 | - |
コード長 | 4,565 bytes |
コンパイル時間 | 4,630 ms |
コンパイル使用メモリ | 275,180 KB |
実行使用メモリ | 63,064 KB |
最終ジャッジ日時 | 2024-09-13 08:11:50 |
合計ジャッジ時間 | 40,827 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge1 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | RE | - |
testcase_01 | AC | 3 ms
6,816 KB |
testcase_02 | RE | - |
testcase_03 | WA | - |
testcase_04 | RE | - |
testcase_05 | RE | - |
testcase_06 | RE | - |
testcase_07 | RE | - |
testcase_08 | RE | - |
testcase_09 | RE | - |
testcase_10 | RE | - |
testcase_11 | RE | - |
testcase_12 | RE | - |
testcase_13 | RE | - |
testcase_14 | RE | - |
testcase_15 | RE | - |
testcase_16 | RE | - |
testcase_17 | RE | - |
testcase_18 | RE | - |
testcase_19 | RE | - |
testcase_20 | RE | - |
testcase_21 | RE | - |
testcase_22 | RE | - |
testcase_23 | RE | - |
testcase_24 | RE | - |
testcase_25 | RE | - |
testcase_26 | RE | - |
testcase_27 | RE | - |
testcase_28 | RE | - |
testcase_29 | RE | - |
testcase_30 | RE | - |
testcase_31 | RE | - |
testcase_32 | RE | - |
testcase_33 | RE | - |
testcase_34 | RE | - |
testcase_35 | RE | - |
testcase_36 | RE | - |
testcase_37 | RE | - |
testcase_38 | RE | - |
testcase_39 | RE | - |
testcase_40 | RE | - |
testcase_41 | RE | - |
testcase_42 | RE | - |
testcase_43 | RE | - |
testcase_44 | RE | - |
ソースコード
#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(n); 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 s[q1] < s[q2] ? -1 : 1; } 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'; } }