Files
Data-Structure/Algorithm/Divide&Conquer/UVa536-TreeRecovery.cpp
2025-08-31 10:21:06 +08:00

22 lines
592 B
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include <iostream>
#include <string>
using namespace std;
void build(string& _pre, string& _in, int pre_front, int pre_end, int in_front, int in_end){
if (pre_front > pre_end) return;
char root;
root = _pre[pre_front];
int k = _in.find(root, in_front);//´Óin_front¿ªÊ¼
int leftsize = k - in_front;
build(_pre, _in, pre_front + 1, pre_front + leftsize, in_front, k - 1);
build(_pre, _in, pre_front + leftsize + 1 , pre_end, k + 1, in_end);
cout << root << " ";
}
int main(){
string pre, in;
cin >> pre >> in;
build(pre, in, 0, pre.size() - 1, 0, in.size() - 1);
return 0;
}