結果

問題 No.22 括弧の対応
ユーザー KKT89
提出日時 2020-03-16 00:57:47
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 3 ms / 5,000 ms
コード長 632 bytes
コンパイル時間 1,505 ms
コンパイル使用メモリ 109,944 KB
最終ジャッジ日時 2025-01-09 07:11:56
ジャッジサーバーID
(参考情報)
judge3 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 19
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <algorithm>
#include <random>
#include <map>
#include <queue>
#include <cstring>
#include <set>
#include <stack>
using namespace std;
typedef long long int ll;

int main(){
	cin.tie(nullptr);
	ios::sync_with_stdio(false);
	int n,k; cin >> n >> k;
	string s; cin >> s;
	stack<pair<int,int>> st;
	k--;
	for(int i=0;i<n;i++){
		if(st.size()&&st.top().second==0&&s[i]==')'){
			if(i==k){
				cout << st.top().first+1 << endl;
				return 0;
			}
			else if(st.top().first==k){
				cout << i+1 << endl;
				return 0;
			}
			st.pop();
		}else{
			st.push(make_pair(i,(s[i]==')')));
		}
	}
}
0