Altered Structure
This commit is contained in:
21
Algorithm/Divide&Conquer/UVa536-TreeRecovery.cpp
Normal file
21
Algorithm/Divide&Conquer/UVa536-TreeRecovery.cpp
Normal file
@@ -0,0 +1,21 @@
|
||||
#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);//<2F><>in_front<6E><74>ʼ
|
||||
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;
|
||||
}
|
Reference in New Issue
Block a user