結果
問題 | No.2858 Make a Palindrome |
ユーザー | t98slider |
提出日時 | 2024-08-25 15:06:06 |
言語 | C++17 (gcc 12.3.0 + boost 1.83.0) |
結果 |
WA
|
実行時間 | - |
コード長 | 3,302 bytes |
コンパイル時間 | 2,217 ms |
コンパイル使用メモリ | 207,284 KB |
実行使用メモリ | 23,364 KB |
最終ジャッジ日時 | 2024-08-25 15:06:28 |
合計ジャッジ時間 | 18,463 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge1 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 54 ms
6,812 KB |
testcase_01 | WA | - |
testcase_02 | WA | - |
testcase_03 | WA | - |
testcase_04 | WA | - |
testcase_05 | AC | 120 ms
6,940 KB |
testcase_06 | WA | - |
testcase_07 | WA | - |
testcase_08 | WA | - |
testcase_09 | WA | - |
testcase_10 | WA | - |
testcase_11 | WA | - |
testcase_12 | WA | - |
testcase_13 | WA | - |
testcase_14 | WA | - |
testcase_15 | WA | - |
testcase_16 | WA | - |
testcase_17 | WA | - |
testcase_18 | WA | - |
testcase_19 | WA | - |
testcase_20 | WA | - |
testcase_21 | WA | - |
testcase_22 | WA | - |
testcase_23 | AC | 521 ms
20,624 KB |
testcase_24 | WA | - |
testcase_25 | WA | - |
testcase_26 | AC | 382 ms
16,184 KB |
testcase_27 | WA | - |
testcase_28 | WA | - |
testcase_29 | AC | 560 ms
21,456 KB |
testcase_30 | WA | - |
testcase_31 | WA | - |
testcase_32 | WA | - |
testcase_33 | WA | - |
testcase_34 | WA | - |
testcase_35 | WA | - |
testcase_36 | WA | - |
testcase_37 | WA | - |
testcase_38 | WA | - |
testcase_39 | WA | - |
ソースコード
#include <bits/stdc++.h> using namespace std; using ll = long long; struct RollingHash{ long long BASE = 257, RMOD = (1ll<<61)-1; long long MASK31 = (1ll<<31)-1, MASK30 = (1ll<<30)-1; int n; std::vector<long long> dat, dat2, base_pow; RollingHash() {} RollingHash(std::string &s) : n(s.size()), dat(n+1), dat2(n+1), base_pow(n+1) { base_pow[0] = 1; for(int i = 0; i < n; i++){ dat[i+1] = safe_mod(internal_mul(dat[i], BASE) + s[i]); dat2.rbegin()[i+1] = safe_mod(internal_mul(dat2.rbegin()[i], BASE) + s.rbegin()[i]); base_pow[i+1] = internal_mul(base_pow[i], BASE); } } long long internal_mul(long long a, long long b){ long long au = a >> 31, ad = a & MASK31; long long bu = b >> 31, bd = b & MASK31; long long mid = ad * bu + au * bd; long long midu = mid >> 30; long long midd = mid & MASK30; return safe_mod(au * bu * 2 + midu + (midd << 31) + ad * bd); } long long safe_mod(long long x){ long long xu = x >> 61, xd = x & RMOD; long long res = xu + xd; if (res >= RMOD) res -= RMOD; return res; } long long hash(int l, int r){ long long res = dat[r] - internal_mul(dat[l], base_pow[r - l]); if(res < 0)res += RMOD; return res; } long long revhash(int l, int r){ long long res = dat2[l] - internal_mul(dat2[r], base_pow[r - l]); if(res < 0)res += RMOD; return res; } long long joint(long long lhs, long long rhs, long long rsize){ long long res = internal_mul(lhs, base_pow[rsize]) + rhs; if(res >= RMOD) res -= RMOD; return res; } }; bool para(string &s){ int l = 0, r = s.size() - 1; while(l < r){ if(s[l] != s[r]) return false; l++, r--; } return true; } int check(string &s){ int n = s.size(); RollingHash RH(s); int ans = 0; for(int i = 0; i < n; i++){ int ok = 0, ng = min(i, n - 1 - i) + 1; while(ok + 1 < ng){ int mid = (ok + ng) / 2; if(RH.hash(i - mid, i) == RH.revhash(i + 1, i + 1 + mid)) ok = mid; else ng = mid; } ans = max(ans, 1 + ok * 2); } for(int i = 1; i < n; i++){ int ok = 0, ng = min(i, n - i) + 1; while(ok + 1 < ng){ int mid = (ok + ng) / 2; if(RH.hash(i - mid, i) == RH.revhash(i, i + mid)) ok = mid; else ng = mid; } ans = max(ans, ok * 2); } return ans; } int main(){ ios::sync_with_stdio(false); cin.tie(0); int T; cin >> T; while(T--){ int n, m; string s; cin >> n >> m >> s; if(para(s)){ cout << (m + n - 1) / n << '\n'; continue; } string t = s; if(check(t) >= m){ cout << 1 << '\n'; continue; } t += s; if(check(t) >= m){ cout << 2 << '\n'; continue; } t += s; if(check(t) >= m){ cout << 3 << '\n'; continue; } t += s; if(check(t) >= m){ cout << 4 << '\n'; continue; } cout << -1 << '\n'; } }