結果

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

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

int main() {
	int n, m;
	string s;
	cin >> n >> m;
	cin >> s;
	int counter = 0;
	if (s[m - 1] == '(') {
		for (int i = m; i < s.length(); ++i) {
			if (s[i] == '(') {
				counter++;
			} else if (counter == 0) {
				cout << i + 1 << endl;
				return 0;
			} else {
				counter--;
			}
		}
	} else {
		for (int i = m-2; i >= 0; --i) {
			if (s[i] == ')') {
				counter++;
			} else if (counter == 0) {
				cout << i + 1  << endl;
				return 0;
			} else {
				counter--;
			}
		}
	}
}

0