結果

問題 No.592 括弧の対応 (2)
ユーザー kichirb3
提出日時 2018-03-14 07:19:12
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 247 ms / 5,000 ms
コード長 901 bytes
コンパイル時間 768 ms
コンパイル使用メモリ 77,804 KB
実行使用メモリ 6,820 KB
最終ジャッジ日時 2024-11-25 06:49:28
合計ジャッジ時間 2,067 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 1
other AC * 3
権限があれば一括ダウンロードができます

ソースコード

diff #

// No.592 括弧の対応 (2)
// https://yukicoder.me/problems/no/592
//

#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <stack>
using namespace std;

vector<int> solve(string &S);


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

    int N;
    cin >> N;
    string S;
    cin >> S;
    vector<int> ans = solve(S);
    for (auto i = 1; i < ans.size(); ++i)
        cout << ans[i] << endl;
}

vector<int> solve(string &S) {
    vector<int> res(S.size()+1);
    stack<int> status;
    for (auto i = 0; i < S.size(); ++i) {
        if (S[i] == '(') {
            status.push(i + 1);
        } else if (S[i] == ')') {
            if (!status.empty()) {
                int t = status.top();
                status.pop();
                res[i + 1] = t;
                res[t] = i + 1;
            }
        }
    }
    return res;
}

0