Advertisement
pgapr14

algoH1

Oct 11th, 2015
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.64 KB | None | 0 0
  1. #include <iostream>
  2. #include <time.h>
  3. #include <decimal/decimal>
  4. #include <bitset>
  5. #include <algorithm>
  6. #include <set>
  7. #include <vector>
  8. #include <utility>
  9. #include <queue>
  10. #define For(a, b, c) for(int b=a; b<c; b++)
  11. #define rFor(a, b, c) for(int b=a; b>c; b--)
  12. //#include <vector>
  13. using namespace std;
  14. string a;
  15. struct node{
  16.     char val;
  17.     node* Lch;
  18.     node* Rch;
  19.  
  20.     node(char value, node * Lnode, node * Rnode){
  21.         val = value;
  22.         Lch = Lnode;
  23.         Rch = Rnode;
  24.     }
  25. };
  26.  
  27. void build(node* curNode, int & curIndex){
  28.     if(curIndex == -1)
  29.         return;
  30.  
  31.     if(curNode->val > 'Z')
  32.         return;
  33.  
  34.     if(curNode->Rch == NULL){
  35.         curIndex--;
  36.         curNode->Rch = new node(a[curIndex], NULL, NULL);
  37.         build(curNode->Rch, curIndex);
  38.     }
  39.  
  40.     if(curNode->Lch == NULL){
  41.         curIndex--;
  42.         curNode->Lch = new node(a[curIndex], NULL, NULL);
  43.         build(curNode->Lch, curIndex);
  44.     }
  45.  
  46. }
  47.  
  48. void print(node* head){
  49.     queue<node*> qq;
  50.     qq.push(head);
  51.     int i = a.length();
  52.     while(!qq.empty()){
  53.         a[--i] = qq.front()->val;
  54.         node* z = qq.front();
  55.         qq.pop();
  56.         if(z->Lch != NULL)
  57.             qq.push(z->Lch);
  58.  
  59.         if(z->Rch != NULL)
  60.             qq.push((z->Rch));
  61.     }
  62. }
  63.  
  64. int main()
  65. {
  66.     //algo homework
  67.  
  68.     clock_t end; clock_t start;
  69.  
  70.     int n;
  71.     cin>>n;
  72.  
  73.  
  74. for(int i=0; i<n; i++){
  75.     cin>>a;
  76.  
  77.     //cout<<(a[a.length()-1]);
  78.     node* head = new node(a[a.length()-1], NULL, NULL);
  79.  
  80.     int curIndex = a.length()-1;
  81.  
  82.     build(head, curIndex);
  83.  
  84.     print(head);
  85.  
  86.     cout<<a<<endl;
  87. }
  88.  
  89.     return 0;
  90. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement