Kr, Kb = gets.split.take(2).map(&:to_i) S = gets.chomp def valid?(s) range = 0...s.size !s.size.times.any?{|i| case s[i] when ?R range.cover?(i - Kr) && s[i - Kr] == ?R ||\ range.cover?(i + Kr) && s[i + Kr] == ?R when ?B range.cover?(i - Kb) && s[i - Kb] == ?B ||\ range.cover?(i + Kb) && s[i + Kb] == ?B else end } end q = {S=>1} while !q.empty? qq = {} q.keys.each{|s| s.size.times{|i| next if s[i] == ?W t = s[0...i] + s[i+1..-1] if valid?(t) p t.size exit end qq[t] = 1 } } q = qq end