結果

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

ソースコード

diff #

#include <iostream>
#include <stack>

int solve(int N, int K, std::string S) {
    std::stack<int> stk;
    for (int i = 1; i < N + 1; i++) {
        if (S.at(i - 1) == '(') {
            stk.push(i);
        } else {
            if (i == K) {
                return stk.top();
            } else if (stk.top() == K) {
                return i;
            }
            stk.pop();
        }
    }
    return 0;
}

int main() {
    int N, K;
    std::cin >> N >> K;
    std::string S;
    std::cin >> S;
    std::cout << solve(N, K, S) << std::endl;
}
0