結果

問題 No.2606 Mirror Relay
ユーザー akakimidoriakakimidori
提出日時 2024-01-22 22:23:50
言語 Rust
(1.77.0)
結果
AC  
実行時間 51 ms / 2,000 ms
コード長 4,247 bytes
コンパイル時間 7,909 ms
コンパイル使用メモリ 177,184 KB
実行使用メモリ 47,540 KB
最終ジャッジ日時 2024-01-22 22:24:03
合計ジャッジ時間 11,888 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
6,676 KB
testcase_01 AC 1 ms
6,676 KB
testcase_02 AC 1 ms
6,676 KB
testcase_03 AC 1 ms
6,676 KB
testcase_04 AC 1 ms
6,676 KB
testcase_05 AC 1 ms
6,676 KB
testcase_06 AC 2 ms
6,676 KB
testcase_07 AC 1 ms
6,676 KB
testcase_08 AC 1 ms
6,676 KB
testcase_09 AC 1 ms
6,676 KB
testcase_10 AC 2 ms
6,676 KB
testcase_11 AC 1 ms
6,676 KB
testcase_12 AC 1 ms
6,676 KB
testcase_13 AC 1 ms
6,676 KB
testcase_14 AC 1 ms
6,676 KB
testcase_15 AC 0 ms
6,676 KB
testcase_16 AC 1 ms
6,676 KB
testcase_17 AC 1 ms
6,676 KB
testcase_18 AC 1 ms
6,676 KB
testcase_19 AC 2 ms
6,676 KB
testcase_20 AC 2 ms
6,676 KB
testcase_21 AC 1 ms
6,676 KB
testcase_22 AC 1 ms
6,676 KB
testcase_23 AC 1 ms
6,676 KB
testcase_24 AC 2 ms
6,676 KB
testcase_25 AC 1 ms
6,676 KB
testcase_26 AC 2 ms
6,676 KB
testcase_27 AC 3 ms
6,676 KB
testcase_28 AC 1 ms
6,676 KB
testcase_29 AC 1 ms
6,676 KB
testcase_30 AC 1 ms
6,676 KB
testcase_31 AC 1 ms
6,676 KB
testcase_32 AC 1 ms
6,676 KB
testcase_33 AC 1 ms
6,676 KB
testcase_34 AC 1 ms
6,676 KB
testcase_35 AC 1 ms
6,676 KB
testcase_36 AC 1 ms
6,676 KB
testcase_37 AC 1 ms
6,676 KB
testcase_38 AC 1 ms
6,676 KB
testcase_39 AC 14 ms
6,676 KB
testcase_40 AC 15 ms
6,676 KB
testcase_41 AC 13 ms
6,676 KB
testcase_42 AC 13 ms
6,676 KB
testcase_43 AC 13 ms
6,676 KB
testcase_44 AC 14 ms
6,676 KB
testcase_45 AC 12 ms
6,676 KB
testcase_46 AC 13 ms
6,676 KB
testcase_47 AC 13 ms
6,676 KB
testcase_48 AC 13 ms
6,676 KB
testcase_49 AC 24 ms
15,360 KB
testcase_50 AC 24 ms
16,508 KB
testcase_51 AC 25 ms
17,036 KB
testcase_52 AC 20 ms
13,440 KB
testcase_53 AC 25 ms
17,188 KB
testcase_54 AC 25 ms
15,896 KB
testcase_55 AC 39 ms
25,588 KB
testcase_56 AC 25 ms
15,884 KB
testcase_57 AC 20 ms
13,952 KB
testcase_58 AC 28 ms
18,896 KB
testcase_59 AC 13 ms
6,676 KB
testcase_60 AC 11 ms
6,676 KB
testcase_61 AC 11 ms
6,676 KB
testcase_62 AC 10 ms
6,676 KB
testcase_63 AC 11 ms
6,676 KB
testcase_64 AC 11 ms
6,676 KB
testcase_65 AC 11 ms
6,676 KB
testcase_66 AC 11 ms
6,676 KB
testcase_67 AC 13 ms
6,676 KB
testcase_68 AC 11 ms
6,676 KB
testcase_69 AC 51 ms
47,540 KB
testcase_70 AC 1 ms
6,676 KB
testcase_71 AC 1 ms
6,676 KB
testcase_72 AC 1 ms
6,676 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

use std::collections::*;

type Map<K, V> = BTreeMap<K, V>;

fn main() {
    input!(s: chars);
    let mut tree = PalindromicTree::new();
    for s in s {
        tree.add(s);
    }
    let tree = tree;
    let mut cnt = vec![0; tree.node.len()];
    for p in tree.pos.iter() {
        cnt[*p] += 1;
    }
    for i in (2..cnt.len()).rev() {
        cnt[tree.node[i].fail] += cnt[i];
    }
    let mut dp = vec![0; cnt.len()];
    for i in 2..dp.len() {
        dp[i] = cnt[i] * tree.node[i].len + dp[tree.node[i].fail];
    }
    println!("{}", dp.into_iter().max().unwrap());
}

#[derive(Debug)]
struct Node<T> {
    fail: usize,
    len: usize,
    next: Map<T, usize>,
}

impl<T> Node<T>
where
    T: Copy + Ord,
{
    fn new(fail: usize, len: usize) -> Self {
        Self {
            fail,
            len,
            next: Map::new(),
        }
    }
    fn next(&self, x: T) -> Option<usize> {
        self.next.get(&x).cloned()
    }
    fn insert(&mut self, c: T, x: usize) {
        self.next.insert(c, x);
    }
}

pub struct PalindromicTree<T> {
    s: Vec<T>,
    pos: Vec<usize>,
    node: Vec<Node<T>>,
}

impl<T> PalindromicTree<T>
where
    T: Copy + Ord,
{
    pub fn new() -> Self {
        Self {
            s: vec![],
            pos: vec![],
            node: vec![Node::new(0, !0), Node::new(0, 0)],
        }
    }
    pub fn add(&mut self, c: T) {
        self.s.push(c);
        let all = self.s.len();
        let now = self.pos.last().map_or(1, |p| *p);
        let mut x = now;
        while x > 0 {
            let k = self.node[x].len;
            if k + 2 <= all && self.s[all - k - 2] == c {
                break;
            }
            x = self.node[x].fail;
        }
        let len = self.node[x].len + 2;
        let mut fail = 1;
        if x > 0 {
            fail = self.node[now].fail;
            while fail > 0 {
                let k = self.node[fail].len;
                if self.node[fail].next(c).is_some() && self.s[all - k - 2] == c {
                    break;
                }
                fail = self.node[fail].fail;
            }
            fail = self.node[fail].next(c).unwrap();
        }
        if let Some(z) = self.node[x].next(c) {
            self.pos.push(z);
        } else {
            let y = self.node.len();
            self.node[x].insert(c, y);
            self.node.push(Node::new(fail, len));
            self.pos.push(y);
        }
    }
}

impl<T> PalindromicTree<T>
where
    T: std::fmt::Debug,
{
    pub fn enumerate(&self, x: usize) {
        println!("enumerate: {}", x);
        let mut pos = self.pos[x];
        while pos > 1 {
            let len = self.node[pos].len;
            println!("{:?}", &self.s[(x - len + 1)..=x]);
            pos = self.node[pos].fail;
        }
    }
}

// ---------- begin input macro ----------
// reference: https://qiita.com/tanakh/items/0ba42c7ca36cd29d0ac8
#[macro_export]
macro_rules! input {
    (source = $s:expr, $($r:tt)*) => {
        let mut iter = $s.split_whitespace();
        input_inner!{iter, $($r)*}
    };
    ($($r:tt)*) => {
        let s = {
            use std::io::Read;
            let mut s = String::new();
            std::io::stdin().read_to_string(&mut s).unwrap();
            s
        };
        let mut iter = s.split_whitespace();
        input_inner!{iter, $($r)*}
    };
}

#[macro_export]
macro_rules! input_inner {
    ($iter:expr) => {};
    ($iter:expr, ) => {};
    ($iter:expr, $var:ident : $t:tt $($r:tt)*) => {
        let $var = read_value!($iter, $t);
        input_inner!{$iter $($r)*}
    };
}

#[macro_export]
macro_rules! read_value {
    ($iter:expr, ( $($t:tt),* )) => {
        ( $(read_value!($iter, $t)),* )
    };
    ($iter:expr, [ $t:tt ; $len:expr ]) => {
        (0..$len).map(|_| read_value!($iter, $t)).collect::<Vec<_>>()
    };
    ($iter:expr, chars) => {
        read_value!($iter, String).chars().collect::<Vec<char>>()
    };
    ($iter:expr, bytes) => {
        read_value!($iter, String).bytes().collect::<Vec<u8>>()
    };
    ($iter:expr, usize1) => {
        read_value!($iter, usize) - 1
    };
    ($iter:expr, $t:ty) => {
        $iter.next().unwrap().parse::<$t>().expect("Parse error")
    };
}
// ---------- end input macro ----------
0