結果

問題 No.263 Common Palindromes Extra
ユーザー akakimidoriakakimidori
提出日時 2024-01-22 22:43:37
言語 Rust
(1.77.0 + proconio)
結果
AC  
実行時間 202 ms / 2,000 ms
コード長 4,676 bytes
コンパイル時間 14,239 ms
コンパイル使用メモリ 377,980 KB
実行使用メモリ 174,776 KB
最終ジャッジ日時 2024-09-28 06:31:22
合計ジャッジ時間 14,395 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 6 ms
5,248 KB
testcase_01 AC 1 ms
5,248 KB
testcase_02 AC 1 ms
5,376 KB
testcase_03 AC 15 ms
7,180 KB
testcase_04 AC 68 ms
25,888 KB
testcase_05 AC 69 ms
26,036 KB
testcase_06 AC 8 ms
5,376 KB
testcase_07 AC 110 ms
60,888 KB
testcase_08 AC 131 ms
60,648 KB
testcase_09 AC 130 ms
97,608 KB
testcase_10 AC 202 ms
174,776 KB
testcase_11 AC 40 ms
25,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

use std::collections::*;

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

fn main() {
    input!(s: chars, t: chars);
    let mut tree = PalindromicTree::new();
    let mut a = vec![];
    for s in s {
        a.push(tree.add(s));
    }
    tree.add('$');
    tree.add('@');
    let mut b = vec![];
    for t in t {
        b.push(tree.add(t));
    }
    let tree = tree;
    let hist = |a: Vec<usize>| -> Vec<usize> {
        let mut cnt = vec![0; tree.node.len()];
        for p in a.iter() {
            cnt[*p] += 1;
        }
        for i in (2..cnt.len()).rev() {
            cnt[tree.node[i].fail()] += cnt[i];
        }
        cnt
    };
    let a = hist(a);
    let b = hist(b);
    let ans = a.into_iter().zip(b).skip(2).fold(0, |s, p| s + p.0 * p.1);
    println!("{}", ans);
}

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

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

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) -> usize {
        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);
        }
        self.pos.last().cloned().unwrap()
    }
}

/*
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