結果

問題 No.2858 Make a Palindrome
ユーザー dyktr_06dyktr_06
提出日時 2024-05-13 01:26:36
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 411 ms / 3,000 ms
コード長 2,879 bytes
コンパイル時間 2,483 ms
コンパイル使用メモリ 206,376 KB
実行使用メモリ 9,208 KB
最終ジャッジ日時 2024-05-13 01:26:43
合計ジャッジ時間 6,167 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 411 ms
6,816 KB
testcase_01 AC 89 ms
6,944 KB
testcase_02 AC 86 ms
6,940 KB
testcase_03 AC 97 ms
6,940 KB
testcase_04 AC 92 ms
6,944 KB
testcase_05 AC 71 ms
6,944 KB
testcase_06 AC 52 ms
6,940 KB
testcase_07 AC 56 ms
6,940 KB
testcase_08 AC 54 ms
6,940 KB
testcase_09 AC 55 ms
6,940 KB
testcase_10 AC 62 ms
6,944 KB
testcase_11 AC 62 ms
6,940 KB
testcase_12 AC 62 ms
6,944 KB
testcase_13 AC 59 ms
6,940 KB
testcase_14 AC 61 ms
6,940 KB
testcase_15 AC 15 ms
6,940 KB
testcase_16 AC 15 ms
6,944 KB
testcase_17 AC 16 ms
6,944 KB
testcase_18 AC 15 ms
6,944 KB
testcase_19 AC 15 ms
6,940 KB
testcase_20 AC 19 ms
8,480 KB
testcase_21 AC 20 ms
8,648 KB
testcase_22 AC 10 ms
6,940 KB
testcase_23 AC 20 ms
8,480 KB
testcase_24 AC 17 ms
7,960 KB
testcase_25 AC 18 ms
7,528 KB
testcase_26 AC 15 ms
7,380 KB
testcase_27 AC 22 ms
9,024 KB
testcase_28 AC 17 ms
7,648 KB
testcase_29 AC 21 ms
8,680 KB
testcase_30 AC 20 ms
9,208 KB
testcase_31 AC 18 ms
9,204 KB
testcase_32 AC 20 ms
9,208 KB
testcase_33 AC 21 ms
9,208 KB
testcase_34 AC 20 ms
9,092 KB
testcase_35 AC 19 ms
9,092 KB
testcase_36 AC 20 ms
9,204 KB
testcase_37 AC 21 ms
9,096 KB
testcase_38 AC 19 ms
9,176 KB
testcase_39 AC 21 ms
9,192 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

template <typename T>
vector<int> manacher(const T &s){
    int n = s.size();
    vector<int> rad(n);
    int i = 0, j = 0;
    while(i < n){
        while(i - j >= 0 && i + j < (int) s.size() && s[i - j] == s[i + j]){
            j++;
        }
        rad[i] = j;
        int k = 1;
        while(i - k >= 0 && i + k < n && k + rad[i - k] < j){
            rad[i + k] = rad[i - k];
            k++;
        }
        i += k;
        j -= k;
    }
    return rad;
}

struct PalindromeCheck{
    const string s;
    string t;
    int n;
    vector<int> mana;

    PalindromeCheck(const string &str) : s(str){
        n = s.size();
        t = "$";
        for(int i = 0; i < n; i++){
            t += s[i];
            t += "$";
        }
        mana = manacher(t);
    }

    // [l, r)
    bool isPalindrome(int l, int r) const {
        int mid = l + r;
        return r * 2 - mid - 1 < mana[mid];
    }
};

const long long INF = 1LL << 60;

void solve(){
    int n;
    long long m;
    cin >> n >> m;
    string s;
    cin >> s;

    string u = "";
    {
        for(int i = 1; i <= 2; i++){
            u += s;
            int len = u.size();
            PalindromeCheck pc(u);
            for(int j = 0; j < len - m + 1; j++){
                if(pc.isPalindrome(j, j + m)){
                    cout << i << endl;
                    return;
                }
            }
            for(int j = 0; j < len - m; j++){
                if(pc.isPalindrome(j, j + m + 1)){
                    cout << i << endl;
                    return;
                }
            }
        }
    }

    long long ans = INF;
    PalindromeCheck pc(u);
    for(int i = 0; i < n; i++){
        for(int j = n; j <= n + 1; j++){
            if(pc.isPalindrome(i, i + j)){
                long long l = -1, r = -1;
                if(j % 2){
                    long long need = m / 2;
                    l = i + j / 2 - need, r = i + j / 2 + need;
                }else{
                    long long need = (m + 1) / 2;
                    l = i + j / 2 - need, r = i + j / 2 + need - 1;
                }

                // 二分探索使わなくてもできるはず。。。
                long long ok = 1e9, ng = -1;
                while(abs(ok - ng) > 1){
                    long long mid = (ok + ng) / 2;
                    if(l + mid * n >= 0){
                        ok = mid;
                    }else{
                        ng = mid;
                    }
                }
                l += ok * n, r += ok * n;

                ans = min(ans, 1LL + r / n);
            }
        }
    }

    if(ans == INF){
        cout << -1 << endl;
    }else{
        cout << ans << endl;
    }
}

int main(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int T; cin >> T;
    while(T--){
        solve();
    }
}
0