結果

問題 No.22 括弧の対応
ユーザー tenten
提出日時 2020-11-05 18:46:07
言語 Java
(openjdk 23)
結果
AC  
実行時間 167 ms / 5,000 ms
コード長 783 bytes
コンパイル時間 2,538 ms
コンパイル使用メモリ 74,512 KB
実行使用メモリ 41,776 KB
最終ジャッジ日時 2024-07-22 11:16:02
合計ジャッジ時間 6,573 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 19
権限があれば一括ダウンロードができます

ソースコード

diff #

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 count = 0;
		if (arr[k - 1] == '(') {
		    for (int i = k - 1; ; i++) {
		        if (arr[i] == '(') {
		            count++;
		        } else {
		            count--;
		        }
		        if (count == 0) {
		            System.out.println(i + 1);
		            return;
		        }
		    }
		} else {
		    for (int i = k - 1; ; i--) {
		        if (arr[i] == '(') {
		            count++;
		        } else {
		            count--;
		        }
		        if (count == 0) {
		            System.out.println(i + 1);
		            return;
		        }
		    }
		}
	}
}
0