打卡 代码写的有点乱 思路表述不清晰 但终究是做出来了 花了不少时间
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define N 100
int n;
char layer[N] = "";
char mid[N] = "";
char ans[N] = "";
typedef struct Free
{
char value;
struct Free* left;//左节点
struct Free* right;//右节点
}Free;
void fun(int la, int rear, int front, Free* now)
{
now->value = layer[la];
now->left = NULL;
now->right = NULL;
if (la == n - 1) return;
for (int i = la + 1;i < n;i++)
{
if (mid[layer[i]] < mid[layer[la]] && mid[layer[i]] >= rear && mid[layer[i]] <= front && now->left == NULL)
{
Free* left = (Free*)malloc(sizeof(Free));
now->left = left;
fun(i, rear, mid[layer[la]], left);
}
if (mid[layer[i]] > mid[layer[la]] && mid[layer[i]] >= rear && mid[layer[i]] <= front )
{
Free* right = (Free*)malloc(sizeof(Free));
now->right = right;
fun(i, mid[layer[la]], front, right);
return;
}
}
}
void Print(Free* now)
{
cout << now->value;
if (now->left != NULL) Print(now->left);
if (now->right != NULL) Print(now->right);
free(now);
}
int main()
{
cin >> mid >> layer;
n = strlen(mid);
for (int i = 0;i < n;i++) mid[mid[i]] = i;
Free* head = (Free*)malloc(sizeof(Free));
head->left = NULL;
head->right = NULL;
fun(0, 0, n - 1, head);
Print(head);
return 0;
}