結果

問題 No.22 括弧の対応
ユーザー aketijyuuzouaketijyuuzou
提出日時 2024-10-10 21:35:01
言語 C#(csc)
(csc 3.9.0)
結果
AC  
実行時間 31 ms / 5,000 ms
コード長 1,791 bytes
コンパイル時間 1,060 ms
コンパイル使用メモリ 115,052 KB
実行使用メモリ 27,120 KB
最終ジャッジ日時 2024-10-10 21:35:04
合計ジャッジ時間 2,651 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 29 ms
25,116 KB
testcase_01 AC 29 ms
25,336 KB
testcase_02 AC 29 ms
25,336 KB
testcase_03 AC 30 ms
25,208 KB
testcase_04 AC 29 ms
25,080 KB
testcase_05 AC 29 ms
25,156 KB
testcase_06 AC 31 ms
27,120 KB
testcase_07 AC 30 ms
25,084 KB
testcase_08 AC 29 ms
25,140 KB
testcase_09 AC 30 ms
27,116 KB
testcase_10 AC 29 ms
25,084 KB
testcase_11 AC 29 ms
25,336 KB
testcase_12 AC 29 ms
25,028 KB
testcase_13 AC 29 ms
24,952 KB
testcase_14 AC 29 ms
25,204 KB
testcase_15 AC 29 ms
23,220 KB
testcase_16 AC 29 ms
25,180 KB
testcase_17 AC 30 ms
25,080 KB
testcase_18 AC 29 ms
25,208 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