C   70
struct node
Guest on 11th February 2023 01:27:09 AM


  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. struct node{
  4.     struct node* left;
  5.     char c;
  6.     struct node* right;
  7. };
  8. char preo[55],ino[55];
  9. int num[55];
  10. int p;
  11. int len;
  12. void en(){
  13.     int i,j;
  14.     for(i=0;i<len;i++)
  15.         for(j=0;j<len;j++)
  16.             if(preo[i]==ino[j]){
  17.                 num[i]=j;
  18.                 break;
  19.             }
  20.         /*num[i] = 10000;*/
  21.     return;
  22. }
  23. void erect(struct node* m,int w,int l){
  24.         struct node* t;
  25.     if(num[p+1]<w){
  26.         p++;
  27.         m->left=(struct node*)malloc(sizeof(struct node));
  28.         t=m->left;
  29.         t->c=preo[p];
  30.         t->left=t->right=NULL;
  31.         erect(t,num[p],w);
  32.     }
  33.     if(num[p+1]<l){
  34.         p++;
  35.         m->right=(struct node*)malloc(sizeof(struct node));
  36.         t=m->right;
  37.         t->c=preo[p];
  38.         t->left=t->right=NULL;
  39.         erect(t,num[p],l);
  40.     }
  41.     return;
  42. }
  43. void visit(struct node* m){
  44.     if(m){
  45.         if(m->left)visit(m->left);
  46.         if(m->right)visit(m->right);
  47.         if(m->c)printf("%c",m->c);
  48.     }
  49.     return;
  50. }
  51. struct node* root;
  52. int main(){
  53.     int n;
  54.     scanf("%d",&n);
  55.     while(n--){
  56.         scanf("%d %s %s",&len,preo,ino);
  57.         en();
  58.         root=(struct node*)malloc(sizeof(struct node));
  59.         root->c=preo[0];
  60.         root->right=root->left=NULL;
  61.         p=0;
  62.         erect(root,num[0],len);
  63.         visit(root);
  64.         printf("\n");
  65.         free(root);
  66.     }
  67.     return 0;
  68. }

Raw Paste

Login or Register to edit or fork this paste. It's free.