結果
| 問題 |
No.2595 Parsing Challenge
|
| ユーザー |
|
| 提出日時 | 2023-12-23 11:35:48 |
| 言語 | D (dmd 2.109.1) |
| 結果 |
WA
|
| 実行時間 | - |
| コード長 | 2,065 bytes |
| コンパイル時間 | 1,909 ms |
| コンパイル使用メモリ | 155,640 KB |
| 実行使用メモリ | 60,096 KB |
| 最終ジャッジ日時 | 2024-09-27 12:20:24 |
| 合計ジャッジ時間 | 13,846 ms |
|
ジャッジサーバーID (参考情報) |
judge5 / judge4 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 5 |
| other | AC * 21 WA * 9 TLE * 1 -- * 24 |
ソースコード
import std.stdio;
import std.string;
import std.conv;
import std.bigint;
import std.container.slist;
string[] lex(string s){
string[] result;
long i = 0;
while(i < s.length){
switch(s[i]){
case '+', '-', '*', '(', ')':
result ~= s[i .. i + 1];
i++;
break;
default:
auto j = i;
while(i < s.length && s[i] >= '0' && s[i] <= '9'){
i++;
}
result ~= s[j .. i];
}
}
return result;
}
int[string] priority;
string[] rpn(string[] l){
string[] result;
auto stack = SList!string();
auto minus = false;
foreach(i, s; l){
auto p = priority.get(s, 0);
switch(s){
case ")":
while(!stack.empty && stack.front != "("){
result ~= stack.front;
stack.removeFront;
}
stack.removeFront;
break;
case "(":
stack.insert(s);
break;
case "-":
if(stack.empty || l[i - 1] in priority){
minus = !minus;
break;
}
goto default;
default:
while(!stack.empty && p >= priority.get(stack.front, 0)){
result ~= stack.front;
stack.removeFront;
}
if(minus){
s = "-" ~ s;
}
minus = false;
stack.insert(s);
}
}
while(!stack.empty){
result ~= stack.front;
stack.removeFront;
}
return result;
}
BigInt eval(string[] l){
auto stack = SList!BigInt();
foreach(s; l){
switch(s){
case "+":
auto a = stack.front;
stack.removeFront;
stack.front += a;
break;
case "-":
auto a = stack.front;
stack.removeFront;
stack.front -= a;
break;
case "*":
auto a = stack.front;
stack.removeFront;
stack.front *= a;
break;
default:
stack.insert(BigInt(s));
}
}
return stack.front;
}
void main(){
auto N = readln.chomp.to!long;
auto S = readln.chomp;
priority = ["+" : 2, "-" : 2, "*" : 1, "(" : 3];
writeln(S.lex.rpn.eval);
}