結果

問題 No.265 数学のテスト
ユーザー simansiman
提出日時 2022-12-15 07:24:45
言語 Ruby
(3.4.1)
結果
RE  
(最新)
AC  
(最初)
実行時間 -
コード長 1,218 bytes
コンパイル時間 192 ms
コンパイル使用メモリ 7,424 KB
実行使用メモリ 12,544 KB
最終ジャッジ日時 2024-11-09 00:43:55
合計ジャッジ時間 3,964 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample RE * 4
other RE * 32
権限があれば一括ダウンロードができます
コンパイルメッセージ
Syntax OK

ソースコード

diff #

if !ENV['RUBY_THREAD_VM_STACK_SIZE']
  exec({'RUBY_THREAD_VM_STACK_SIZE'=>'1000000000'}, '/usr/bin/ruby', $0)
else
  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(' ')
end
0