結果

問題 No.852 連続部分文字列
ユーザー phsplsphspls
提出日時 2022-12-06 16:59:09
言語 Rust
(1.72.1)
結果
AC  
実行時間 517 ms / 3,153 ms
コード長 1,083 bytes
コンパイル時間 2,072 ms
コンパイル使用メモリ 143,084 KB
実行使用メモリ 19,624 KB
最終ジャッジ日時 2023-08-03 09:18:10
合計ジャッジ時間 11,239 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 1 ms
4,384 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 1 ms
4,380 KB
testcase_06 AC 1 ms
4,380 KB
testcase_07 AC 1 ms
4,380 KB
testcase_08 AC 1 ms
4,380 KB
testcase_09 AC 1 ms
4,376 KB
testcase_10 AC 1 ms
4,380 KB
testcase_11 AC 1 ms
4,376 KB
testcase_12 AC 1 ms
4,380 KB
testcase_13 AC 7 ms
4,376 KB
testcase_14 AC 4 ms
4,376 KB
testcase_15 AC 12 ms
4,384 KB
testcase_16 AC 8 ms
4,380 KB
testcase_17 AC 11 ms
4,376 KB
testcase_18 AC 13 ms
4,376 KB
testcase_19 AC 1 ms
4,380 KB
testcase_20 AC 6 ms
4,376 KB
testcase_21 AC 13 ms
4,380 KB
testcase_22 AC 5 ms
4,380 KB
testcase_23 AC 9 ms
4,376 KB
testcase_24 AC 6 ms
4,376 KB
testcase_25 AC 10 ms
4,380 KB
testcase_26 AC 13 ms
4,376 KB
testcase_27 AC 9 ms
4,380 KB
testcase_28 AC 11 ms
4,380 KB
testcase_29 AC 14 ms
4,380 KB
testcase_30 AC 3 ms
4,380 KB
testcase_31 AC 14 ms
4,380 KB
testcase_32 AC 14 ms
4,376 KB
testcase_33 AC 488 ms
19,188 KB
testcase_34 AC 484 ms
19,468 KB
testcase_35 AC 493 ms
19,156 KB
testcase_36 AC 494 ms
19,332 KB
testcase_37 AC 493 ms
19,228 KB
testcase_38 AC 495 ms
19,384 KB
testcase_39 AC 498 ms
19,156 KB
testcase_40 AC 493 ms
19,220 KB
testcase_41 AC 492 ms
19,340 KB
testcase_42 AC 517 ms
19,624 KB
testcase_43 AC 432 ms
18,672 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

fn lower_bound(val: usize, target: &Vec<usize>) -> usize {
    let mut lower = 0usize;
    let mut upper = target.len();
    while upper > lower {
        let middle = (upper + lower) / 2;
        if val <= target[middle] {
            upper = middle;
        } else {
            lower = middle+1;
        }
    }
    upper
}

fn main() {
    let mut s = String::new();
    std::io::stdin().read_line(&mut s).ok();
    let s = s.trim().chars().map(|c| c as usize - 'a' as usize).collect::<Vec<_>>();

    let n = s.len();
    let total_pattern = n * (n + 1) / 2;
    let mut cnt = 0usize;
    let mut patterns = vec![vec![]; 26];
    for i in 0..n {
        patterns[s[i]].push(i);
    }
    for i in 0..26 {
        for j in 0..n {
            if s[j] == i {
                cnt += n - j;
                continue;
            }
            let lidx = lower_bound(j, &patterns[i]);
            if lidx == patterns[i].len() { continue; }
            let lidx = patterns[i][lidx];
            cnt += n - lidx;
        }
    }
    println!("{}", cnt as f64 / total_pattern as f64);
}
0