結果

問題 No.852 連続部分文字列
ユーザー phsplsphspls
提出日時 2022-12-06 16:59:09
言語 Rust
(1.77.0)
結果
AC  
実行時間 600 ms / 3,153 ms
コード長 1,083 bytes
コンパイル時間 9,219 ms
コンパイル使用メモリ 149,380 KB
実行使用メモリ 19,488 KB
最終ジャッジ日時 2024-04-21 07:58:54
合計ジャッジ時間 16,752 ms
ジャッジサーバーID
(参考情報)
judge2 / 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 2 ms
5,376 KB
testcase_04 AC 1 ms
5,376 KB
testcase_05 AC 1 ms
5,376 KB
testcase_06 AC 1 ms
5,376 KB
testcase_07 AC 1 ms
5,376 KB
testcase_08 AC 1 ms
5,376 KB
testcase_09 AC 1 ms
5,376 KB
testcase_10 AC 1 ms
5,376 KB
testcase_11 AC 1 ms
5,376 KB
testcase_12 AC 1 ms
5,376 KB
testcase_13 AC 9 ms
5,376 KB
testcase_14 AC 4 ms
5,376 KB
testcase_15 AC 15 ms
5,376 KB
testcase_16 AC 9 ms
5,376 KB
testcase_17 AC 12 ms
5,376 KB
testcase_18 AC 15 ms
5,376 KB
testcase_19 AC 2 ms
5,376 KB
testcase_20 AC 7 ms
5,376 KB
testcase_21 AC 15 ms
5,376 KB
testcase_22 AC 5 ms
5,376 KB
testcase_23 AC 11 ms
5,376 KB
testcase_24 AC 6 ms
5,376 KB
testcase_25 AC 10 ms
5,376 KB
testcase_26 AC 16 ms
5,376 KB
testcase_27 AC 9 ms
5,376 KB
testcase_28 AC 12 ms
5,376 KB
testcase_29 AC 16 ms
5,376 KB
testcase_30 AC 4 ms
5,376 KB
testcase_31 AC 16 ms
5,376 KB
testcase_32 AC 17 ms
5,376 KB
testcase_33 AC 571 ms
19,072 KB
testcase_34 AC 566 ms
19,200 KB
testcase_35 AC 566 ms
19,036 KB
testcase_36 AC 572 ms
19,328 KB
testcase_37 AC 570 ms
19,056 KB
testcase_38 AC 580 ms
19,200 KB
testcase_39 AC 569 ms
19,200 KB
testcase_40 AC 571 ms
19,044 KB
testcase_41 AC 570 ms
19,200 KB
testcase_42 AC 600 ms
19,488 KB
testcase_43 AC 490 ms
18,816 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