結果
問題 | No.22 括弧の対応 |
ユーザー |
|
提出日時 | 2023-05-09 14:50:53 |
言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 2 ms / 5,000 ms |
コード長 | 956 bytes |
コンパイル時間 | 1,470 ms |
コンパイル使用メモリ | 171,924 KB |
実行使用メモリ | 5,248 KB |
最終ジャッジ日時 | 2024-11-26 01:46:49 |
合計ジャッジ時間 | 2,154 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge2 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 19 |
ソースコード
#include<bits/stdc++.h> using namespace std; using ll = long long; using Graph = vector<vector<int>>; int main () { int N,M; cin >> N >> M; string S; cin >> S; stack<int> sta; stack<int> dic; M--; if(S[M] == '(') { int ans = 0; for(int i = 0; i < N; i++) { if('(' == S[i]) { sta.push(S[i]); dic.push(i); }else { sta.pop(); int x= dic.top(); dic.pop(); if(x == M) { ans = i + 1; } } } cout << ans << endl; }else { int ans = 0; for(int i = N - 1; i >= 0; i--) { if(')' == S[i]) { sta.push(S[i]); dic.push(i); }else { sta.pop(); int x= dic.top(); dic.pop(); if(x == M) { ans = i + 1; } } } cout << ans << endl; } }