fork download
  1. def odai_11_911(h, words)
  2. w = (words.map do |e|
  3. h[e]
  4. end).compact
  5.  
  6. if w == []
  7. # 新規
  8. vmax = h.values.compact.uniq
  9. vnext = vmax == [] ? 0 : vmax.max + 1
  10. words.map do |e|
  11. h[e] = vnext
  12. end
  13. else
  14. # 既存
  15. vmin = w.min
  16. vmax = w.max
  17. words.map do |e|
  18. h[e] = w.min
  19. end
  20. h.map do |k, v|
  21. h[k] = vmin if v == vmax
  22. end
  23. end
  24. h
  25. end
  26.  
  27. h = {}
  28. while w = gets
  29. w = w.chomp
  30. words = w.split(' ')
  31. h = odai_11_911(h, words)
  32. end
  33.  
  34. h.values.uniq.map do |n|
  35. p (h.map do |k, v|
  36. v == n ? k : nil
  37. end).compact
  38. end# your code goes here
Success #stdin #stdout 0s 28224KB
stdin
goose pigeon
cat dog
eel goldfish
goose duck
horse dog
cod eel
dove pigeon
dog rhino
goldfish squid
goose lark
stdout
["goose", "pigeon", "duck", "dove", "lark"]
["cat", "dog", "horse", "rhino"]
["eel", "goldfish", "cod", "squid"]