結果

問題 No.3290 Encrypt Failed, but Decrypt Succeeded
ユーザー koba-e964
提出日時 2025-09-18 10:40:40
言語 Ruby
(3.4.1)
結果
MLE  
実行時間 -
コード長 901 bytes
コンパイル時間 1,049 ms
コンパイル使用メモリ 7,936 KB
実行使用メモリ 823,516 KB
最終ジャッジ日時 2025-09-18 10:42:10
合計ジャッジ時間 87,805 ms
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 10 MLE * 1 -- * 16
権限があれば一括ダウンロードができます
コンパイルメッセージ
Syntax OK

ソースコード

diff #

# Modified version of https://yukicoder.me/submissions/1115587
map = "abcdefghijklmnopqrstuvwxyz".chars
cnt = 1
hash = {}
map.each do |v|
  hash[v] = cnt
  hash[cnt] = v
  cnt += 1
end

n,k = gets.split.map(&:to_i)
s = gets.chomp.chars.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
    # TLE! This should cause infinitely growing dp[i]
    # dp[i] = 10**18 if dp[i] > 10**18
  end
end

# 復元
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