# Код на ruby 2.2.3p173
def zadanie(line, time)
line = line.gsub(/[^MD]/, "")
time.times {
k = 0
while k < line.size - 1 do
if line[k..k + 1] == "DM"
line[k], line[k + 1] = line[k + 1], line[k]
k+=1
end
k+=1
p [k,line]
end
}
return line
end
# # Примеры применения
p zadanie("MDDDMMDMDDDMMMMMMDDM", 10)
Ответ для примера
"MMMMMMMMDMDMDMDDDDDD"