結果

問題 No.22 括弧の対応
ユーザー しらゆきしらゆき
提出日時 2015-11-19 10:05:45
言語 C#(csc)
(csc 3.9.0)
結果
AC  
実行時間 59 ms / 5,000 ms
コード長 1,119 bytes
コンパイル時間 1,997 ms
コンパイル使用メモリ 103,832 KB
実行使用メモリ 22,940 KB
最終ジャッジ日時 2023-09-27 12:58:21
合計ジャッジ時間 4,047 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 57 ms
22,940 KB
testcase_01 AC 57 ms
20,716 KB
testcase_02 AC 56 ms
20,652 KB
testcase_03 AC 57 ms
18,696 KB
testcase_04 AC 58 ms
22,812 KB
testcase_05 AC 58 ms
20,780 KB
testcase_06 AC 57 ms
20,852 KB
testcase_07 AC 58 ms
18,680 KB
testcase_08 AC 58 ms
20,644 KB
testcase_09 AC 59 ms
20,796 KB
testcase_10 AC 58 ms
22,628 KB
testcase_11 AC 58 ms
20,800 KB
testcase_12 AC 58 ms
20,844 KB
testcase_13 AC 59 ms
20,820 KB
testcase_14 AC 59 ms
20,772 KB
testcase_15 AC 58 ms
20,788 KB
testcase_16 AC 58 ms
20,720 KB
testcase_17 AC 58 ms
20,632 KB
testcase_18 AC 57 ms
18,660 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
Microsoft (R) Visual C# Compiler version 3.9.0-6.21124.20 (db94f4cc)
Copyright (C) Microsoft Corporation. All rights reserved.

ソースコード

diff #

using System;

class Program
{
    static void Main()
    {
        string[] s = Console.ReadLine().Split();
        int n = int.Parse(s[0]);
        int k = int.Parse(s[1]);
        char[] c = Console.ReadLine().ToCharArray();

        int a = 0;
        int b = 0;
        int ans = 0;

        if (c[k - 1] == '(')
        {
            a++;
            for (int i = k; ; i++)
            {
                if (c[i] == ')')
                {
                    b++;
                    if (a == b)
                    {
                        ans = k + 2 * a - 1;
                        break;
                    }
                }
                else a++;
            }
        }
        else
        {
            b++;
            for (int i = k - 2; ; i--)
            {
                if (c[i] == '(')
                {
                    a++;
                    if (a == b)
                    {
                        ans = k - 2 * a + 1;
                        break;
                    }
                }
                else b++;
            }
        }

        Console.WriteLine(ans);
    }
}
0