結果

問題 No.22 括弧の対応
コンテスト
ユーザー Adamantite
提出日時 2023-06-28 00:16:12
言語 C#
(.NET 10.0.201)
コンパイル:
dotnet_c
実行:
/usr/bin/dotnet_wrap
結果
AC  
実行時間 53 ms / 5,000 ms
コード長 1,125 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 8,091 ms
コンパイル使用メモリ 171,540 KB
実行使用メモリ 192,532 KB
最終ジャッジ日時 2026-03-26 00:17:03
合計ジャッジ時間 10,601 ms
ジャッジサーバーID
(参考情報)
judge3_1 / judge1_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 19
権限があれば一括ダウンロードができます
コンパイルメッセージ
  復元対象のプロジェクトを決定しています...
  /home/judge/data/code/main.csproj を復元しました (95 ミリ秒)。
  main -> /home/judge/data/code/bin/Release/net10.0/main.dll
  main -> /home/judge/data/code/bin/Release/net10.0/publish/

ソースコード

diff #
raw source code

using System;
using System.Collections.Generic;

namespace No22_ParenthesesCorrespondence
{
    internal class Program
    {
        static void Main(string[] args) {
            string[] param = Console.ReadLine().Split(' ');
            int n = int.Parse(param[0]);
            int k = int.Parse(param[1]);
            string s = Console.ReadLine();

            Stack<char> stack = new Stack<char>();
            int count = 0;
            char countChar = ' ';
            int inc = 0;
            if (s[k - 1] == '(') {
                countChar = '(';
                inc = 1;
            } else {
                countChar = ')';
                inc = -1;
            }
            for (int i = k - 1; i < n; i += inc) {
                stack.Push(s[i]);
                if (s[i] == countChar) {
                    count++;
                } else {
                    while (stack.Pop() != countChar) { }
                    count--;
                }
                if (stack.Count == 0) {
                    Console.WriteLine(i + 1);
                    return;
                }
            }
        }
    }
}
0