結果

問題 No.345 最小チワワ問題
ユーザー cra77756176
提出日時 2022-12-03 20:23:11
言語 Rust
(1.83.0 + proconio)
結果
AC  
実行時間 1 ms / 2,000 ms
コード長 620 bytes
コンパイル時間 13,929 ms
コンパイル使用メモリ 383,076 KB
実行使用メモリ 5,248 KB
最終ジャッジ日時 2024-10-11 00:01:39
合計ジャッジ時間 15,462 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 29
権限があれば一括ダウンロードができます

ソースコード

diff #

fn main() {
    let mut s = String::new();
    std::io::stdin().read_line(&mut s).ok();
    let s = s.trim();

    let mut counts = vec![];
    let mut n = 0;
    for c in s.chars() {
        if c == 'w' {
            n += 1;
        }
        counts.push(n);
    }

    let mut min: Option<usize> = None;
    for (i, c) in s.chars().enumerate() {
        if c == 'c' {
            if let Some(j) = counts.iter().position(|&n| n == counts[i] + 2) {
                min = min.map_or(Some(j - i), |n| Some(n.min(j - i)));
            }
        }
    }

    min.map_or_else(|| println!("-1"), |n| println!("{}", n + 1));
}
0