結果

問題 No.22 括弧の対応
ユーザー Hydrogen332
提出日時 2024-09-27 00:53:02
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 19 ms / 5,000 ms
コード長 796 bytes
コンパイル時間 2,283 ms
コンパイル使用メモリ 198,164 KB
最終ジャッジ日時 2025-02-24 12:58:25
ジャッジサーバーID
(参考情報)
judge5 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 19
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, s, e) for (int i = (int)s; i < (int)e; i++)

int main() {
    cin.tie(nullptr);
    
    int N, K;
    string S;
    cin >> N >> K >> S;
    K--;
    
    stack<pair<char, int>> st;
    
    rep(i, 0, N) {
        if (S[i] == ')') {
            if (st.top().first == '(') {
                if (i == K) {
                    cout << st.top().second + 1 << '\n';
                    return 0;
                } else if (st.top().second == K) {
                    cout << i + 1 << '\n';
                    return 0;
                }
                
                st.pop();
            } else {
                st.push({S[i], i});
            }
        } else {
            st.push({S[i], i});
        }
    }
}
0