結果

問題 No.22 括弧の対応
ユーザー mono1977
提出日時 2016-11-30 23:55:47
言語 C++11(廃止可能性あり)
(gcc 13.3.0)
結果
AC  
実行時間 1 ms / 5,000 ms
コード長 487 bytes
コンパイル時間 137 ms
コンパイル使用メモリ 22,912 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-07-20 07:18:32
合計ジャッジ時間 898 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 19
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function ‘int main()’:
main.cpp:5:14: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
    5 |         scanf("%d %d",&n,&k);
      |         ~~~~~^~~~~~~~~~~~~~~
main.cpp:8:14: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
    8 |         scanf("%s",s);
      |         ~~~~~^~~~~~~~

ソースコード

diff #

#include<stdio.h>

int main(){
	int n,k;
	scanf("%d %d",&n,&k);
	k--;
	char s[n+1];
	scanf("%s",s);
	int p[n],i,j=0;

	if(s[k]=='('){
		j=1;
		for(i=k+1;i<n;i++){
			if(s[i]==')'){
				j--;
				if(j==0){
					printf("%d",i+1);
					return 0;
				}
			}else{//s[i]==(;
				j++;
			}
		}
	}else{//s[k]==)
		j=-1;
		for(i=k-1;i>=0;i--){
			if(s[i]=='('){
				j++;
				if(j==0){
					printf("%d",i+1);
					return 0;
				}
			}else{//s[i]==(
				j--;
			}
		}
	}
	
	puts("???");
	return 0;
}
0