fork(1) download
  1. $list = []
  2. $words = []
  3.  
  4. def index_of_list
  5. $words.map do |e1|
  6. ret = nil
  7. $list.map.with_index do |e2, i|
  8. if e2.index(e1)
  9. ret = i
  10. break
  11. end
  12. end
  13. ret
  14. end
  15. end
  16.  
  17. def words_to_indexes
  18. w = index_of_list.flatten.compact.uniq
  19. if w == []
  20. $list << $words
  21. else
  22. $list[w.min] += $words
  23. $list.delete($list[w.max]) if w.min != w.max
  24. $list.map! do |e|
  25. e.uniq.sort
  26. end
  27. end
  28. end
  29.  
  30. while w = gets
  31. w = w.chomp
  32. $words = w.split(' ')
  33. words_to_indexes
  34. end
  35.  
  36. $list.map do |e|
  37. puts e.join(', ')
  38. end
  39.  
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
stdout
dove, duck, goose, lark, pigeon
cat, dog, horse, rhino
cod, eel, goldfish, squid