結果
問題 | No.2858 Make a Palindrome |
ユーザー | t98slider |
提出日時 | 2024-08-25 16:21:33 |
言語 | C++17 (gcc 12.3.0 + boost 1.83.0) |
結果 |
WA
|
実行時間 | - |
コード長 | 3,625 bytes |
コンパイル時間 | 2,507 ms |
コンパイル使用メモリ | 208,244 KB |
実行使用メモリ | 69,628 KB |
最終ジャッジ日時 | 2024-08-25 16:23:02 |
合計ジャッジ時間 | 87,594 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge1 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 201 ms
6,812 KB |
testcase_01 | WA | - |
testcase_02 | WA | - |
testcase_03 | WA | - |
testcase_04 | WA | - |
testcase_05 | AC | 2,446 ms
6,944 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 | 2,197 ms
60,904 KB |
testcase_24 | WA | - |
testcase_25 | WA | - |
testcase_26 | AC | 1,585 ms
45,836 KB |
testcase_27 | WA | - |
testcase_28 | WA | - |
testcase_29 | AC | 2,310 ms
63,844 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; template<class T> istream& operator >> (istream& is, vector<T>& vec) { for(T& x : vec) is >> x; return is; } template<class T> ostream& operator << (ostream& os, const vector<T>& vec) { if(vec.empty()) return os; os << vec[0]; for(auto it = vec.begin(); ++it != vec.end(); ) os << ' ' << *it; return os; } 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; } vector<int> manacher(const string &s) { vector<int> radius(s.size()); int i = 0, j = 0; while(i < s.size()) { while(i - j >= 0 && i + j < s.size() && s[i - j] == s[i + j]) { ++j; } radius[i] = j; int k = 1; while(i - k >= 0 && i + k < s.size() && k + radius[i - k] < j) { radius[i + k] = radius[i - k]; ++k; } i += k; j -= k; } return radius; } int check(string &s){ int n = s.size(); RollingHash RH(s); auto vec = manacher(s); int ans = 0; for(int i = 0; i < n; i++) ans = max(ans, 2 * vec[i] - 1); 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; clock_t finish = clock() + (CLOCKS_PER_SEC / 1000 * 2300) * double(n) / 200000; if(para(s)){ cout << (m + n - 1) / n << '\n'; continue; } string t; int ans = -1; for(int i = 1; clock() <= finish; i++){ t += s; if(check(t) >= m){ ans = i; break; } } cout << ans << '\n'; } }