リアルタイム現場で頂いた意見で少し修正しています。
def battle(ms, ws)
mh = {:B=>:a,:D=>:c,:F=>:e,:H=>:g,:J=>:i,:L=>:k}
wh = {:B=>:c,:D=>:e,:F=>:g,:H=>:i,:J=>:k,:L=>:a}
ms_k, ms_a = ms.partition{|m| ws.include?(mh[m])}
return 0 if ms_k.size == 0
ws_a = ms_k.map{|m| wh[m]}
battle(ms_a, ws_a) + ms_k.size
end
def solve(q)
ms, ws = q.chars.map(&:to_sym).partition{|e| (:A..:Z).include?(e)}
battle(ms, ws)
end
DATA.readlines.each do |line|
no,q,a = line.chop.split(/\s+/)
ans = solve(q)
print no + "\t" + ans.to_s
puts ans == a.to_i ? ' o' : ' x'
end
__END__
0 gLDLBgBgHDaD 6
1 DBcDLaLgDBH 6
2 JJca 0