fork download
  1. def qs(er)
  2. h = Hash.new Float::INFINITY
  3. erc = 0
  4. er.each do |r|
  5. min = r.map{|e| h[e]}.min
  6. ern = min == Float::INFINITY ? erc += 1 : min
  7. r.each{|e| h[e] = ern}
  8. end
  9. h.group_by(&:last).values.map{|s| s.map(&:first).sort}
  10. end
  11.  
  12. er = $<.readlines.map &:split
  13. puts qs(er).map{|a| a * ' '} * $/
Success #stdin #stdout 0s 28216KB
stdin
goose pigeon
cat dog
eel goldfish
goose duck
horse dog
cod eel
dove pigeon
dog rhino
goldfish squid
goose lark
squid lark
stdout
dove duck goose lark pigeon squid
cat dog horse rhino
cod eel goldfish