結果

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

ソースコード

diff #

#include <iostream>
#include <vector>
#include <string>

void solve() {
    int n, k;
    std::string s;
    std::cin >> n >> k >> s;
    --k;

    std::vector<int> stk;
    for (int i = 0; i < n; ++i) {
        if (s[i] == '(') {
            stk.push_back(i);

        } else {
            int j = stk.back();
            stk.pop_back();

            if (i == k) {
                std::cout << j + 1 << std::endl;
            } else if (j == k) {
                std::cout << i + 1 << std::endl;
            }
        }
    }
}

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

    solve();

    return 0;
}
0