結果
問題 | No.22 括弧の対応 |
ユーザー |
![]() |
提出日時 | 2017-04-23 13:51:40 |
言語 | Java (openjdk 23) |
結果 |
AC
|
実行時間 | 89 ms / 5,000 ms |
コード長 | 1,398 bytes |
コンパイル時間 | 2,792 ms |
コンパイル使用メモリ | 80,396 KB |
実行使用メモリ | 39,396 KB |
最終ジャッジ日時 | 2024-07-20 07:22:55 |
合計ジャッジ時間 | 4,749 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge4 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 19 |
ソースコード
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.Stack; public class Main { public static void main(String[] args) throws IOException { BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); String[] firstLine = reader.readLine().split(" "); int N = Integer.parseInt(firstLine[0]); int K = Integer.parseInt(firstLine[1]); String[] S = reader.readLine().split(""); Stack<Parenthesis> stack = new Stack<>(); for (int i = 0; i < N; i++) { if (S[i].equals("(")) { stack.add(new Parenthesis(S[i], i + 1)); } else { Parenthesis parenthesis = stack.pop(); if (i + 1 == K) { System.out.println(parenthesis.index); break; } else if (parenthesis.index == K) { System.out.println(i + 1); break; } } } } } class Parenthesis { String value; int index; Parenthesis(String value, int index) { this.value = value; this.index = index; } @Override public String toString() { return "Parenthesis{" + "value='" + value + '\'' + ", index=" + index + '}'; } }