結果
問題 |
No.2162 Copy and Paste 2
|
ユーザー |
![]() |
提出日時 | 2021-12-21 11:11:34 |
言語 | Rust (1.83.0 + proconio) |
結果 |
MLE
|
実行時間 | - |
コード長 | 2,169 bytes |
コンパイル時間 | 14,720 ms |
コンパイル使用メモリ | 378,832 KB |
実行使用メモリ | 623,956 KB |
最終ジャッジ日時 | 2024-11-06 21:06:48 |
合計ジャッジ時間 | 23,463 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge4 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 3 MLE * 1 -- * 22 |
ソースコード
// O(|S|^2) BFS解 // ---------- begin chmin, chmax ---------- pub trait ChangeMinMax { fn chmin(&mut self, x: Self) -> bool; fn chmax(&mut self, x: Self) -> bool; } impl<T: PartialOrd> ChangeMinMax for T { fn chmin(&mut self, x: Self) -> bool { *self > x && { *self = x; true } } fn chmax(&mut self, x: Self) -> bool { *self < x && { *self = x; true } } } // ---------- end chmin, chmax ---------- // ---------- begin z_algorithm ---------- fn z_algorithm<T: Eq>(s: &[T]) -> Vec<usize> { let len = s.len(); let mut a = vec![0; len]; a[0] = len; let mut i = 1; let mut j = 0; while i < len { while i + j < len && s[j] == s[i + j] { j += 1; } a[i] = j; if j == 0 { i += 1; continue; } let mut k = 1; while i + k < len && k + a[k] < j { a[i + k] = a[k]; k += 1; } i += k; j -= k; } a } // ---------- end z_algorithm ---------- fn read() -> Vec<char> { let mut s = String::new(); std::io::stdin().read_line(&mut s).unwrap(); let s = s.trim().chars().collect::<Vec<_>>(); assert!(1 <= s.len() && s.len() <= 200000); assert!(s.iter().all(|s| "ab".contains(*s))); s } fn main() { let s = read(); let za = z_algorithm(&s); let inf = s.len() + 1; let mut map = std::collections::HashMap::new(); let mut q = std::collections::VecDeque::new(); q.push_back((0, 0)); map.insert((0, 0), 0); let mut ans = s.len(); while let Some((x, y)) = q.pop_front() { if x == s.len() { ans = map[&(x, y)]; break; } let d = map[&(x, y)] + 1; if map.entry((x + 1, y)).or_insert(inf).chmin(d) { q.push_back((x + 1, y)); } if x > y && map.entry((x, x)).or_insert(inf).chmin(d) { q.push_back((x, x)); } if za[x] >= y && map.entry((x + y, y)).or_insert(inf).chmin(d) { q.push_back((x + y, y)); } } println!("{}", ans); }