def f(s,p): \x09c=s[:1];h=1;d=s.find;l=[c] \x09if''==s:print p;l=s \x09if'Z'<c:h+=d(']');l=list(s[1:h-1]) \x09if'('==c:h+=d(')');l=s[1:h-1].split('|') \x09if d('?')==h:h+=1;l=['']+l \x09for j in l:f(s[h:],p+j) f(raw_input(),'')
Note that non-ascii characters in the above source code will be escaped (such as \x9f).