fork download
  1. def m(A,B):return b(A,B),b(B,A)
  2. def b(A,B,C=''):
  3. while len(A)>0:p=[j for j in range(len(A),0,-1)if A[:j]in B];j=p[0]if len(p)>0 else 1;C+=A[:j][::-1];A=A[j:];
  4. return C
  5.  
  6. l=[("abc bab", "abdabc"),
  7. ("abcde", "abcd bcde"),
  8. ("hello test", "test banana"),
  9. ("birds flying high", "whistling high nerds")]
  10. for e in l:
  11. print m(*e)
  12.  
Success #stdin #stdout 0s 8968KB
stdin
Standard input is empty
stdout
('cba bba', 'badcba')
('dcbae', 'dcba edcb')
('hello tset', 'tset banana')
('bisdr flyhgih gni', 'wihstlhgih gni nesdr')