結果
| 問題 | No.22 括弧の対応 |
| コンテスト | |
| ユーザー |
htensai
|
| 提出日時 | 2020-02-13 18:57:05 |
| 言語 | Java (openjdk 26.0.2.1) |
| 結果 |
AC
|
| 実行時間 | 78 ms / 5,000 ms |
| + 775µs | |
| コード長 | 954 bytes |
| 記録 | |
| コンパイル時間 | 1,718 ms |
| コンパイル使用メモリ | 84,064 KB |
| 実行使用メモリ | 44,812 KB |
| 最終ジャッジ日時 | 2026-08-19 18:45:43 |
| 合計ジャッジ時間 | 4,369 ms |
|
ジャッジサーバーID (参考情報) |
judge3_1 / judge2_0 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 19 |
ソースコード
import java.util.*;
public class Main {
public static void main (String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int k = sc.nextInt();
char[] arr = sc.next().toCharArray();
int x = -1;
int current = 0;
if (arr[k - 1] == '(') {
for (int i = 0; i < n; i++) {
if (i + 1 == k) {
x = current;
}
if (arr[i] == '(') {
current++;
} else {
current--;
}
if (current == x) {
System.out.println(i + 1);
return;
}
}
} else {
for (int i = n - 1; i >= 0; i--) {
if (i + 1 == k) {
x = current;
}
if (arr[i] == ')') {
current++;
} else {
current--;
}
if (current == x) {
System.out.println(i + 1);
return;
}
}
}
}
}
htensai