結果
問題 | No.2858 Make a Palindrome |
ユーザー |
![]() |
提出日時 | 2024-08-25 15:17:30 |
言語 | C++23 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 502 ms / 3,000 ms |
コード長 | 1,116 bytes |
コンパイル時間 | 1,222 ms |
コンパイル使用メモリ | 97,492 KB |
実行使用メモリ | 16,304 KB |
最終ジャッジ日時 | 2024-08-25 15:17:36 |
合計ジャッジ時間 | 5,825 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge1 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 40 |
ソースコード
#include <iostream>#include <vector>#include <algorithm>using namespace std;vector<int> kai(string &s){int n = s.size(), c = 0;vector<int> ret(n);for (int i = 0; i < n; ++i){int l = 2 * c - i;if (c + ret[c] > i + ret[l]) ret[i] = ret[l];else{int j = max(c + ret[c] - i, 0);while (i - j >= 0 && i + j < n && s[i - j] == s[i + j]) ++j;ret[i] = j;c = i;}}return ret;}void solve(){int n, m;cin >> n >> m;string s;cin >> s;string tmp(2 * n, '#');for (int i = 0; i < n; ++i) tmp[2 * i] = s[i];string st = "#";int a[5] = {-1, -1, -1, -1, -1};for (int i = 0; i < 5; ++i){st += tmp;auto res = kai(st);int val = -1;for (auto v : res) val = max(val, v - 1);a[i] = val;}for (int i = 0; i < 5; ++i){if (a[i] >= m){cout << i + 1 << endl;return;}}int d = a[4] - a[3];if (d == 0){cout << -1 << endl;return;}int ans = (m - a[3] + d - 1) / d + 4;cout << ans << endl;}int main(){int t;cin >> t;while (t--) solve();}