1011-[NOIP2001]求先序排列_2021秋季算法入门班第二章习题:递归、分治 (nowcoder.com)
#include <bits/stdc++.h>
using namespace std;
string zhong,hou;
void deal(int l1,int r1,int l2,int r2){
int pos=-1;
if(l1>r1)return ;
cout<<hou[r2];//先找根(由分析知,根一定在后序遍历的最后位置)
for(int i=l1;i<=r1;i++){
if(zhong[i]==hou[r2])pos=i;//找到根在中序遍历的位置
}
deal(l1,pos-1,l2,l2+(pos-1-l1));//由根所在的位置分为左子树,再递归找根
deal(pos+1,r1,l2+(pos-1-l1)+1,r2-1);//右子树,递归找根
}
int main(){
cin>>zhong>>hou;
int l1=zhong.size();
deal(0,l1-1,0,l1-1);
return 0;
}