結果

問題 No.2858 Make a Palindrome
ユーザー jupiter_68jupiter_68
提出日時 2024-08-25 16:11:53
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 3,704 bytes
コンパイル時間 4,221 ms
コンパイル使用メモリ 233,428 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-08-25 16:12:00
合計ジャッジ時間 6,828 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 294 ms
6,816 KB
testcase_01 WA -
testcase_02 WA -
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 AC 29 ms
6,944 KB
testcase_11 AC 29 ms
6,940 KB
testcase_12 AC 29 ms
6,944 KB
testcase_13 AC 30 ms
6,944 KB
testcase_14 AC 29 ms
6,940 KB
testcase_15 AC 3 ms
6,944 KB
testcase_16 AC 3 ms
6,944 KB
testcase_17 AC 3 ms
6,944 KB
testcase_18 AC 3 ms
6,940 KB
testcase_19 AC 3 ms
6,940 KB
testcase_20 AC 19 ms
6,940 KB
testcase_21 AC 19 ms
6,944 KB
testcase_22 AC 6 ms
6,940 KB
testcase_23 AC 18 ms
6,944 KB
testcase_24 AC 12 ms
6,940 KB
testcase_25 AC 14 ms
6,944 KB
testcase_26 AC 10 ms
6,940 KB
testcase_27 AC 28 ms
6,940 KB
testcase_28 AC 15 ms
6,940 KB
testcase_29 AC 26 ms
6,940 KB
testcase_30 AC 23 ms
6,940 KB
testcase_31 AC 21 ms
6,940 KB
testcase_32 AC 27 ms
6,944 KB
testcase_33 AC 24 ms
6,940 KB
testcase_34 AC 25 ms
6,944 KB
testcase_35 AC 29 ms
6,944 KB
testcase_36 AC 27 ms
6,944 KB
testcase_37 AC 27 ms
6,940 KB
testcase_38 AC 27 ms
6,940 KB
testcase_39 AC 23 ms
6,940 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

//#pragma GCC optimize("Ofast")
//#pragma GCC optimize("unroll-loops")

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using P = pair<ll, ll>;
using vi = vector<ll>;
using vd = vector<ld>;
using vP = vector<P>;
using vvi = vector<vector<ll>>;
using vvd = vector<vector<ld>>;
using vvP = vector<vector<P>>;
using vvvi = vector<vector<vector<ll>>>;
using vvvd = vector<vector<vector<ld>>>;
using vvvP = vector<vector<vector<P>>>;
#if __has_include(<atcoder/all>)
#include <atcoder/all>
using namespace atcoder;
using mint = modint998244353;
using Mint = modint1000000007;
using vm = vector<mint>;
using vM = vector<Mint>;
using vvm = vector<vector<mint>>;
using vvM = vector<vector<Mint>>;
using vvvm = vector<vector<vector<mint>>>;
using vvvM = vector<vector<vector<Mint>>>;
#endif
#define rrep(i, n) for (ll i = (n)-1; (i) >= 0; --(i))
#define rep(i, n) for (ll i = 0; (i) < (n); ++(i))
#define reps(i, n) for (ll i = 1; (i) <= (n); ++(i))
#define Rep(i,a,b) for(ll i = (a); i <= (b); i++)
#define all(a) (a).begin(),(a).end()
const ll MOD = 998244353;
#define Yes(b) ((b)?"Yes":"No")
#define YES(b) ((b)? 1 : -1)
#define ft first
#define sd second
bool chmin(ll& a, ll b) { return a > b ? a = b, 1 : 0; }
bool chmax(ll& a, ll b) { return a < b ? a = b, 1 : 0; }
//array s90 = { 0, 1, 0, -1 }, c90 = { 1, 0, -1, 0 };
//array s45 = { 0, 1, 1, 1, 0, -1, -1, -1 }, c45 = { 1, 1, 0, -1, -1, -1, 0, 1 };

signed main(){
    cin.tie(nullptr);
    ios_base::sync_with_stdio(false);
    cout << fixed << setprecision(20);
    ll t; cin >> t;
    rep(i, t){
        ll N, M; cin >> N >> M;
        ll len = 0;
        string S; cin >> S;
        string T = S; reverse(all(T));
        if(S == T) len = N;
        else{
            reps(i, N-1){
                if(S[0] == S[i-1] && T[0] == T[N - i - 1]){
                    string W = S.substr(0, i);
                    string V = W; reverse(all(V));
                    if(W == V){
                        W = S.substr(i);
                        V = W; reverse(all(W));
                        if(W == V) chmax(len, max(i, N - i));
                    }
                }
            }
        }
        if(len){
            ll ans = (M - len + N - 1) / N + 1;
            cout << ans << endl;
        }
        else{
            if(M >= N) cout << -1 << endl;
            else{
                string s(2 * N + 1, '#');
                rep(k, N) s[2 * k + 1] = S[k];
                N = 2 * N + 1;
                vi rad(N);
                ll c = 0, r = 0;
                ll len = 0;
                while (c < N) {
                    // cを中心に同じ文字がどこまで連続するか
                    while (0 <= c - r && c + r < N && s[c - r] == s[c + r]) r++;
                    rad[c] = r;
                    //回文の長さに応じて利用可能な範囲を確認しつつメモ
                    ll k = 1;
                    while (0 <= c - k && k + rad[c - k] < r) {
                        rad[c + k] = rad[c - k];
                        k++;
                    }
                    //すでに計算が終わった分だけ中心と探索半径をずらす
                    c += k;
                    r -= k;
                    rep(i, N){
                        if (i % 2 == 0)
                        rad[i] = (rad[i] - 1) / 2;
                        else
                        rad[i] /= 2;
                    }
                    ll Lmax = *max_element(rad.begin(), rad.end()) * 2 - 1;
                    chmax(len, Lmax);
                    //cout << Lmax << endl;
                }
                cout << YES(M <= len) << endl;
            }
        }
    }
}
0