結果
問題 |
No.22 括弧の対応
|
ユーザー |
![]() |
提出日時 | 2016-10-08 15:59:39 |
言語 | C#(csc) (csc 3.9.0) |
結果 |
AC
|
実行時間 | 25 ms / 5,000 ms |
コード長 | 1,038 bytes |
コンパイル時間 | 786 ms |
コンパイル使用メモリ | 104,064 KB |
実行使用メモリ | 17,920 KB |
最終ジャッジ日時 | 2024-07-20 07:17:09 |
合計ジャッジ時間 | 1,941 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge1 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 19 |
コンパイルメッセージ
Microsoft (R) Visual C# Compiler version 3.9.0-6.21124.20 (db94f4cc) Copyright (C) Microsoft Corporation. All rights reserved.
ソースコード
using System; namespace Yukicoder { class Program { static void Main(string[] args) { var line = Console.ReadLine().Split(' '); int N = int.Parse(line[0]); //()の数 int K = int.Parse(line[1]); //対応した( or )の番号を探す var S = Console.ReadLine(); bool right = S[K - 1] == '(' ? true : false; //rightなら右方向に探索 char k = right ? '(' : ')'; //rightなら右方向に探索 int count = 0; for (int i = K - 1; i < N;) { if (S[i] == k) count++; else count--; if (count == 0) { Console.WriteLine(i + 1); return; } if (right) { i++; } else { i--; } } } } }