結果

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

ソースコード

diff #

#include<cstdio>


int main(void)
{
	int n, k, i, f, d;
	char s[10000+2];

	while(scanf("%d%d\n", &n, &k)==2)
	{
		scanf("%[()]", s);

		if(s[k-1]=='(') d= 1;
		else            d=-1;

		f=d;
		for(i=k-1+d;i<n && i>=0;i+=d)
		{
			if(s[i]=='(') f++;
			if(s[i]==')') f--;
			if(f==0) break;
		}
		printf("%d\n", i+1);
	}
	return 0;
}
0