Advertisement
royalsflush

PREEMPOS SPOJ Br AC

Sep 24th, 2012
159
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.05 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <algorithm>
  4. #include <string>
  5. using namespace std;
  6.  
  7. char preOrd[60];
  8. char inOrd[60];
  9. int c, n;
  10. bool isInLeft[256];
  11.  
  12. string postOrder(string in, string pre) {
  13.     if (pre.size()==0)
  14.         return "";
  15.     if (pre.size()==1)
  16.         return pre;
  17.  
  18.     int root=0;
  19.  
  20.     for (int i=0; i<(int)in.size(); i++)
  21.         if (pre[0]==in[i]) {
  22.             root=i;
  23.             break;
  24.         }
  25.  
  26.     string leftIn="", rightIn="";
  27.  
  28.     for (int i=0; i<(int)in.size(); i++)
  29.         if (i<root) leftIn+=in[i];
  30.         else
  31.             if (i>root) rightIn+=in[i];
  32.  
  33.     for (int i='a'; i<='z'; i++)
  34.         isInLeft[(int)i]=false;
  35.  
  36.     for (int i='A'; i<='Z'; i++)
  37.         isInLeft[(int)i]=false;
  38.  
  39.     for (int i=0; i<(int)leftIn.size(); i++)
  40.         isInLeft[(int)leftIn[i]]=true;
  41.  
  42.     string leftPre="", rightPre="";
  43.  
  44.     for (int i=1; i<(int)pre.size(); i++)
  45.         if (isInLeft[(int)pre[i]]) leftPre+=pre[i];
  46.         else rightPre+=pre[i];
  47.  
  48.     return postOrder(leftIn, leftPre)+postOrder(rightIn, rightPre)+pre[0];
  49. }
  50.  
  51. int main() {
  52.     scanf("%d", &c);
  53.  
  54.     while (c--) {
  55.         scanf("%d %s %s", &n,preOrd,inOrd);
  56.         printf("%s\n", postOrder(inOrd,preOrd).c_str());
  57.     }
  58.  
  59.     return 0;
  60. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement