結果

問題 No.22 括弧の対応
ユーザー phspls
提出日時 2020-02-23 19:49:44
言語 Rust
(1.83.0 + proconio)
結果
AC  
実行時間 1 ms / 5,000 ms
コード長 1,214 bytes
コンパイル時間 11,092 ms
コンパイル使用メモリ 380,244 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-07-20 07:47:55
合計ジャッジ時間 11,979 ms
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 19
権限があれば一括ダウンロードができます

ソースコード

diff #

fn main() {
    let mut nk = String::new();
    std::io::stdin().read_line(&mut nk).ok();
    let k: usize = nk.trim().split_whitespace().skip(1).next().unwrap().parse().unwrap();
    let mut s = String::new();
    std::io::stdin().read_line(&mut s).ok();
    let s: Vec<char> = s.trim().chars().collect();

    match s[k-1] {
        '(' => {
            let mut count = 1;
            for pair in s.iter().skip(k).enumerate() {
                let c = pair.1;
                if *c == ')' {
                    count -= 1;
                } else {
                    count += 1;
                }
                if count == 0 {
                    println!("{}", k + pair.0 + 1);
                    return;
                }
            }
        },
        ')' => {
            let mut count = 1;
            for pair in s.iter().take(k-1).rev().enumerate() {
                let c = pair.1;
                if *c == ')' {
                    count += 1;
                } else {
                    count -= 1;
                }
                if count == 0 {
                    println!("{}", k - pair.0 - 1);
                    return;
                }
            }
        },
        _ => {}
    }
}
0