結果

問題 No.22 括弧の対応
ユーザー Mayimg
提出日時 2018-11-21 05:49:26
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 2 ms / 5,000 ms
コード長 597 bytes
コンパイル時間 1,745 ms
コンパイル使用メモリ 170,900 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-07-20 07:37:31
合計ジャッジ時間 2,372 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 19
権限があれば一括ダウンロードができます

ソースコード

diff #

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

int main(){
	ios::sync_with_stdio(false);
	cin.tie(0);

	int n, k; cin >> n >> k;
	k--;
	string s; cin >> s;
	stack<int> pos;
	if(s[k] == ')'){
		for(int i = 0;i < n; i++) {
			if(i == k){
				cout << pos.top() + 1 << endl;
				return 0;
			}
			if(s[i] == '(') pos.push(i);
			else pos.pop();
		}
	}
	if(s[k] == '('){
		for(int i = n - 1; i >= 0; i--){
			if(i == k){
				cout << pos.top() + 1 << endl;
				return 0;
			}
			if(s[i] == ')') pos.push(i);
			else pos.pop();
		}
	}
			
			
	return 0;	
}





  
0