結果

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

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 50 ms
20,796 KB
testcase_01 AC 51 ms
20,720 KB
testcase_02 AC 50 ms
20,816 KB
testcase_03 AC 51 ms
20,924 KB
testcase_04 AC 51 ms
22,768 KB
testcase_05 AC 50 ms
20,656 KB
testcase_06 AC 53 ms
20,912 KB
testcase_07 AC 53 ms
20,792 KB
testcase_08 AC 53 ms
20,836 KB
testcase_09 AC 51 ms
22,768 KB
testcase_10 AC 53 ms
20,712 KB
testcase_11 AC 52 ms
22,860 KB
testcase_12 AC 54 ms
20,752 KB
testcase_13 AC 53 ms
20,712 KB
testcase_14 AC 52 ms
20,808 KB
testcase_15 AC 52 ms
18,748 KB
testcase_16 AC 52 ms
20,712 KB
testcase_17 AC 50 ms
20,816 KB
testcase_18 AC 50 ms
20,728 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
Microsoft (R) Visual C# Compiler version 3.9.0-6.21124.20 (db94f4cc)
Copyright (C) Microsoft Corporation. All rights reserved.

ソースコード

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