![](https://i-blog.csdnimg.cn/direct/f4df094fc3f94a6f9781c16074f5b436.png)
#include<bits/stdc++.h>
using namespace std;
struct Node
{
char val;
int left, right;
int length;
};
Node node[100];
int p;
char pre_s[100], mid_s[100];
int createTree(int pl, int pr, int ml, int mr)
{
if(pl > pr || ml > mr)
return 0;
int np = ++p, i;
node[np].val = pre_s[pl];
for(i = ml; i <= mr; ++i)
if(mid_s[i] == pre_s[pl])
break;
node[np].left = createTree(pl + 1, pl + i - ml, ml, i - 1);
node[np].right = createTree(pl + i - ml + 1, pr, i + 1, mr);
return np;
}
int calcLen(int r)
{
if(r == 0)
return 0;
else if(node[r].left == 0 && node[r].right == 0)
return node[r].length = 1;
else
return node[r].length = calcLen(node[r].left) + calcLen(node[r].right);
}
void preOrder(int r)
{
if(r == 0)
return;
for(int i = 0; i < node[r].length; ++i)
cout << node[r].val;
cout << endl;
preOrder(node[r].left);
preOrder(node[r].right);
}
int main()
{
cin >> pre_s >> mid_s;
int len = strlen(pre_s);
int root = createTree(0, len - 1, 0, len - 1);
calcLen(root);
preOrder(root);
return 0;
}
![](https://i-blog.csdnimg.cn/direct/adfce465812649baaaebafae0dec9427.gif)