結果

問題 No.22 括弧の対応
ユーザー umaumax
提出日時 2017-05-04 01:12:21
言語 C++11(廃止可能性あり)
(gcc 13.3.0)
結果
AC  
実行時間 2 ms / 5,000 ms
コード長 489 bytes
コンパイル時間 435 ms
コンパイル使用メモリ 55,152 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-07-20 07:23:08
合計ジャッジ時間 1,020 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 19
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>

using namespace std;

int main(int argc, const char* argv[])
{
	int N, K;
	string S;
	cin >> N >> K >> S;
	int cnt  = 0;
	int kcnt = -10001;
	K--;
	bool revFlag = S[K] == ')';
	for (int i = 0; i < S.size(); i++) {
		int index		   = i;
		if (revFlag) index = N - 1 - i;
		char c			   = S[index];
		if (cnt + (c == '(' ? 1 : -1) == kcnt) {
			cout << index + 1 << endl;
			return 0;
		}
		if (index == K) {
			kcnt = cnt;
		}
		cnt += c == '(' ? 1 : -1;
	}
	return 0;
}
0