fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. #define AC cin.sync_with_stdio(0),cin.tie(0);
  4. int preord[1000000],inord[1000000];
  5.  
  6. void tree( int pre ,int in, int root){
  7. int leftN;
  8. //leftN = left tree nodes and leaves total cnt
  9. for(int i=0;i<root;i++){
  10. if(inord[i+in] == preord[pre]) leftN = i;
  11. }
  12. //cout<<root<<" "<<leftN<<"\n";
  13. //left
  14. if(leftN != 0 ){
  15. tree(pre+1,in, leftN );
  16. }
  17. if(leftN != root-1){
  18. //right not empty
  19. tree(pre+1+leftN, in+1+leftN, root-leftN-1);
  20. }
  21.  
  22. cout<<char('A'-1+preord[pre]);//leaf and node
  23.  
  24. }
  25. int main(){
  26. AC
  27. string tmp1,tmp2;
  28. cin>>tmp1>>tmp2;
  29. for(int i=0;i<tmp1.length();i++){
  30. preord[i]=tmp1[i]-'A'+1;
  31. inord[i] =tmp2[i]-'A'+1;
  32. //cout<<preord[i]<<" ";
  33. }
  34. //cout<<"\n";
  35. //preoder: mid->left->right
  36. //inorder: left->mid->right
  37. //postorder: left->right->mid
  38. tree(0,0,tmp1.length());
  39. cout<<"\n";
  40. return 0;
  41.  
  42. }
Success #stdin #stdout 0.01s 5580KB
stdin
ABDECF DBEACF 
stdout
DEBFCA