Advertisement
royalsflush

Referência para LA 3538 (Luiza)

Apr 6th, 2012
42
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.94 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <algorithm>
  3. #include <string.h>
  4. using namespace std;
  5.  
  6. char tmp[5];
  7. int n1, n2;
  8. char t1[100010], t2[100010];
  9. int d; int pos;
  10. char str[100010];
  11.  
  12. bool eq(int r1, int r2) {
  13.     if (t1[r1]!=t2[r2])
  14.         return false;
  15.  
  16.     if (t1[r1]=='$')
  17.         return true;
  18.  
  19.     return (eq(2*r1+1,2*r2+1)&& eq(2*r1+2,2*r2+2)) ||
  20.         (eq(2*r1+1,2*r2+2) && eq(2*r1+2,2*r2+1));
  21. }
  22.  
  23. void buildTree(char* t, int tpos) {
  24.     if (pos<0) return;
  25.  
  26.     t[tpos]=str[pos];
  27.     if (str[pos]=='$') {
  28.         pos--;
  29.         return;
  30.     }
  31.  
  32.     pos--;
  33.     buildTree(t,2*tpos+2);
  34.     buildTree(t,2*tpos+1);
  35. }
  36.  
  37. void getTree(char* t, int& n) {
  38.     n=0;
  39.     while (1) {
  40.         scanf(" %s", tmp);
  41.         if (!strcmp(tmp, "end")) break;
  42.         if (!strcmp(tmp, "nil")) str[n++]='$';
  43.         else str[n++]=tmp[0];
  44.     }
  45.  
  46.     pos=n-1;
  47.     buildTree(t,0);
  48. }
  49.  
  50. int main() {
  51.     scanf("%d", &d);
  52.  
  53.     while (d--) {
  54.        
  55.         getTree(t1,n1);
  56.         getTree(t2,n2);
  57.         printf("%s\n", eq(0,0)? "true":"false");
  58.     }
  59.    
  60.     return 0;
  61. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement