結果
問題 | No.263 Common Palindromes Extra |
ユーザー | akakimidori |
提出日時 | 2024-01-22 22:32:31 |
言語 | Rust (1.77.0 + proconio) |
結果 |
MLE
|
実行時間 | - |
コード長 | 4,494 bytes |
コンパイル時間 | 12,124 ms |
コンパイル使用メモリ | 377,864 KB |
実行使用メモリ | 229,476 KB |
最終ジャッジ日時 | 2024-09-28 06:30:53 |
合計ジャッジ時間 | 14,187 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge1 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 6 ms
5,248 KB |
testcase_01 | AC | 2 ms
5,248 KB |
testcase_02 | AC | 1 ms
5,376 KB |
testcase_03 | AC | 17 ms
7,048 KB |
testcase_04 | AC | 75 ms
26,520 KB |
testcase_05 | AC | 68 ms
25,644 KB |
testcase_06 | AC | 8 ms
5,376 KB |
testcase_07 | AC | 127 ms
74,688 KB |
testcase_08 | AC | 149 ms
74,356 KB |
testcase_09 | AC | 171 ms
124,976 KB |
testcase_10 | MLE | - |
testcase_11 | AC | 42 ms
25,384 KB |
ソースコード
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: 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) -> 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 ----------