結果

問題 No.2858 Make a Palindrome
ユーザー GOTKAKOGOTKAKO
提出日時 2024-08-25 15:02:29
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 1,580 bytes
コンパイル時間 2,077 ms
コンパイル使用メモリ 206,364 KB
実行使用メモリ 13,624 KB
最終ジャッジ日時 2024-08-25 15:02:36
合計ジャッジ時間 4,422 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 WA -
testcase_02 WA -
testcase_03 WA -
testcase_04 WA -
testcase_05 AC 31 ms
6,940 KB
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 AC 20 ms
12,620 KB
testcase_22 WA -
testcase_23 AC 20 ms
12,404 KB
testcase_24 AC 18 ms
11,200 KB
testcase_25 WA -
testcase_26 AC 17 ms
9,296 KB
testcase_27 AC 23 ms
13,316 KB
testcase_28 WA -
testcase_29 AC 22 ms
12,768 KB
testcase_30 AC 22 ms
13,556 KB
testcase_31 WA -
testcase_32 AC 22 ms
13,512 KB
testcase_33 AC 22 ms
13,500 KB
testcase_34 AC 22 ms
13,428 KB
testcase_35 AC 22 ms
13,580 KB
testcase_36 AC 22 ms
13,432 KB
testcase_37 AC 23 ms
13,392 KB
testcase_38 WA -
testcase_39 AC 23 ms
13,580 KB
権限があれば一括ダウンロードができます

ソースコード

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);
            break;
        }
        if(start != -1) cout << (start+M+N-1)/N << "\n";
        else cout << "-1\n";
    }
}
0