#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <sstream>
#include <vector>
using namespace std;
#define VI vector<int>
#define ALL(x) x.begin(),x.end()
#define SZ(x) (x).size()
const int maxn = 30;
char pre[maxn];
char in[maxn];
struct Node{
int val;
Node* lchild;
Node* rchild;
};
Node* build_tree(const VI& x,const VI& y){
Node* crt = NULL;
if(!y.empty()){
int cnt = x.front();
crt = new Node;
crt->lchild = crt->rchild = NULL;
VI::const_iterator it =find(ALL(y),cnt); //这里的find用的是algorithm
VI p1(y.begin(),it);
VI p2(++it,y.end());
VI q1(x.begin()+1,x.begin()+1+SZ(p1));
VI q2(x.begin()+1+SZ(p1),x.begin()+1+SZ(p1)+SZ(p2));
crt->val = cnt;
crt->lchild = build_tree(q1,p1);
crt->rchild = build_tree(q2,p2);
}
return crt;
}
void solve(Node* tree){
if(tree){
solve(tree->lchild);
solve(tree->rchild);
char ch = tree->val+'A';
printf("%c",ch);
if(!tree->lchild&&tree->rchild)return;
}
}
int main()
{
// freopen("in.txt","r",stdin);
while(~scanf("%s%s",pre,in)){
int len = strlen(pre);
vector<int>A;
vector<int>B;
for(int i=0;i<len;++i)A.push_back((int)(pre[i]-'A'));
for(int i=0;i<len;++i)B.push_back((int)(in[i]-'A'));
Node* tree = build_tree(A,B);
solve(tree);
printf("\n");
}
return 0;
}
UVa 536 Tree Recovery
最新推荐文章于 2024-11-03 22:17:06 发布