結果

問題 No.22 括弧の対応
ユーザー 37zigen37zigen
提出日時 2016-04-01 17:40:44
言語 Java21
(openjdk 21)
結果
AC  
実行時間 131 ms / 5,000 ms
コード長 755 bytes
コンパイル時間 3,798 ms
コンパイル使用メモリ 73,272 KB
実行使用メモリ 57,600 KB
最終ジャッジ日時 2023-09-27 13:03:44
合計ジャッジ時間 4,974 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 119 ms
57,600 KB
testcase_01 AC 111 ms
56,192 KB
testcase_02 AC 119 ms
56,012 KB
testcase_03 AC 127 ms
55,524 KB
testcase_04 AC 123 ms
56,208 KB
testcase_05 AC 125 ms
55,720 KB
testcase_06 AC 126 ms
55,788 KB
testcase_07 AC 124 ms
55,556 KB
testcase_08 AC 126 ms
55,496 KB
testcase_09 AC 131 ms
55,884 KB
testcase_10 AC 126 ms
55,856 KB
testcase_11 AC 124 ms
55,732 KB
testcase_12 AC 125 ms
55,936 KB
testcase_13 AC 125 ms
55,904 KB
testcase_14 AC 117 ms
55,740 KB
testcase_15 AC 131 ms
56,044 KB
testcase_16 AC 130 ms
56,140 KB
testcase_17 AC 109 ms
55,948 KB
testcase_18 AC 108 ms
56,340 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

package yukicoder022;
import java.util.Scanner;
public class Main {
	public static void main(String[] args){
		Scanner sc=new Scanner(System.in);
		int n=sc.nextInt();//文字数
		int k=sc.nextInt();//何文字目の対応を取るか(1<=k<=n)
		String s=sc.next();
		int[] table=new int[n];
		int count=0;
		for(int i=0;i<n;i++){
			if(s.charAt(i)=='('){
				count--;
				table[i]=count;
			}else if(s.charAt(i)==')'){
				table[i]=count;
				count++;
			}
		}
		if(s.charAt(k-1)=='('){
			for(int i=k;i<n;i++){
				if(table[i]==table[k-1]){
					System.out.println(i+1);
					return;
				}
			}
		}else if(s.charAt(k-1)==')'){
			for(int i=k-2;i>=0;i--){
				if(table[i]==table[k-1]){
					System.out.println(i+1);
					return;
				}
			}
		}
	}
}
0