Skip to content

Instantly share code, notes, and snippets.

@whatalnk
Last active January 31, 2016 04:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save whatalnk/d8a831ce8b9838a66c03 to your computer and use it in GitHub Desktop.
Save whatalnk/d8a831ce8b9838a66c03 to your computer and use it in GitHub Desktop.
DDPC 2016 予選
s = "DiscoPresentsDiscoveryChannelProgrammingContest2016"
w = gets.chomp.to_i
n = s.length
a = n / w
b = n % w
i = 0
a.times do
puts s[i...(i + w)]
i += w
end
if b > 0 then
puts s[(n - b)...n]
end
n = gets.chomp.to_i
a = gets.chomp.split(" ").map(&:to_i)
h = Hash.new{|h,k| h[k] = []}
n.times do |i|
h[a[i]] << i
end
pos = 0
res = 1
h.keys.sort.each do |k|
v = h[k]
if v.first < pos && v.last > pos then
res += 1
pos = (v.select{|x| x < pos}).last
elsif v.last < pos then
res += 1
pos = v.last
else
pos = v.last
end
end
res -= 1 if pos == 0
puts res
# ref: Submission #624244
s = gets.chomp
k = gets.chomp.to_i
def f(s, k)
if k <= 0 then
return [s]
end
res = []
(0...(s.length)).each do |i|
res << s[0...i] + s[(i+1)..-1]
res << s[0...i] + "a" + s[(i + 1)..-1]
end
res << s[0...-1]
res << "a" + s
rr = []
res.each do |r|
rr += f(r, k-1)
end
return rr
end
if s.length > 10 then
puts -1
exit
end
ans = f(s, k).min
if ans == "" then
ans = "a"
end
puts ans
# ref: Submission #625127
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment