結果

問題 No.22 括弧の対応
コンテスト
ユーザー kuramu
提出日時 2016-01-20 10:21:20
言語 Java
(openjdk 25.0.2)
コンパイル:
javac -encoding UTF8 _filename_
実行:
java -ea -Xmx700m -Xss256M -DONLINE_JUDGE=true _class_
結果
AC  
実行時間 32 ms / 5,000 ms
コード長 1,047 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 1,721 ms
コンパイル使用メモリ 83,156 KB
実行使用メモリ 122,224 KB
最終ジャッジ日時 2026-04-02 21:46:33
合計ジャッジ時間 2,895 ms
ジャッジサーバーID
(参考情報)
judge3_0 / judge2_0
純コード判定待ち
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 19
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

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