結果

問題 No.22 括弧の対応
ユーザー cra77756176
提出日時 2022-11-18 21:49:01
言語 Rust
(1.83.0 + proconio)
結果
AC  
実行時間 1 ms / 5,000 ms
コード長 844 bytes
コンパイル時間 12,642 ms
コンパイル使用メモリ 398,888 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-09-20 02:12:21
合計ジャッジ時間 13,436 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 19
権限があれば一括ダウンロードができます

ソースコード

diff #

use std::io::{self, BufRead};

fn main() {
    let input = io::stdin()
        .lock()
        .lines()
        .map(|l| {
            l.unwrap()
                .split(' ')
                .map(std::string::ToString::to_string)
                .collect::<Vec<_>>()
        })
        .collect::<Vec<_>>();

    let pos = input[0][1].parse::<usize>().unwrap() - 1;
    let chars = input[1][0].chars().enumerate().collect::<Vec<_>>();

    let part = if chars[pos].1 == '(' {
        chars.iter().skip(pos).collect::<Vec<_>>()
    } else {
        chars.iter().take(pos + 1).rev().collect::<Vec<_>>()
    };

    let mut diff = 0;
    for c in part {
        if c.1 == '(' {
            diff += 1;
        } else {
            diff -= 1;
        }
        if diff == 0 {
            println!("{}", c.0 + 1);
            break;
        }
    }
}
0