結果

問題 No.22 括弧の対応
ユーザー hanorverhanorver
提出日時 2015-09-07 19:30:29
言語 C++11
(gcc 13.3.0)
結果
AC  
実行時間 2 ms / 5,000 ms
コード長 447 bytes
コンパイル時間 550 ms
コンパイル使用メモリ 64,440 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-07-20 07:03:55
合計ジャッジ時間 1,268 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 19
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<iostream>
#include<stack>
#include<algorithm>
#include<string>

int main() {
	int n, k;
	std::string str;
	std::stack<int> s;
	std::cin >> n >> k >> str;

	for (int i = 0; i < str.length(); i++) {
		if (str[i] == '(') {
			s.push(i);
		} else {
			if (i + 1 == k) {
				std::cout << s.top() + 1 << std::endl;
				break;
			} else if (s.top() + 1 == k) {
				std::cout << i + 1 << std::endl;
				break;
			}
			s.pop();
		}
	}
	return 0;
}
0