fork download
  1. def qs(er)
  2. h = Hash.new Float::INFINITY
  3. erc = 0
  4. er.each do |r|
  5. min, max = r.map{|e| h[e]}.minmax
  6. ern = min == Float::INFINITY ? erc += 1 : min
  7. h.each{|k, v| h[k] = ern if v == max}
  8. r.each{|e| h[e] = ern}
  9. end
  10. h.group_by(&:last).values.map{|s| s.map(&:first).sort}
  11. end
  12.  
  13. ers = [<<~EOT, <<~EOT, <<~EOT]
  14. goose pigeon
  15. cat dog
  16. eel goldfish
  17. goose duck
  18. horse dog
  19. cod eel
  20. dove pigeon
  21. dog rhino
  22. goldfish squid
  23. goose lark
  24. EOT
  25. goose pigeon
  26. cat dog
  27. eel goldfish
  28. goose duck
  29. horse dog
  30. cod eel
  31. dove pigeon
  32. dog rhino
  33. goldfish squid
  34. goose lark
  35. squid lark
  36. EOT
  37. cat cat
  38. dog cat
  39. dog cat
  40. cat dog
  41. dog horse
  42. horse rhino
  43. rhino dog
  44. lark lark
  45. EOT
  46.  
  47. ers.each do |erstr|
  48. qsstr = qs(erstr.each_line.map &:split).map{|a| a * ' '} * $/
  49. puts <<~"EOT"
  50. # input #
  51. #{erstr}
  52. # output #
  53. #{qsstr}
  54.  
  55.  
  56. EOT
  57. end
Success #stdin #stdout 0s 28224KB
stdin
Standard input is empty
stdout
# input #
goose pigeon
cat dog
eel goldfish
goose duck
horse dog
cod eel
dove pigeon
dog rhino
goldfish squid
goose lark

# output #
dove duck goose lark pigeon
cat dog horse rhino
cod eel goldfish squid


# input #
goose pigeon
cat dog
eel goldfish
goose duck
horse dog
cod eel
dove pigeon
dog rhino
goldfish squid
goose lark
squid lark

# output #
cod dove duck eel goldfish goose lark pigeon squid
cat dog horse rhino


# input #
cat cat
dog cat
dog cat
cat dog
dog horse
horse rhino
rhino dog
lark lark

# output #
cat dog horse rhino
lark