f=lambda s,r='',c='':f(s[1:],*{'B':(r[:-1],c),'C':(r,r),'D':('',c),'P':(r+c,c),'':(r+s[0],c)}[s[0][1:2]])if s else r

tests = [
	['H', 'e', 'l', 'l', 'o'],
	['H', 'e', 'l', 'l', 'o', ' ', '[C]', '[P]'],
	['[D]', 'D', '[B]'],
	[''],
	['e', '[C]', '[B]', 'I', ' ', 'l', 'i', 'k', '[P]', ' ', 'b', '[P]', '[P]', 's', '!'],
	['N', '[P]'],
	['#', '5', '0', 'K', '0', '0', '1', '[D]', '#', 'n', 'o', 't']]
note = '* Note: output is repr(f(input)), to quote the string for clarity *'
border1  = '*'*len(note)+'\n'
border2 = '*'+' '*(len(note)-2)+'*'+'\n'
print border1+border2+note+'\n'+border2+border1
for test in tests:
	print 'Input:', test
	print 'Output:', repr(f(test))
	print ''