結果

問題 No.2162 Copy and Paste 2
ユーザー akakimidoriakakimidori
提出日時 2021-12-21 11:15:32
言語 Rust
(1.77.0)
結果
MLE  
実行時間 -
コード長 2,242 bytes
コンパイル時間 2,643 ms
コンパイル使用メモリ 171,476 KB
実行使用メモリ 822,712 KB
最終ジャッジ日時 2024-04-24 13:01:02
合計ジャッジ時間 12,710 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
5,248 KB
testcase_01 AC 1 ms
5,376 KB
testcase_02 AC 1 ms
5,376 KB
testcase_03 AC 1 ms
5,376 KB
testcase_04 AC 1 ms
5,376 KB
testcase_05 AC 1 ms
5,376 KB
testcase_06 MLE -
testcase_07 -- -
testcase_08 -- -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

// O(|S|log|S|)

// ---------- 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, 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;
        if map.entry((x + 1, y, used)).or_insert(inf).chmin(d) {
            q.push_back((x + 1, y, used));
        }
        if x > y && used && 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);
}
0