結果
問題 |
No.22 括弧の対応
|
ユーザー |
![]() |
提出日時 | 2019-03-12 17:32:44 |
言語 | Rust (1.83.0 + proconio) |
結果 |
WA
|
実行時間 | - |
コード長 | 1,797 bytes |
コンパイル時間 | 11,974 ms |
コンパイル使用メモリ | 390,660 KB |
実行使用メモリ | 6,944 KB |
最終ジャッジ日時 | 2024-06-23 15:47:19 |
合計ジャッジ時間 | 13,314 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge4 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 14 WA * 5 |
ソースコード
fn main() { let vec: Vec<usize> = read_vec(); let index = vec[1]; let text: String = read(); let mut chars: Vec<char> = text.chars().collect(); let mut ans = hoge(index, chars.clone()); if ans == 0 { chars.reverse(); ans = vec[0] - hoge_rev(vec[0] - index + 1, chars) + 1; } println!("{}", ans); } fn hoge(index: usize, chars: Vec<char>) -> usize { let mut counter = 0; for i in 0..index { match chars[i] { '(' => { counter += 1; }, ')' => { counter -= 1; }, _ => {}, } } let registor = counter; for i in index..chars.len() { match chars[i] { '(' => { counter += 1; }, ')' => { counter -= 1; }, _ => {}, } if counter == registor - 1 { return i + 1; } } return 0; } fn hoge_rev(index: usize, chars: Vec<char>) -> usize { let mut counter = 0; for i in 0..index { match chars[i] { ')' => { counter += 1; }, '(' => { counter -= 1; }, _ => {}, } } let registor = counter; for i in index..chars.len() { match chars[i] { ')' => { counter += 1; }, '(' => { counter -= 1; }, _ => {}, } if counter == registor - 1 { return i + 1; } } return 0; } 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() }