結果

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

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 RE -
testcase_01 RE -
testcase_02 AC 133 ms
16,256 KB
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 AC 87 ms
12,416 KB
testcase_18 AC 88 ms
12,288 KB
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 AC 89 ms
12,288 KB
testcase_24 AC 91 ms
12,288 KB
testcase_25 WA -
testcase_26 WA -
testcase_27 WA -
testcase_28 WA -
testcase_29 WA -
testcase_30 WA -
testcase_31 WA -
testcase_32 AC 90 ms
12,288 KB
testcase_33 AC 91 ms
12,160 KB
testcase_34 AC 92 ms
12,416 KB
testcase_35 AC 90 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
      a += ch.to_i
      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