結果

問題 No.22 括弧の対応
ユーザー kenken
提出日時 2018-07-14 16:20:32
言語 C#(csc)
(csc 3.9.0)
結果
AC  
実行時間 60 ms / 5,000 ms
コード長 1,423 bytes
コンパイル時間 1,145 ms
コンパイル使用メモリ 65,184 KB
実行使用メモリ 22,860 KB
最終ジャッジ日時 2023-09-27 13:27:58
合計ジャッジ時間 3,223 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 58 ms
20,616 KB
testcase_01 AC 58 ms
20,664 KB
testcase_02 AC 58 ms
20,664 KB
testcase_03 AC 58 ms
20,688 KB
testcase_04 AC 59 ms
20,676 KB
testcase_05 AC 59 ms
22,792 KB
testcase_06 AC 59 ms
20,740 KB
testcase_07 AC 59 ms
20,692 KB
testcase_08 AC 60 ms
20,684 KB
testcase_09 AC 60 ms
20,820 KB
testcase_10 AC 60 ms
22,860 KB
testcase_11 AC 60 ms
22,784 KB
testcase_12 AC 60 ms
20,664 KB
testcase_13 AC 60 ms
20,664 KB
testcase_14 AC 60 ms
20,612 KB
testcase_15 AC 60 ms
22,708 KB
testcase_16 AC 60 ms
20,664 KB
testcase_17 AC 58 ms
20,700 KB
testcase_18 AC 59 ms
20,832 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

using System;
using System.Collections.Generic;

public class Brackets{
    private static void WritePairIndex(List<char> s, int index, char dir){
        if(dir == '('){
            FindRight(s, index+1, 1);
        }else if(dir == ')'){
            FindLeft(s, index-1, 1);
        }
    }
    private static void FindRight(List<char> s, int index, int cnt){
        if(s[index] == '('){
            FindRight(s, index+1, cnt+1);
        }else if(s[index] == ')'){
            if(cnt == 1){
                Console.WriteLine(index+1);
            }else{
                FindRight(s, index+1, cnt-1);
            }
        }
    }
    public static void FindLeft(List<char> s, int index, int cnt){
        if(s[index] == ')'){
            FindLeft(s, index-1, cnt+1);
        }else if(s[index] == '('){
            if(cnt == 1){
                Console.WriteLine(index+1);
            }else{
                FindLeft(s, index-1, cnt-1);
            }
        }
    }
    public static void Main(){
        var input = Console.ReadLine();
        var inputStr = input.Split(' ');
        //var n = int.Parse(inputStr[0]);
        var k = int.Parse(inputStr[1]) - 1;
        input = Console.ReadLine();
        var bracketsArr = input.ToCharArray();
        var bracketsList = new List<char>(bracketsArr);
        var direction = bracketsList[k];
        
        WritePairIndex(bracketsList, k, direction);
        
    }
}
0