結果

問題 No.592 括弧の対応 (2)
ユーザー nanaenanae
提出日時 2017-11-10 23:05:09
言語 D
(dmd 2.107.1)
結果
AC  
実行時間 60 ms / 5,000 ms
コード長 1,596 bytes
コンパイル時間 794 ms
コンパイル使用メモリ 94,704 KB
実行使用メモリ 6,116 KB
最終ジャッジ日時 2023-09-03 16:49:07
合計ジャッジ時間 1,672 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 60 ms
5,352 KB
testcase_02 AC 59 ms
5,060 KB
testcase_03 AC 59 ms
6,116 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import std.stdio, std.string, std.conv, std.algorithm, std.numeric;
import std.range, std.array, std.math, std.typecons, std.container, core.bitop;

void main() {
    int n;
    string s;

    scan(n);
    scan(s);

    auto st = Stack!(int)(n);
    auto taiou = new int[](n);

    foreach (i, ch; s) {
        if (ch == '(') {
            st.push(i.to!int);
        }
        else {
            taiou[i] = st.top;
            taiou[st.top] = i.to!int;
            st.pop;
        }
    }

    foreach (i ; 0 .. n) {
        writeln(taiou[i] + 1);
    }
}



struct Stack(T) {
private:
    int N, peek;
    T[] data;

public:
    this(int size) 
    {
        N = size;
        data = new T[](N);
    }

    bool empty() @property
    {
        return peek == 0;
    }

    bool full() @property
    {
        return peek == N;
    }

    void push(T x) @property
    {
        assert(!full);
        data[peek++] = x;
    }

    void pop() @property
    {
        assert(!empty);
        --peek;
    }

    T top() @property
    {
        return data[peek - 1];
    }

    void clear() @property
    {
        peek = 0;
    }

    int length() @property
    {
        return peek;
    }

}




void scan(T...)(ref T args) {
    string[] line = readln.split;
    foreach (ref arg; args) {
        arg = line.front.to!(typeof(arg));
        line.popFront();
    }
    assert(line.empty);
}



void fillAll(R, T)(ref R arr, T value) {
    static if (is(typeof(arr[] = value))) {
        arr[] = value;
    }
    else {
        foreach (ref e; arr) {
            fillAll(e, value);
        }
    }
}
0