fork download
  1. m = []
  2. s = []
  3. while 1:
  4. x = input().strip().split()
  5. if x[0] == 'end':
  6. break
  7. if x[0] not in m:
  8. m.append(x[0])
  9. s.append([x[0]])
  10. for i in range(1,len(x)):
  11. if x[i] not in s[-1]:
  12. s[-1].append(x[i])
  13. else:
  14. f = m.index(x[0])
  15. for i in range(1,len(x)):
  16. if x[i] not in s[f]:
  17. s[f].append(x[i])
  18. s.sort(key = lambda k: len(k))
  19. u = []
  20. for i in s:
  21. for j in range(1,len(i)):
  22. if i[j] not in u:
  23. u.append(i[j])
  24. print(i[0],i[j])
  25. break
Success #stdin #stdout 0.02s 28376KB
stdin
Apple A B C D
Banana E F G G G G
Cat H J K
Dog A H E F G D B
end
stdout
Banana E
Cat H
Apple A
Dog F