結果

問題 No.22 括弧の対応
ユーザー shimadandyshimadandy
提出日時 2016-10-03 17:42:05
言語 C#(csc)
(csc 3.9.0)
結果
AC  
実行時間 68 ms / 5,000 ms
コード長 1,798 bytes
コンパイル時間 2,387 ms
コンパイル使用メモリ 108,024 KB
実行使用メモリ 23,952 KB
最終ジャッジ日時 2023-09-27 13:08:53
合計ジャッジ時間 4,532 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 64 ms
23,864 KB
testcase_01 AC 63 ms
21,840 KB
testcase_02 AC 63 ms
21,820 KB
testcase_03 AC 68 ms
23,916 KB
testcase_04 AC 65 ms
21,860 KB
testcase_05 AC 65 ms
23,804 KB
testcase_06 AC 65 ms
23,868 KB
testcase_07 AC 66 ms
21,876 KB
testcase_08 AC 66 ms
21,900 KB
testcase_09 AC 66 ms
21,876 KB
testcase_10 AC 66 ms
21,932 KB
testcase_11 AC 65 ms
21,972 KB
testcase_12 AC 66 ms
21,876 KB
testcase_13 AC 66 ms
21,816 KB
testcase_14 AC 65 ms
21,828 KB
testcase_15 AC 65 ms
21,884 KB
testcase_16 AC 65 ms
21,936 KB
testcase_17 AC 64 ms
21,968 KB
testcase_18 AC 65 ms
23,952 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.Linq;
using System.Collections.Generic;

class Program
{

    /// <summary>
    /// プログラムのエントリポイント
    /// </summary>
    /// <param name="args"></param>
    static void Main(string[] args)
    {
        // 括弧の数と調査する括弧のインデックスを取得する
        var n = (from tmp in Console.ReadLine().Split(' ')
                 select int.Parse(tmp)).ToArray();

        // 一行分読み込む
        var s = Console.ReadLine();

        // 開きと閉じの対応をリスト化する
        var list = new List<Interaction>();
        for (int si = 0; si < s.Length; si++)
        {
            if (s[si] == '(')
            {
                list.Add(new Interaction() { OpenIndex = si });
            }
            else
            {
                for (int i = list.Count - 1; i >= 0; i--)
                {
                    if (list[i].CloseIndex < 0)
                    {
                        list[i].CloseIndex = si;
                        break;
                    }
                }
            }
        }

        // 結果出力
        if (s[n[1] - 1] == '(')
            Console.WriteLine(list.Where((intr) => intr.OpenIndex == n[1] - 1).ToArray()[0].CloseIndex + 1);
        else
            Console.WriteLine(list.Where((intr) => intr.CloseIndex == n[1] - 1).ToArray()[0].OpenIndex + 1);
    }
    

    /// <summary>
    /// 対応情報クラス
    /// </summary>
    public class Interaction
    {
        /// <summary>
        /// 開き括弧のインデックス
        /// </summary>
        public int OpenIndex { get; set; }
        /// <summary>
        /// 閉じ括弧のインデックス
        /// </summary>
        public int CloseIndex { get; set; } = -1;
    }
}
0