結果

問題 No.2858 Make a Palindrome
ユーザー GOTKAKOGOTKAKO
提出日時 2024-08-25 15:03:34
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
RE  
実行時間 -
コード長 1,607 bytes
コンパイル時間 2,322 ms
コンパイル使用メモリ 205,796 KB
実行使用メモリ 13,616 KB
最終ジャッジ日時 2024-08-25 15:03:44
合計ジャッジ時間 8,400 ms
ジャッジサーバーID
(参考情報)
judge3 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#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();
    s = t;
    return Mana(s); //M[i%2 == 0]=偶数は-1,M[i%2 == 1]=奇数は-1.
}

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

    int T; cin >> T;
    while(T--){
        int N,M; cin >> N >> M;
        string s; cin >> s;
        s += s+s+s;
        auto Ma = Mana2(s);
        int nowmax = 0,start = -1;
        for(int i=0; i<Ma.size(); i++){
            if(i%2 == 0 && Ma.at(i)%2 == 0) Ma.at(i)--;
            if(i%2 && Ma.at(i)%2) Ma.at(i)--;
            nowmax = max(nowmax,Ma.at(i));
            if(nowmax >= M){
                int m = Ma.at(i);
                if(i%2) m--,i--;
                start = (i/2)-(m/2-1);
                break;
            }
        }
        if(start != -1){cout << (start+M+N-1)/N << "\n"; continue;}

        for(int i=0; i<2*N; i++) if(Ma.at(i)+2*N == Ma.at(i+2*N)){
            int m = Ma.at(i);
            if(i%2) m--,i--;
            start = (i/2)-(m/2-1);
            assert(false);
            break;
        }
        if(start != -1) cout << (start+M+N-1)/N << "\n";
        else cout << "-1\n";
    }
}
0