結果

問題 No.958 たぷりすたべる (回文)
ユーザー treeone
提出日時 2019-12-19 18:08:43
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
WA  
実行時間 -
コード長 1,177 bytes
コンパイル時間 2,344 ms
コンパイル使用メモリ 197,208 KB
最終ジャッジ日時 2025-01-08 12:46:49
ジャッジサーバーID
(参考情報)
judge3 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample WA * 2
other WA * 45 RE * 8
権限があれば一括ダウンロードができます

ソースコード

diff #

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

vector<int> manacher(string s){
    int i = 0, j = 0;
    vector<int> res(s.size());
    while (i < s.size()) {
        while (i - j >= 0 && i + j < s.size() && s[i - j] == s[i + j]) ++j;
        res[i] = j;
        int k = 1;
        while (i - k >= 0 && i + k < s.size() && k + res[i - k] < j) res[i + k] = res[i - k], ++k;
        i += k; j -= k;
    }
    return res;
}

int main(){
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    string S;
    ll K, Q;
    cin >> S >> K >> Q;
    ll N = S.size();
    string T = "";
    for(int i = 0; i < min(K, 3LL); i++){
        T += S;
    }
    vector<int> A = manacher(T);
    while(Q--){
        ll pos, ans = -1;
        cin >> pos;
        pos--;
        if(K <= 3){
            ans = A[pos];
        }else{
            if(2 * A[N + pos % N] - 1 >= N){
                ans = min(pos, K * N - 1 - pos) + 1;
            }else{
                if(pos < N) ans = A[pos];
                else if((K - 1) * N <= pos) ans = A[2 * N + pos % N];
                else ans = A[N + pos % N];
            }
        }
        cout << 2 * ans - 1 << endl;
    }
}
0