結果

問題 No.22 括弧の対応
ユーザー Adamantite
提出日時 2023-06-28 00:20:10
言語 C#
(.NET 8.0.404)
結果
AC  
実行時間 56 ms / 5,000 ms
コード長 1,450 bytes
コンパイル時間 9,285 ms
コンパイル使用メモリ 165,860 KB
実行使用メモリ 184,288 KB
最終ジャッジ日時 2024-07-04 16:34:03
合計ジャッジ時間 10,078 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 19
権限があれば一括ダウンロードができます
コンパイルメッセージ
  復元対象のプロジェクトを決定しています...
  /home/judge/data/code/main.csproj を復元しました (98 ms)。
MSBuild のバージョン 17.9.6+a4ecab324 (.NET)
  main -> /home/judge/data/code/bin/Release/net8.0/main.dll
  main -> /home/judge/data/code/bin/Release/net8.0/publish/

ソースコード

diff #

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;
            if (s[k - 1] == '(') {
                for (int i = k - 1; i < n; i++) {
                    stack.Push(s[i]);
                    if (s[i] == '(') {
                        count++;
                    } else {
                        while (stack.Pop() != '(') { }
                        count--;
                    }
                    if (stack.Count == 0) {
                        Console.WriteLine(i + 1);
                        return;
                    }
                }
            } else {
                for (int i = k - 1; -1 < i; i--) {
                    stack.Push(s[i]);
                    if (s[i] == ')') {
                        count++;
                    } else {
                        while (stack.Pop() != ')') { }
                        count--;
                    }
                    if (stack.Count == 0) {
                        Console.WriteLine(i + 1);
                        return;
                    }
                }
            }
        }
    }
}
0