結果

問題 No.22 括弧の対応
ユーザー ken
提出日時 2018-07-14 16:20:32
言語 C#(csc)
(csc 3.9.0)
結果
AC  
実行時間 27 ms / 5,000 ms
コード長 1,423 bytes
コンパイル時間 2,385 ms
コンパイル使用メモリ 103,936 KB
実行使用メモリ 18,048 KB
最終ジャッジ日時 2024-07-20 07:32:42
合計ジャッジ時間 2,113 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 19
権限があれば一括ダウンロードができます
コンパイルメッセージ
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