結果

問題 No.3290 Encrypt Failed, but Decrypt Succeeded
ユーザー kona0001
提出日時 2025-08-28 03:06:58
言語 Ruby
(3.4.1)
結果
AC  
実行時間 937 ms / 2,000 ms
コード長 1,098 bytes
コンパイル時間 352 ms
コンパイル使用メモリ 8,192 KB
実行使用メモリ 31,232 KB
最終ジャッジ日時 2025-08-28 03:07:17
合計ジャッジ時間 16,882 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 27
権限があれば一括ダウンロードができます
コンパイルメッセージ
Syntax OK

ソースコード

diff #

def assert f
  unless f
    puts "Error!: Constraint Violation"
    exit
  end
end

map = "abcdefghijklmnopqrstuvwxyz".chars
cnt = 1
hash = {}
map.each do |v|
  hash[v] = cnt
  hash[cnt] = v
  cnt += 1
end
nums = "0123456789".chars
nums_hash = {}
nums.each do |v|
  nums_hash[v] = 1
end

n,k = gets.split.map(&:to_i)
assert(1 <= n && n <= 300000)
assert(1 <= k && k <= 10**18)

s = gets.chomp.chars
s.each do |v|
  assert(nums_hash[v] == 1)
end
assert(n == s.length)
s.map!{|v|v.to_i}

# 数え上げ
dp = Array.new(n+1, 0)
dp[n] = 1
(n-1).downto(0) do |i|
  # 先頭が 0 を除く
  if s[i] != 0
    # 一桁使う
    dp[i] += dp[i+1]
    # 二桁使う
    if i != n-1
      x = s[i..i+1].join.to_i
      if hash[x] != nil
        dp[i] += dp[i+2]
      end
    end
    dp[i] = 10**18 if dp[i] > 10**18
  end
end
assert(dp[0] >= k)

# 復元
ans = []
i = 0
rest = k
while i < n
  if dp[i+1] >= rest
    ans << hash[s[i]]
    i += 1
    next
  end
  rest -= dp[i+1]
  if i+1 < n
    x = s[i..i+1].join.to_i
    if hash[x] != nil
      ans << hash[x]
      i += 2
    end
  end
end
puts ans.join
0