結果

問題 No.22 括弧の対応
ユーザー jp_ste
提出日時 2016-02-19 00:07:21
言語 Java
(openjdk 23)
結果
AC  
実行時間 164 ms / 5,000 ms
コード長 914 bytes
コンパイル時間 2,463 ms
コンパイル使用メモリ 78,056 KB
実行使用メモリ 41,700 KB
最終ジャッジ日時 2024-07-20 07:09:21
合計ジャッジ時間 5,328 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 19
権限があれば一括ダウンロードができます

ソースコード

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