結果

問題 No.2162 Copy and Paste 2
ユーザー akakimidoriakakimidori
提出日時 2021-12-21 12:39:49
言語 Rust
(1.77.0)
結果
AC  
実行時間 364 ms / 7,000 ms
コード長 2,308 bytes
コンパイル時間 2,050 ms
コンパイル使用メモリ 174,976 KB
実行使用メモリ 13,044 KB
最終ジャッジ日時 2024-04-24 13:01:44
合計ジャッジ時間 7,984 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 0 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 AC 76 ms
10,708 KB
testcase_07 AC 89 ms
11,728 KB
testcase_08 AC 158 ms
12,492 KB
testcase_09 AC 84 ms
10,444 KB
testcase_10 AC 85 ms
10,452 KB
testcase_11 AC 131 ms
11,984 KB
testcase_12 AC 120 ms
11,476 KB
testcase_13 AC 195 ms
12,496 KB
testcase_14 AC 101 ms
10,704 KB
testcase_15 AC 102 ms
10,564 KB
testcase_16 AC 103 ms
10,704 KB
testcase_17 AC 269 ms
12,240 KB
testcase_18 AC 324 ms
12,620 KB
testcase_19 AC 301 ms
12,624 KB
testcase_20 AC 279 ms
12,624 KB
testcase_21 AC 177 ms
12,748 KB
testcase_22 AC 179 ms
12,748 KB
testcase_23 AC 77 ms
11,596 KB
testcase_24 AC 353 ms
12,240 KB
testcase_25 AC 360 ms
12,244 KB
testcase_26 AC 364 ms
13,044 KB
testcase_27 AC 364 ms
12,240 KB
testcase_28 AC 297 ms
10,828 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 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![0i32; s.len() + 1];
    for i in 1..s.len() {
        let d = dp[i];
        dp[i + 1].chmax(d);
        let len = i as i32;
        let mut cnt = 0;
        let mut pos = i;
        while let Some(&x) = set.range(pos..).next() {
            cnt += 1;
            dp[x + i].chmax(d - 1 + cnt * (len - 1));
            pos = x + i;
        }
        while index.last().map_or(false, |p| p.0 <= i) {
            let (_, x) = index.pop().unwrap();
            set.remove(&x);
        }
    }
    let ans = s.len() as i32 - dp[s.len()];
    println!("{}", ans);
}
0