結果

問題 No.22 括弧の対応
ユーザー TLwiegehtt
提出日時 2015-07-07 01:48:49
言語 C90
(gcc 12.3.0)
結果
AC  
実行時間 1 ms / 5,000 ms
コード長 653 bytes
コンパイル時間 106 ms
コンパイル使用メモリ 20,992 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-07-20 07:02:27
合計ジャッジ時間 720 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 19
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.c: In function ‘main’:
main.c:11:9: warning: ignoring return value of ‘scanf’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   11 |         scanf("%d %d", &N, &K);
      |         ^~~~~~~~~~~~~~~~~~~~~~
main.c:12:9: warning: ignoring return value of ‘scanf’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   12 |         scanf("%s", S);
      |         ^~~~~~~~~~~~~~

ソースコード

diff #

#include <stdio.h>



int main(void){
	int i;
	int N,K;
	int kDepth = 0;
	int depth = 0;
	char S[10100];
	scanf("%d %d", &N, &K);
	scanf("%s", S);
	
	if(S[K-1] == '('){
		for(i=0;i<N;i++){
			if(S[i] == '('){
				depth += 1;
			}
			
			if(kDepth == depth ){
				printf("%d\n", i+1);
				break;
			}
			
			if(K == (i+1)){
				kDepth = depth;
			}
			
			if(S[i] == ')'){
				depth -= 1;
			}
		}
	}else{
		for(i=N-1;i>=0;i--){
			if(S[i] == ')'){
				depth += 1;
			}
			
			if(kDepth == depth ){
				printf("%d\n", i+1);
				break;
			}
			
			if(K == (i+1)){
				kDepth = depth;
			}
			
			if(S[i] == '('){
				depth -= 1;
			}
		}
	}
	
	return 0;
}
0