fork download
  1. def e(s,l):return''==l and s or"^"==l[0]and l[1]+e(s+' ',l[2:])or' '+e(s+l[0],l[1:])
  2. print(e('\n',input()))
Success #stdin #stdout 0.02s 9984KB
stdin
abc^d+ef^g + hijk^l - M^NO^P (Ag^+)
stdout
   d   g       l    N P    + 
abc +ef  + hijk  - M O  (Ag )