• sdut 2482 二叉排序树


    题目:http://acm.sdut.edu.cn/sdutoj/problem.php?action=showproblem&problemid=2482

    感觉树这一部分掌握的真心不好,以前练习的一道题,当时不会,今天参考了一下别人的代码,出了点错误,但终于A了。。。。。

     1 #include <iostream>
     2 #include <cstring>
     3 #include <cstdlib>
     5 using namespace std;
     6 
     7 typedef struct node
     8 {
     9     char data;
    10     struct node *l,*r;
    11 }tree;
    12 tree *head,*p2;
    13 char s[20],s1[20];
    14 int x1,y1;
    15 
    16 void cre(tree *q,char ch)
    17 {
    18     if(q->data<ch)
    19     {
    20         if(q->r==NULL)
    21         {
    22             p2=new tree;
    23             p2->data=ch;
    24             p2->l=NULL;
    25             p2->r=NULL;
    26             q->r=p2;
    27         }
    28         else
    29         cre(q->r,ch);
    30     }
    31     else
    32     {
    33         if(q->l==NULL)
    34         {
    35             p2=new tree;
    36             p2->data=ch;
    37             p2->l=NULL;
    38             p2->r=NULL;
    39             q->l=p2;
    40         }
    41         else
    42         cre(q->l,ch);
    43     }
    44 }
    45 void bulid(char *s)
    46 {
    47     int len=strlen(s);
    48     head=new tree;
    49     head->data=*s;
    50     head->l=NULL;
    51     head->r=NULL;
    52     s++; len--;
    53     while(len--)
    54     {
    55         cre(head,*s);
    56         s++;
    57     }
    58 }
    59 
    60 void cal(tree *q,char s[],int count)
    61 {
    62     if(q)
    63     {
    64         s[count++]=q->data;
    65         cal(q->l,s,count);
    66         cal(q->r,s,count);
    67     }
    68 }
    69 int main()
    70 {
    71     int n;
    72     while(cin>>n&&n)
    73     {
    74         x1=0;
    75         cin>>s;
    76         bulid(s);
    77         memset(s,0,sizeof(s));
    78         cal(head,s,x1);
    79         while(n--)
    80         {
    81             y1=0;
    82             cin>>s1;
    83             bulid(s1);
    84             memset(s1,0,sizeof(s1));
    85             cal(head,s1,y1);
    86             if(strcmp(s,s1)==0)
    87             cout<<"YES"<<endl;
    88             else
    89             cout<<"NO"<<endl;
    90         }
    91     }
    92     return 0;
    93 }
  • 相关阅读:
    指针和数组的关系
    深入学习数组
    const关键字与指针
    野指针是什么
    指针带来的一些符号的理解
    指针的本质
    内存管理之堆
    内存管理之栈
    元类
    断点调式和面向对象进阶
  • 原文地址:https://www.cnblogs.com/bfshm/p/3466145.html
Copyright © 2020-2023  润新知