結果
| 問題 |
No.592 括弧の対応 (2)
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2017-11-11 11:34:31 |
| 言語 | D (dmd 2.109.1) |
| 結果 |
AC
|
| 実行時間 | 56 ms / 5,000 ms |
| コード長 | 1,564 bytes |
| コンパイル時間 | 1,979 ms |
| コンパイル使用メモリ | 149,928 KB |
| 実行使用メモリ | 6,940 KB |
| 最終ジャッジ日時 | 2024-06-12 22:28:40 |
| 合計ジャッジ時間 | 2,746 ms |
|
ジャッジサーバーID (参考情報) |
judge2 / judge3 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 1 |
| other | AC * 3 |
ソースコード
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 cor = new int[](n);
foreach (i ; 0 .. n) {
if (s[i] == '(') {
st.push(i);
}
else {
cor[i] = st.top;
cor[st.top] = i;
st.pop;
}
}
cor[] += 1;
writefln("%(%s\n%)", cor);
}
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);
}
}
}