結果

問題 No.265 数学のテスト
ユーザー simansiman
提出日時 2022-12-15 07:23:30
言語 Ruby
(3.4.1)
結果
RE  
実行時間 -
コード長 979 bytes
コンパイル時間 103 ms
コンパイル使用メモリ 7,424 KB
実行使用メモリ 20,992 KB
最終ジャッジ日時 2024-11-09 00:42:59
合計ジャッジ時間 4,413 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 30 RE * 2
権限があれば一括ダウンロードができます
コンパイルメッセージ
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