ZOJ1004 귀속 실현 창고
import sys
stack = []
operate = []
str_a = ''
str_b = ''
def dfs(index_a, index_b):
if index_a == len(str_a) and index_b == len(str_b):
for oper in operate:
sys.stdout.write(oper)
sys.stdout.write(' ')
sys.stdout.write('
')
return
if index_a < len(str_a):
stack.append(str_a[index_a])
operate.append('i')
dfs(index_a 1, index_b)
stack.pop()
operate.pop()
if len(stack) > 0 and stack[-1] == str_b[index_b]:
str_pop = stack.pop()
operate.append('o')
dfs(index_a, index_b 1)
stack.append(str_pop)
operate.pop()
if __name__ == '__main__':
i = 0
is_first = True
for line in sys.stdin:
line = line.split('
')[0]
if i == 0:
str_a = line
else:
str_b = line
if is_first:
print '['
is_first = False
else:
print '
['
if len(str_a) == len(str_b):
stack = []
operate = []
dfs(0, 0)
sys.stdout.write(']')
i = (i 1) % 2
전체적인 사상은 귀속으로 창고 조작을 모의하고 깊이를 우선하는 것이다.형식 문제를 주의해야 합니다. 마지막 "]"이후에는 리턴이 없고, 각 줄의 출력 뒤에는 공백이 있습니다.