結果

問題 No.958 たぷりすたべる (回文)
ユーザー koba-e964koba-e964
提出日時 2021-11-04 20:18:28
言語 Rust
(1.77.0)
結果
AC  
実行時間 46 ms / 2,000 ms
コード長 2,480 bytes
コンパイル時間 863 ms
コンパイル使用メモリ 174,504 KB
実行使用メモリ 11,372 KB
最終ジャッジ日時 2024-04-23 02:59:23
合計ジャッジ時間 3,969 ms
ジャッジサーバーID
(参考情報)
judge3 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 0 ms
6,812 KB
testcase_01 AC 1 ms
6,812 KB
testcase_02 AC 0 ms
6,940 KB
testcase_03 AC 1 ms
6,944 KB
testcase_04 AC 1 ms
6,940 KB
testcase_05 AC 1 ms
6,944 KB
testcase_06 AC 1 ms
6,940 KB
testcase_07 AC 1 ms
6,944 KB
testcase_08 AC 1 ms
6,944 KB
testcase_09 AC 1 ms
6,940 KB
testcase_10 AC 1 ms
6,944 KB
testcase_11 AC 0 ms
6,944 KB
testcase_12 AC 0 ms
6,940 KB
testcase_13 AC 1 ms
6,940 KB
testcase_14 AC 0 ms
6,944 KB
testcase_15 AC 1 ms
6,944 KB
testcase_16 AC 0 ms
6,940 KB
testcase_17 AC 0 ms
6,944 KB
testcase_18 AC 1 ms
6,944 KB
testcase_19 AC 1 ms
6,940 KB
testcase_20 AC 1 ms
6,944 KB
testcase_21 AC 1 ms
6,940 KB
testcase_22 AC 0 ms
6,940 KB
testcase_23 AC 0 ms
6,940 KB
testcase_24 AC 1 ms
6,940 KB
testcase_25 AC 1 ms
6,940 KB
testcase_26 AC 1 ms
6,940 KB
testcase_27 AC 10 ms
6,940 KB
testcase_28 AC 10 ms
6,944 KB
testcase_29 AC 10 ms
6,940 KB
testcase_30 AC 10 ms
6,940 KB
testcase_31 AC 10 ms
6,944 KB
testcase_32 AC 10 ms
6,940 KB
testcase_33 AC 10 ms
6,940 KB
testcase_34 AC 10 ms
6,940 KB
testcase_35 AC 11 ms
6,944 KB
testcase_36 AC 10 ms
6,940 KB
testcase_37 AC 12 ms
6,944 KB
testcase_38 AC 12 ms
6,940 KB
testcase_39 AC 12 ms
6,940 KB
testcase_40 AC 12 ms
6,940 KB
testcase_41 AC 13 ms
6,944 KB
testcase_42 AC 13 ms
6,940 KB
testcase_43 AC 12 ms
6,944 KB
testcase_44 AC 13 ms
6,944 KB
testcase_45 AC 13 ms
6,944 KB
testcase_46 AC 13 ms
6,940 KB
testcase_47 AC 37 ms
8,648 KB
testcase_48 AC 40 ms
11,124 KB
testcase_49 AC 41 ms
11,372 KB
testcase_50 AC 40 ms
11,372 KB
testcase_51 AC 43 ms
11,324 KB
testcase_52 AC 46 ms
11,316 KB
testcase_53 AC 42 ms
11,132 KB
testcase_54 AC 45 ms
11,268 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

use std::cmp::*;
use std::io::{Write, BufWriter};
// https://qiita.com/tanakh/items/0ba42c7ca36cd29d0ac8
macro_rules! input {
    ($($r:tt)*) => {
        let stdin = std::io::stdin();
        let mut bytes = std::io::Read::bytes(std::io::BufReader::new(stdin.lock()));
        let mut next = move || -> String{
            bytes.by_ref().map(|r|r.unwrap() as char)
                .skip_while(|c|c.is_whitespace())
                .take_while(|c|!c.is_whitespace())
                .collect()
        };
        input_inner!{next, $($r)*}
    };
}

macro_rules! input_inner {
    ($next:expr) => {};
    ($next:expr,) => {};
    ($next:expr, $var:ident : $t:tt $($r:tt)*) => {
        let $var = read_value!($next, $t);
        input_inner!{$next $($r)*}
    };
}

macro_rules! read_value {
    ($next:expr, [ $t:tt ; $len:expr ]) => {
        (0..$len).map(|_| read_value!($next, $t)).collect::<Vec<_>>()
    };
    ($next:expr, chars) => {
        read_value!($next, String).chars().collect::<Vec<char>>()
    };
    ($next:expr, $t:ty) => ($next().parse::<$t>().expect("Parse error"));
}

// Manacher http://snuke.hatenablog.com/entry/2014/12/02/235837
// Verified by https://atcoder.jp/contests/wupc2019/submissions/4540033
fn manacher<T: PartialEq>(tmp: &[T]) -> Vec<usize> {
    let n = tmp.len();
    let mut r = vec![0; n];
    {
        let mut i = 0;
        let mut j = 0;
        while i < n {
            while i >= j && i + j < n && tmp[i - j] == tmp[i + j] {
                j += 1;
            }
            r[i] = j;
            let mut k = 1;
            while i >= k && i + k < n && k + r[i - k] < j {
                r[i + k] = r[i - k];
                k += 1;
            }
            i += k;
            j -= k;
        }
    }
    r
}

fn main() {
    let out = std::io::stdout();
    let mut out = BufWriter::new(out.lock());
    macro_rules! puts {($($format:tt)*) => (let _ = write!(out,$($format)*););}
    input! {
        n: usize, k: i64, q: usize,
        s: chars,
        a: [i64; q],
    }
    let mut t = s.clone();
    t.extend(&s);
    t.extend(&s);
    let dp = manacher(&t);
    for a in a {
        let a = a - 1;
        let idx = (a as usize % n) + n;
        let len = dp[idx] - 1;
        let lim = min(a, n as i64 * k - 1 - a);
        if idx <= len || idx + len + 1 >= 3 * n {
            puts!("{}\n", lim * 2 + 1);
            continue;
        } else {
            puts!("{}\n", 2 * min(lim, len as i64) + 1);
        }
    }
}
0