結果

問題 No.22 括弧の対応
ユーザー kuramukuramu
提出日時 2016-01-20 10:21:20
言語 Java21
(openjdk 21)
結果
AC  
実行時間 49 ms / 5,000 ms
コード長 1,047 bytes
コンパイル時間 3,641 ms
コンパイル使用メモリ 75,100 KB
実行使用メモリ 50,036 KB
最終ジャッジ日時 2023-09-27 13:01:09
合計ジャッジ時間 4,906 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 47 ms
47,960 KB
testcase_01 AC 46 ms
49,556 KB
testcase_02 AC 47 ms
49,760 KB
testcase_03 AC 48 ms
49,660 KB
testcase_04 AC 48 ms
49,824 KB
testcase_05 AC 47 ms
49,836 KB
testcase_06 AC 46 ms
49,764 KB
testcase_07 AC 47 ms
49,516 KB
testcase_08 AC 47 ms
49,544 KB
testcase_09 AC 47 ms
49,464 KB
testcase_10 AC 48 ms
49,704 KB
testcase_11 AC 47 ms
49,660 KB
testcase_12 AC 47 ms
49,740 KB
testcase_13 AC 48 ms
49,460 KB
testcase_14 AC 49 ms
49,516 KB
testcase_15 AC 48 ms
50,036 KB
testcase_16 AC 48 ms
49,468 KB
testcase_17 AC 49 ms
49,764 KB
testcase_18 AC 48 ms
49,624 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Main {

	public static void main(String[] args) {
	      BufferedReader stdReader =new BufferedReader(new InputStreamReader(System.in));
	      try {
	    	  String[] elements = stdReader.readLine().split(" ");
	    	  int N = Integer.parseInt(elements[0]);
	    	  int K = Integer.parseInt(elements[1])-1;
	    	  String c = stdReader.readLine();
	    	  int count = 0;
	    	  if((""+c.charAt(K)).equals("(")){
	    		  for(int i=K;i<N;i++){
	    			  if((""+c.charAt(i)).equals("(")) count++;
	    			  else count--;  
	    			  if(count == 0){
	    				  System.out.println(i+1);
	    				  break;
	    			  }
	    		  }
	    	  }else{
	    		  for(int i=K;0<=i;i--){
	    			  if((""+c.charAt(i)).equals(")"))count++;
	    			  else count--;    			  
	    			  if(count == 0){
	    				  System.out.println(i+1);
	    				  break;
	    			  }
	    		  }
	    	  }
	    	  } catch (IOException e) {
			e.printStackTrace();
	      }
	}
}
0