結果

問題 No.22 括弧の対応
ユーザー wowilsonwowilson
提出日時 2016-10-08 15:59:39
言語 C#(csc)
(csc 3.9.0)
結果
AC  
実行時間 54 ms / 5,000 ms
コード長 1,038 bytes
コンパイル時間 1,990 ms
コンパイル使用メモリ 100,064 KB
実行使用メモリ 22,832 KB
最終ジャッジ日時 2023-09-27 13:09:19
合計ジャッジ時間 3,632 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

using System;

namespace Yukicoder
{
    class Program
    {
        static void Main(string[] args)
        {
            var line = Console.ReadLine().Split(' ');

            int N = int.Parse(line[0]); //()の数
            int K = int.Parse(line[1]); //対応した( or )の番号を探す

            var S = Console.ReadLine();

            bool right = S[K - 1] == '(' ? true : false; //rightなら右方向に探索
            char k = right ? '(' : ')'; //rightなら右方向に探索

            int count = 0;
            for (int i = K - 1; i < N;)
            {
                if (S[i] == k)
                    count++;
                else
                    count--;

                if (count == 0)
                {
                    Console.WriteLine(i + 1);
                    return;
                }

                if (right)
                {
                    i++;
                }
                else
                {
                    i--;
                }
            }
        }

    }
}
0