結果

問題 No.265 数学のテスト
ユーザー simansiman
提出日時 2022-12-15 07:23:30
言語 Ruby
(3.3.0)
結果
RE  
実行時間 -
コード長 979 bytes
コンパイル時間 44 ms
コンパイル使用メモリ 7,552 KB
実行使用メモリ 21,120 KB
最終ジャッジ日時 2024-04-26 10:13:13
合計ジャッジ時間 4,605 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 RE -
testcase_01 RE -
testcase_02 AC 130 ms
16,128 KB
testcase_03 AC 116 ms
14,592 KB
testcase_04 AC 120 ms
14,592 KB
testcase_05 AC 123 ms
14,848 KB
testcase_06 AC 119 ms
14,976 KB
testcase_07 AC 119 ms
14,848 KB
testcase_08 AC 120 ms
16,384 KB
testcase_09 AC 106 ms
14,592 KB
testcase_10 AC 107 ms
14,464 KB
testcase_11 AC 117 ms
14,976 KB
testcase_12 AC 83 ms
12,032 KB
testcase_13 AC 96 ms
13,568 KB
testcase_14 AC 91 ms
12,928 KB
testcase_15 AC 80 ms
12,032 KB
testcase_16 AC 86 ms
12,544 KB
testcase_17 AC 77 ms
12,160 KB
testcase_18 AC 79 ms
12,160 KB
testcase_19 AC 81 ms
12,032 KB
testcase_20 AC 81 ms
12,160 KB
testcase_21 AC 80 ms
12,160 KB
testcase_22 AC 91 ms
12,928 KB
testcase_23 AC 83 ms
12,160 KB
testcase_24 AC 80 ms
12,160 KB
testcase_25 AC 97 ms
13,056 KB
testcase_26 AC 83 ms
12,032 KB
testcase_27 AC 79 ms
12,288 KB
testcase_28 AC 85 ms
12,416 KB
testcase_29 AC 77 ms
12,160 KB
testcase_30 AC 84 ms
12,544 KB
testcase_31 AC 81 ms
12,032 KB
testcase_32 AC 83 ms
12,288 KB
testcase_33 AC 82 ms
12,160 KB
testcase_34 AC 83 ms
12,160 KB
testcase_35 AC 82 ms
12,160 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
Syntax OK

ソースコード

diff #

N = gets.to_i
d = gets.to_i
S = gets.chomp
MEMO = Hash.new
stack = []

S.chars.each_with_index do |s, idx|
  if s == '{'
    stack << idx
  elsif s == '}'
    r_idx = stack.pop
    MEMO[idx] = r_idx
    MEMO[r_idx] = idx
  end
end

def parse(line, left, right)
  i = left
  a = 0
  d = 0
  res = Hash.new(0)

  while i <= right
    ch = line[i]

    case ch
    when 'd'
      l = i + 1
      r = MEMO[l]

      ret = parse(line, l + 1, r - 1)
      ret.each do |deg, e|
        next if deg == 0
        res[deg - 1] += e * deg
      end

      i = r + 1
    when 'x'
      d += 1
      a = 1 if a == 0

      i += 1
    when '*'
      i += 1
    when '+'
      res[d] += a

      d = 0
      a = 0
      i += 1
    when '{'
      i += 1
    else
      if d == 0
        a += ch.to_i
      else
        a *= ch.to_i
      end

      i += 1
    end
  end

  if a != 0 || d != 0
    res[d] += a
  end

  res
end

res = parse(S, 0, N - 1)

puts [*0..d].map { |i| res[i] }.join(' ')
0