結果

問題 No.2595 Parsing Challenge
ユーザー laneguelanegue
提出日時 2023-12-23 12:10:55
言語 D
(dmd 2.106.1)
結果
WA  
実行時間 -
コード長 2,066 bytes
コンパイル時間 1,702 ms
コンパイル使用メモリ 144,640 KB
実行使用メモリ 43,312 KB
最終ジャッジ日時 2023-12-23 12:11:11
合計ジャッジ時間 15,054 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
6,676 KB
testcase_01 AC 1 ms
6,676 KB
testcase_02 AC 1 ms
6,676 KB
testcase_03 AC 1 ms
6,676 KB
testcase_04 AC 1 ms
6,676 KB
testcase_05 AC 1 ms
6,676 KB
testcase_06 AC 1 ms
6,676 KB
testcase_07 AC 2 ms
6,676 KB
testcase_08 AC 2 ms
6,676 KB
testcase_09 AC 1 ms
6,676 KB
testcase_10 WA -
testcase_11 WA -
testcase_12 AC 1 ms
6,676 KB
testcase_13 AC 1 ms
6,676 KB
testcase_14 AC 1 ms
6,676 KB
testcase_15 AC 2 ms
6,676 KB
testcase_16 AC 1 ms
6,676 KB
testcase_17 WA -
testcase_18 AC 2 ms
6,676 KB
testcase_19 AC 1 ms
6,676 KB
testcase_20 AC 6 ms
6,676 KB
testcase_21 WA -
testcase_22 AC 5 ms
6,676 KB
testcase_23 WA -
testcase_24 AC 6 ms
6,676 KB
testcase_25 WA -
testcase_26 WA -
testcase_27 AC 43 ms
6,676 KB
testcase_28 WA -
testcase_29 AC 44 ms
6,676 KB
testcase_30 WA -
testcase_31 AC 705 ms
42,788 KB
testcase_32 AC 747 ms
38,692 KB
testcase_33 AC 1,111 ms
43,268 KB
testcase_34 AC 917 ms
41,756 KB
testcase_35 TLE -
testcase_36 -- -
testcase_37 -- -
testcase_38 -- -
testcase_39 -- -
testcase_40 -- -
testcase_41 -- -
testcase_42 -- -
testcase_43 -- -
testcase_44 -- -
testcase_45 -- -
testcase_46 -- -
testcase_47 -- -
testcase_48 -- -
testcase_49 -- -
testcase_50 -- -
testcase_51 -- -
testcase_52 -- -
testcase_53 -- -
testcase_54 -- -
testcase_55 -- -
testcase_56 -- -
testcase_57 -- -
testcase_58 -- -
testcase_59 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

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();
  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){
          stack.insert("!");
          break;
        }
        goto default;
      default:
        while(!stack.empty && p >= priority.get(stack.front, 0)){
          result ~= stack.front;
          stack.removeFront;
        }
        stack.insert(s);
    }
  }
  while(!stack.empty){
    result ~= stack.front;
    stack.removeFront;
  }
  //stderr.writeln(result);
  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;
      case "!":
        stack.front *= -1;
        break;
      default:
        stack.insert(BigInt(s));
    }
  }
  return stack.front;
}

void main(){
  auto N = readln.chomp.to!long;
  auto S = readln.chomp;
  priority = ["+" : 3, "-" : 3, "*" : 2, "(" : 4, "!" : 1];
  writeln(S.lex.rpn.eval);
}
0