結果

問題 No.22 括弧の対応
ユーザー huffman56huffman56
提出日時 2022-04-17 11:42:11
言語 C#(csc)
(csc 3.9.0)
結果
AC  
実行時間 47 ms / 5,000 ms
コード長 1,538 bytes
コンパイル時間 3,863 ms
コンパイル使用メモリ 106,368 KB
実行使用メモリ 18,944 KB
最終ジャッジ日時 2024-12-26 05:53:11
合計ジャッジ時間 4,443 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 19
権限があれば一括ダウンロードができます
コンパイルメッセージ
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.IO;
using System.Linq;
using System.Collections;
using System.Collections.Generic;
using System.Numerics;
using System.Diagnostics;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using static System.Math;
using static System.Console;

namespace yukicoder
{
    class Program
    {
        static void Main()
        {
            int[] nums = Console.ReadLine().Split(' ').Select(n => int.Parse(n)).ToArray();
            int N = nums[0];
            int K = nums[1];
            string S = Console.ReadLine();
            int[] chk = new int[N];

            for (int i = 0; i < N; i++)
            {
                // 括弧の個数を管理する変数
                int kakko = 1;

                for (int j = i+1; j < N; j++)
                {
                    // すでに対応する数値が分かっている場合は処理をスキップ
                    if (chk[j] != 0)
                    {
                        break;
                    }

                    if (')'.Equals(S[j]))
                    {
                        kakko--;
                    }
                    else
                    {
                        kakko++;
                    }

                    if (kakko == 0)
                    {
                        chk[i] = j+1;
                        chk[j] = i+1;

                        break;
                    }
                }
            }

            Console.WriteLine(chk[K-1]);

        }
    }
}
0