T236450 美国血统

#include<iostream>

#include<string>

#include<cstdio>

using namespace std;

string a,b;//a为中序,b为前序

void dfs(int l1,int l2,int l3,int l4) //l1~l2为前序遍历 l3~l4为中序遍历

{

    if(l1>l2||l3>l4)

        return;

        else{

                for(int i=l1;i<=l2;i++)

                    {

                        if(a[i]==b[l3])//找结点

                             {

                                dfs(l1,i-1,l3+1,l3+i-l1);//递归左子树

                                dfs(i+1,l2,l3+i-l1+1,l4);//递归右子树

                                cout<<a[i];//输出该结点

                             }

                    }  

            }

}

int main()

{

    cin>>a>>b;

    int l=a.size();

    dfs(0,l-1,0,l-1);//全树区间 l是从一开始所以要减一

    return 0;

}

posted @ 2022-05-04 20:48  xh小小孩  阅读(36)  评论(0)    收藏  举报