結果

問題 No.22 括弧の対応
ユーザー 明智重蔵明智重蔵
提出日時 2015-10-31 18:10:57
言語 C#(csc)
(csc 3.9.0)
結果
AC  
実行時間 66 ms / 5,000 ms
コード長 1,750 bytes
コンパイル時間 2,823 ms
コンパイル使用メモリ 107,544 KB
実行使用メモリ 23,940 KB
最終ジャッジ日時 2023-09-27 12:57:53
合計ジャッジ時間 4,998 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 62 ms
23,836 KB
testcase_01 AC 62 ms
23,828 KB
testcase_02 AC 63 ms
21,872 KB
testcase_03 AC 63 ms
19,808 KB
testcase_04 AC 64 ms
21,780 KB
testcase_05 AC 64 ms
19,720 KB
testcase_06 AC 64 ms
21,824 KB
testcase_07 AC 66 ms
21,776 KB
testcase_08 AC 64 ms
23,808 KB
testcase_09 AC 65 ms
21,848 KB
testcase_10 AC 65 ms
23,940 KB
testcase_11 AC 64 ms
21,884 KB
testcase_12 AC 65 ms
23,848 KB
testcase_13 AC 65 ms
21,888 KB
testcase_14 AC 64 ms
21,912 KB
testcase_15 AC 64 ms
21,892 KB
testcase_16 AC 64 ms
23,872 KB
testcase_17 AC 62 ms
21,820 KB
testcase_18 AC 63 ms
23,792 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;
using System.Linq;

class Program
{
    static string InputPattern = "InputX";

    static List<string> GetInputList()
    {
        var WillReturn = new List<string>();

        if (InputPattern == "Input1") {
            WillReturn.Add("4 4");
            WillReturn.Add("(())");
            //1
            //(())の文字の4番目は)である、このとき対応する文字は1番目の(である
        }
        else if (InputPattern == "Input2") {
            WillReturn.Add("12 2");
            WillReturn.Add("(((())()())) ");
            //11
            //(((())()()))という文字列の2番目の文字は(である。
            //このとき対応する文字は11番目の)である。
        }
        else if (InputPattern == "Input3") {
            WillReturn.Add("20 5");
            WillReturn.Add("((((((()))))))(()())");
            //10
        }
        else {
            string wkStr;
            while ((wkStr = Console.ReadLine()) != null) WillReturn.Add(wkStr);
        }
        return WillReturn;
    }

    static void Main()
    {
        List<string> InputList = GetInputList();
        int K = InputList[0].Split(' ').Select(X => int.Parse(X)).Last();
        string S = InputList[1];

        //開き括弧をスタックにPushし、
        //閉じ括弧ならPopすれば、対応が分かる
        var stk = new Stack<int>();

        for (int I = 0; I <= S.Length - 1; I++) {
            if (S[I] == '(') stk.Push(I);
            if (S[I] == ')') {
                int wkPair = stk.Pop();
                if (K - 1 == I) Console.WriteLine(wkPair + 1);
                if (K - 1 == wkPair) Console.WriteLine(I + 1);
            }
        }
    }
}
0