結果
| 問題 |
No.2858 Make a Palindrome
|
| コンテスト | |
| ユーザー |
ripity
|
| 提出日時 | 2024-08-25 16:31:38 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
WA
|
| 実行時間 | - |
| コード長 | 1,948 bytes |
| コンパイル時間 | 2,193 ms |
| コンパイル使用メモリ | 205,660 KB |
| 最終ジャッジ日時 | 2025-02-24 01:58:34 |
|
ジャッジサーバーID (参考情報) |
judge2 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 4 WA * 36 |
ソースコード
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
template <long long MOD>
struct rolling_hash {
long long base;
vector<long long> base_pow, hash_prefix;
rolling_hash(string str, long long _base) {
base = _base;
base_pow.resize(str.size()+1, 1);
hash_prefix.resize(str.size()+1, 0);
for( int i = 1; i <= str.size(); i++ ) base_pow[i] = (base_pow[i-1]*base)%MOD;
for( int i = 1; i <= str.size(); i++ ) hash_prefix[i] = (hash_prefix[i-1]*base+str[i-1])%MOD;
}
long long hash(int l, int r) {
return (MOD*MOD+hash_prefix[r]-hash_prefix[l]*base_pow[r-l])%MOD;
}
};
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 max_pal(string S) {
vector<int> ma = manacher(S);
return *max_element(ma.begin(), ma.end());
}
void solve() {
int N;
ll M;
string S;
cin >> N >> M >> S;
if(max_pal(S) >= M) {
cout << 1 << "\n";
return;
}else if(max_pal(S + S) >= M) {
cout << 2 << "\n";
return;
}
string T = S;
reverse(T.begin(), T.end());
mt19937 rng(time(NULL));
const ll B = rng() % 998244353;
rolling_hash<1000000007> rh7(S, B);
rolling_hash<1000000009> rh9(T, B);
ll INF = 1LL << 60, ans = INF;
for(int i = 0; i <= N; i++) {
bool pref = rh7.hash(0, i) == rh9.hash(N - i, N);
bool suff = rh7.hash(i, N) == rh9.hash(0, N - i);
// cout << rh7.hash(i, N) << " " << rh9.hash(0, N - i) << endl;
if(pref && suff) {
ll k = max(i, N - i);
ans = min(ans, (M + N - k + N - 1) / N);
}
}
cout << (ans == INF ? -1LL : ans) << endl;
}
int main() {
int T;
cin >> T;
while(T--) solve();
}
ripity