結果
| 問題 |
No.2162 Copy and Paste 2
|
| コンテスト | |
| ユーザー |
akakimidori
|
| 提出日時 | 2021-12-21 11:35:59 |
| 言語 | Rust (1.83.0 + proconio) |
| 結果 |
MLE
|
| 実行時間 | - |
| コード長 | 2,430 bytes |
| コンパイル時間 | 13,178 ms |
| コンパイル使用メモリ | 400,988 KB |
| 実行使用メモリ | 797,456 KB |
| 最終ジャッジ日時 | 2024-11-06 21:09:49 |
| 合計ジャッジ時間 | 22,432 ms |
|
ジャッジサーバーID (参考情報) |
judge5 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 5 MLE * 1 -- * 20 |
ソースコード
// 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 mut su = za.clone();
for i in (1..su.len()).rev() {
su[i - 1] = su[i - 1].max(su[i]);
}
let inf = s.len() + 1;
let mut map = std::collections::HashMap::new();
let mut q = std::collections::VecDeque::new();
q.push_back((0, 0, true));
map.insert((0, 0, true), 0);
let mut ans = s.len();
while let Some((x, y, used)) = q.pop_front() {
if x == s.len() {
ans = map[&(x, y, used)];
break;
}
let d = map[&(x, y, used)] + 1;
let y = if y > su[x] { 0 } else { y };
if map.entry((x + 1, y, used)).or_insert(inf).chmin(d) {
q.push_back((x + 1, y, used));
}
if x > y && used && su[x] >= x && map.entry((x, x, false)).or_insert(inf).chmin(d) {
q.push_back((x, x, false));
}
if za[x] >= y && map.entry((x + y, y, true)).or_insert(inf).chmin(d) {
q.push_back((x + y, y, true));
}
}
println!("{}", ans);
}
akakimidori