結果

問題 No.22 括弧の対応
ユーザー iwotiwot
提出日時 2019-12-16 14:35:37
言語 Rust
(1.77.0)
結果
AC  
実行時間 1 ms / 5,000 ms
コード長 963 bytes
コンパイル時間 843 ms
コンパイル使用メモリ 143,260 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-09-27 13:44:04
合計ジャッジ時間 1,773 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 1 ms
4,376 KB
testcase_06 AC 1 ms
4,376 KB
testcase_07 AC 1 ms
4,376 KB
testcase_08 AC 1 ms
4,376 KB
testcase_09 AC 1 ms
4,376 KB
testcase_10 AC 1 ms
4,380 KB
testcase_11 AC 1 ms
4,380 KB
testcase_12 AC 1 ms
4,376 KB
testcase_13 AC 1 ms
4,380 KB
testcase_14 AC 1 ms
4,376 KB
testcase_15 AC 1 ms
4,380 KB
testcase_16 AC 1 ms
4,380 KB
testcase_17 AC 1 ms
4,376 KB
testcase_18 AC 1 ms
4,384 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

fn read<T: std::str::FromStr>() -> T {
    let mut s = String::new();
    std::io::stdin().read_line(&mut s).ok();
    s.trim().parse().ok().unwrap()
}

fn read_vec<T: std::str::FromStr>() -> Vec<T> {
    let mut s = String::new();
    std::io::stdin().read_line(&mut s).ok();
    s.trim()
        .split_whitespace()
        .map(|e| e.parse().ok().unwrap())
        .collect()
}

fn main() {
  let input1: Vec<usize> = read_vec();
  let input2: String = read();
  let mut pairs: Vec<Option<usize>> = vec![None; input1[0]];

  use std::collections::VecDeque;
  let mut opend_at_queue = VecDeque::new();

  let chars = input2.chars().collect::<Vec<char>>();
  for i in 0..chars.len() {
    if chars[i] == '(' {
      opend_at_queue.push_back(i)
    } else {
      let opend_at = opend_at_queue.pop_back().unwrap();
      pairs[opend_at] = Some(i);
      pairs[i] = Some(opend_at);
    }
  }
  
  let ans = pairs[input1[1]-1].unwrap() + 1;
  println!("{}", ans)
}
0