結果

問題 No.22 括弧の対応
ユーザー ta60143ta60143
提出日時 2019-03-13 10:00:59
言語 Rust
(1.72.1)
結果
AC  
実行時間 1 ms / 5,000 ms
コード長 1,953 bytes
コンパイル時間 1,833 ms
コンパイル使用メモリ 145,752 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-09-27 13:35:12
合計ジャッジ時間 1,643 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ(β)

テストケース

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

ソースコード

diff #

fn main() {
    let vec: Vec<usize> = read_vec();
    let (len, index_1org) = (vec[0], vec[1]);    
    let chars: Vec<char> = read::<String>().chars().collect();
    let mut ans_1org: usize = 0;
    
    match chars[index_1org - 1] {
        '(' => {
            let mut prev_char: char = '(';
            let mut indent_lv = 0u32;

            for i_0org in index_1org..len {
                match (chars[i_0org], prev_char) {
                    ( '(', '(' ) => {
                        indent_lv += 1;
                    },
                    ( ')', ')') => {
                        indent_lv -= 1;
                    },
                    _ => {}
                }

                prev_char = chars[i_0org];

                if indent_lv == 0 {
                    ans_1org = i_0org + 1;
                    break;
                }
            }
        },
        ')' => {
            let mut prev_char: char = ')';
            let mut indent_lv = 0u32;

            for i_0org in (0..index_1org-1).rev() {

                match (chars[i_0org], prev_char) {
                    ( '(', '(' ) => {
                        indent_lv -= 1;
                    },
                    ( ')', ')') => {
                        indent_lv += 1;
                    },
                    _ => {}
                }

                prev_char = chars[i_0org];

                if indent_lv == 0 {
                    ans_1org = i_0org + 1;
                    break;
                }
            }
        },

        _ => { println!("error"); }
    }

    println!("{}", ans_1org);
}

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()
}
0