結果

問題 No.22 括弧の対応
ユーザー tsushimatsushima
提出日時 2020-02-07 21:30:57
言語 C#(csc)
(csc 3.9.0)
結果
AC  
実行時間 59 ms / 5,000 ms
コード長 1,008 bytes
コンパイル時間 1,772 ms
コンパイル使用メモリ 69,500 KB
実行使用メモリ 23,960 KB
最終ジャッジ日時 2023-09-27 13:45:05
合計ジャッジ時間 3,284 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 53 ms
19,944 KB
testcase_01 AC 57 ms
23,920 KB
testcase_02 AC 53 ms
23,736 KB
testcase_03 AC 57 ms
21,980 KB
testcase_04 AC 57 ms
21,828 KB
testcase_05 AC 56 ms
21,972 KB
testcase_06 AC 56 ms
21,828 KB
testcase_07 AC 59 ms
23,884 KB
testcase_08 AC 58 ms
21,824 KB
testcase_09 AC 56 ms
22,000 KB
testcase_10 AC 57 ms
22,016 KB
testcase_11 AC 58 ms
21,884 KB
testcase_12 AC 54 ms
21,836 KB
testcase_13 AC 53 ms
23,960 KB
testcase_14 AC 57 ms
21,876 KB
testcase_15 AC 54 ms
19,704 KB
testcase_16 AC 55 ms
21,772 KB
testcase_17 AC 56 ms
22,004 KB
testcase_18 AC 56 ms
21,920 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

using System;
using System.Linq;
using System.Collections.Generic;

namespace Algorithm
{
    class Program
    {
        static void Main(string[] args)
        {
            var l = Console.ReadLine().Split().Select(int.Parse).ToArray();
            int N = l[0], K = l[1];
            var S = Console.ReadLine();

            var stack = new Stack<int>();
            var right = S[K - 1] == '(' ? true : false;
            var ans = 0;
            var count = 0;
            for (var i = 0; i < S.Length; i++)
            {
                if (S[i] == '(')
                    stack.Push(i + 1);
                else
                    count = stack.Pop();

                if (right && count == K)
                {
                    ans = i + 1;
                    break;
                }

                if (!right && i + 1 == K)
                {
                    ans = count;
                    break;
                }
            }

            Console.WriteLine(ans);
        }
    }
}
0