結果

問題 No.22 括弧の対応
ユーザー jp_stejp_ste
提出日時 2016-02-19 00:07:21
言語 Java19
(openjdk 21)
結果
AC  
実行時間 151 ms / 5,000 ms
コード長 914 bytes
コンパイル時間 1,749 ms
コンパイル使用メモリ 74,752 KB
実行使用メモリ 56,340 KB
最終ジャッジ日時 2023-09-27 13:02:11
合計ジャッジ時間 5,062 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 109 ms
55,892 KB
testcase_01 AC 110 ms
56,340 KB
testcase_02 AC 111 ms
55,840 KB
testcase_03 AC 151 ms
56,040 KB
testcase_04 AC 127 ms
55,768 KB
testcase_05 AC 128 ms
55,964 KB
testcase_06 AC 132 ms
56,056 KB
testcase_07 AC 129 ms
55,768 KB
testcase_08 AC 125 ms
56,088 KB
testcase_09 AC 129 ms
55,908 KB
testcase_10 AC 125 ms
55,920 KB
testcase_11 AC 123 ms
55,732 KB
testcase_12 AC 126 ms
55,828 KB
testcase_13 AC 129 ms
56,280 KB
testcase_14 AC 115 ms
56,300 KB
testcase_15 AC 129 ms
55,944 KB
testcase_16 AC 131 ms
55,796 KB
testcase_17 AC 110 ms
56,024 KB
testcase_18 AC 115 ms
55,412 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.ArrayList;
import java.util.Scanner;

public class Main {	
	public static void main(String[] args) {
		try (Scanner scan = new Scanner(System.in)) {
			int N = scan.nextInt();
			int K = scan.nextInt();
			String S = scan.next();
			ArrayList<Node> list = new ArrayList<>();
			
			for(int j=0; j<S.length(); j++) {
				if(S.charAt(j) == '(') {
					int count = 0;
					for(int i=j; i<S.length(); i++) {
						if(S.charAt(i) == '(') {
							count++;
						} else if(S.charAt(i) == ')') {
							count--;
							if(count == 0) {
								list.add(new Node(j+1, i+1));
								break;
							}
						}
					}
				}
			}
			
			for(Node now : list) {
				if(now.first == K) {
					System.out.println(now.end);
					break;
				} else if(now.end == K) {
					System.out.println(now.first);
					break;
				}
			}
		}
	}
}

class Node {
	int first, end;
	Node(int f, int e) {
		first = f;
		end = e;
	}
}
0