https://www.acwing.com/problem/content/3601/

不断找新的先序的根节点,根据位置切割中序,根据中序左右子树大小反切割先序,找到左子树对应的先序中序,然后递归处理

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#include<stdio.h>
#include<vector>
#include<map>
#include<algorithm>
#include <algorithm>
#include <iostream>
#include<string>
using namespace std;
#define ll long long
//# define int long long
#define ull unsigned long long
#define pii pair<int,int>

#define baoliu(x, y) cout << fixed << setprecision(y) << x
#define endl "\n"
#define debug1(x) cerr<<"x"<<" "
#define debug2(x) cerr<<"x"<<endl
const int N = 2e5 + 10;
const int M = 1e6 + 10;
const int inf = 0x3f3f3f3f;
const int mod = 998244353;
const double eps = 1e-8;
int n, m;
int a[N];
void dfs(string pre,string mid){
//if(pre.size()==1)
if(pre.empty())return ;
int idx=mid.find(pre[0]);


//左子树
dfs(pre.substr(1,idx),mid.substr(0,idx));
//右子树
dfs(pre.substr(idx+1),mid.substr(idx+1));
cout<<pre[0];
}
void solve(){
string pre,mid;
cin>>pre>>mid;
dfs(pre,mid);
}
int main() {
cin.tie(0);

ios::sync_with_stdio(false);

int t;
//cin>>t;
t=1;
while (t--) {
solve();
}
return 0;
}