zoukankan      html  css  js  c++  java
  • 九度oj 题目1009:二叉搜索树

    题目1009:二叉搜索树

    时间限制:1 秒

    内存限制:32 兆

    特殊判题:

    提交:5733

    解决:2538

    题目描述:
    判断两序列是否为同一二叉搜索树序列
    输入:
    开始一个数n,(1<=n<=20) 表示有n个需要判断,n= 0 的时候输入结束。
    接下去一行是一个序列,序列长度小于10,包含(0~9)的数字,没有重复数字,根据这个序列可以构造出一颗二叉搜索树。
    接下去的n行有n个序列,每个序列格式跟第一个序列一样,请判断这两个序列是否能组成同一颗二叉搜索树。
    输出:

    如果序列相同则输出YES,否则输出NO

    样例输入:
    2
    567432
    543267
    576342
    0
    样例输出:
    YES
    NO
    来源:
    2010年浙江大学计算机及软件工程研究生机试真题
    静态链表,建树对比。
     1 #include <cstdio>
     2 #include<algorithm>
     3 #include<iostream>
     4 #include<string>
     5 #include<cstring>
     6 #include<vector>
     7 using namespace std;
     8 char tree[1500],t[1500];
     9 int main(){
    10     //freopen("D://INPUT.txt","r",stdin);
    11     int n;
    12     //cout<<n<<endl;
    13     string s;
    14     while(cin>>n&&n){
    15         cin>>s;
    16         //build(tree,s);
    17         memset(tree,'-',sizeof(tree));
    18         int i=0;
    19         int p;
    20         while(i<s.length()){
    21         p=1;
    22         while(tree[p]!='-'){
    23             if(tree[p]>s[i]){
    24                 p=p*2;
    25             }
    26             else{
    27                 if(tree[p]<s[i]){
    28                    p=p*2+1;
    29                 }
    30             }
    31         }
    32         tree[p]=s[i++];
    33         }
    34         while(n--){
    35             cin>>s;
    36             memset(t,'-',sizeof(t));
    37             i=0;
    38             while(i<s.length()){
    39                   p=1;
    40                   while(t[p]!='-'){
    41                        if(t[p]>s[i]){
    42                               p=p*2;
    43                        }
    44                        else{
    45                            if(t[p]<s[i]){
    46                               p=p*2+1;
    47                        }
    48                        }
    49                   }
    50                   t[p]=s[i++];
    51             }
    52             if(!strcmp(t,tree)){
    53                 cout<<"YES"<<endl;
    54             }
    55             else{
    56                 cout<<"NO"<<endl;
    57             }
    58         }
    59     }
    60     return 0;
    61 }
  • 相关阅读:
    凸包模板
    1060E Sergey and Subway(思维题,dfs)
    1060D Social Circles(贪心)
    D
    牛客国庆集训派对Day2
    网络流
    Tarjan算法(缩点)
    莫队分块算法
    计算几何
    hdu5943素数间隙与二分匹配
  • 原文地址:https://www.cnblogs.com/Deribs4/p/4397276.html
Copyright © 2011-2022 走看看