結果

問題 No.2162 Copy and Paste 2
ユーザー akakimidoriakakimidori
提出日時 2021-12-21 12:03:50
言語 Rust
(1.77.0)
結果
AC  
実行時間 417 ms / 7,000 ms
コード長 2,272 bytes
コンパイル時間 1,493 ms
コンパイル使用メモリ 164,912 KB
実行使用メモリ 13,108 KB
最終ジャッジ日時 2023-08-06 18:11:56
合計ジャッジ時間 7,864 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 1 ms
4,380 KB
testcase_06 AC 73 ms
10,876 KB
testcase_07 AC 86 ms
11,692 KB
testcase_08 AC 162 ms
12,776 KB
testcase_09 AC 93 ms
10,720 KB
testcase_10 AC 90 ms
10,624 KB
testcase_11 AC 141 ms
12,204 KB
testcase_12 AC 135 ms
11,648 KB
testcase_13 AC 220 ms
12,828 KB
testcase_14 AC 114 ms
10,844 KB
testcase_15 AC 115 ms
10,736 KB
testcase_16 AC 116 ms
10,824 KB
testcase_17 AC 285 ms
12,920 KB
testcase_18 AC 364 ms
12,920 KB
testcase_19 AC 326 ms
12,912 KB
testcase_20 AC 315 ms
12,908 KB
testcase_21 AC 193 ms
12,880 KB
testcase_22 AC 188 ms
12,872 KB
testcase_23 AC 75 ms
12,884 KB
testcase_24 AC 416 ms
12,884 KB
testcase_25 AC 406 ms
12,936 KB
testcase_26 AC 410 ms
13,108 KB
testcase_27 AC 417 ms
12,884 KB
testcase_28 AC 336 ms
10,876 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

// O(|S|(log|S|)^2)

// ---------- 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 set = std::collections::BTreeSet::new();
    for i in 1..s.len() {
        if za[i] >= 1 {
            set.insert(i);
        }
    }
    let mut index = za.iter().enumerate().map(|p| (*p.1, p.0)).collect::<Vec<_>>();
    index.sort_by_key(|p| !p.0);
    let mut dp = vec![inf; s.len() + 1];
    dp[0] = 0;
    dp[1] = 1;
    for i in 1..s.len() {
        let d = dp[i];
        dp[i + 1].chmin(d + 1);
        let mut cnt = 0;
        let mut pos = i;
        while let Some(&x) = set.range(pos..).next() {
            cnt += 1;
            dp[x + i].chmin(d + x - cnt * i + cnt + 1);
            pos = x + i;
        }
        while index.last().map_or(false, |p| p.0 <= i) {
            let (_, x) = index.pop().unwrap();
            set.remove(&x);
        }
    }
    println!("{}", dp[s.len()]);
}
0