总体思路就是不断地通过后序遍历将中序遍历分成左右两个子树,重点就是确定中序和后序中字母索引从哪到哪是左子树,从哪到哪是右子树。
s1=input()
s2=input()
def dfs(l1,r1,l2,r2,len):
if len<=0:
return
# r2表示后序遍历的最后一个节点,也就是根节点
print(s2[r2],end="")
# pos是中序遍历中根所在索引,split是后序遍历中左子树最右边字母的索引
pos=l1
split=l2-1
# r1中序遍历中最后一个字母的索引
while pos<=r1:
if s1[pos]==s2[r2]:
break
pos+=1
for i in range(l1,pos):
for j in range(l2,r2):
if s1[i]==s2[j]:
split=max(split,j)
# l1是中序遍历的第一个字母索引
# l2后序遍历中的第一个字母索引
# 左子树
dfs(l1,pos-1,l2,split,pos-l1)
# 右子树
dfs(pos+1,r1,split+1,r2-1,r1-pos)
n=len(s1)
dfs(0,n-1,0,n-1,n)