fork(4) download
  1. import collections
  2.  
  3.  
  4. def yoba(c, s, path=()):
  5.  
  6. if not s:
  7.  
  8. yield path
  9.  
  10. if isinstance(c, str):
  11.  
  12. c = collections.Counter(str.split(c))
  13.  
  14. for word in sorted(c, key=len, reverse=True):
  15.  
  16. head, tail = s[:len(word)], s[len(word):]
  17. if sorted(word) == sorted(head):
  18.  
  19. for res in yoba(c - collections.Counter(**{word: 1}), tail, path + ((word, head),)):
  20.  
  21. yield res
  22.  
  23.  
  24. a, b = input(), input()
  25. print("\n".join(map(" ".join, zip(*next(yoba(a, b))))))
  26.  
Success #stdin #stdout 0.04s 9488KB
stdin
abab ab ab ab ab ab ab ab ab ab ab ab
abababababababababababaabb
stdout
ab ab ab ab ab ab ab ab ab ab ab abab
ab ab ab ab ab ab ab ab ab ab ab aabb