結果
| 問題 |
No.2858 Make a Palindrome
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2024-08-25 16:58:09 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
WA
|
| 実行時間 | - |
| コード長 | 1,458 bytes |
| コンパイル時間 | 1,922 ms |
| コンパイル使用メモリ | 198,596 KB |
| 最終ジャッジ日時 | 2025-02-24 02:04:12 |
|
ジャッジサーバーID (参考情報) |
judge1 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 5 WA * 35 |
ソースコード
#include <bits/stdc++.h>
using namespace std;
vector<int> Mana(string &s){ //Manacher
int n = s.size();
vector<int> ret(n);
for(int i=0,w=0; i<n;){
while(i+w < n && i-w >= 0 && s.at(i-w) == s.at(i+w)) w++;
ret.at(i) = w;
int k = 1;
while(i-k >= 0 && k+ret.at(i-k) < w) ret.at(i+k) = ret.at(i-k),k++;
i += k; w -= k;
}
return ret;
}
vector<int> Mana2(string s){ //偶数長検出用.
string t = "";
for(auto &c : s) t += c,t += '$';
t.pop_back();
vector<int> ret = Mana(t); //M[i%2 == 0]=偶数は-1,M[i%2 == 1]=奇数は-1.
for(int i=0; i<ret.size(); i++) if(i%2 == ret.at(i)%2) ret.at(i)--;
return ret;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int T; cin >> T;
while(T--){
int N,M; cin >> N >> M;
string t; cin >> t;
string s;
int answer = -1;
for(int i=0; i<3; i++){
s += t;
auto Ma = Mana2(s);
for(auto &m : Ma) if(m >= M) answer = i+1;
if(answer != -1) break;
}
if(answer != -1){cout << answer << endl; continue;}
auto Ma = Mana2(s);
answer = 1001001001;
for(int i=2*N-1; i<4*N-2; i++) if(Ma.at(i) >= N){
int first = Ma.at(i)%N;
answer = min(answer,(M-first+N-1)/N);
}
if(answer == 1001001001) answer = -1;
cout << answer << "\n";
}
}