結果

問題 No.22 括弧の対応
ユーザー monburan_0401
提出日時 2018-09-15 21:55:46
言語 C
(gcc 13.3.0)
結果
AC  
実行時間 2 ms / 5,000 ms
コード長 571 bytes
コンパイル時間 383 ms
コンパイル使用メモリ 29,440 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-07-20 07:34:12
合計ジャッジ時間 804 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 19
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <stdio.h>
int main(void){
	int N,K;						//	N: 文字数 K:指定番号
	char S[10001];
	int count = 0;
		
	scanf("%d %d",&N,&K);
	scanf("%s",S);
	
	if(S[K-1] == ')'){
		for(int i = K-1; i >= 0; i--){
			if(S[i] == ')'){
				count++;
			}
			if(S[i] == '('){
				count--;
			}
			if(count == 0){
				printf("%d\n",i+1);
				return 0;
			}
		}
	}
	else if(S[K-1] == '('){
		for(int i = K-1; i < N; i++){
			if(S[i] == '('){
				count++;
			}
			if(S[i] == ')'){
				count--;
			}
			if(count == 0){
				printf("%d\n",i+1);
				return 0;
			}
		}
	}
	return 0;
}
0