zoukankan      html  css  js  c++  java
  • 7-20 Binary Search Tree (25分)

    A binary search tree is uniquely determined by a given ordered insertions of a sequence of positive integers. On the other hand, a given binary search tree may correspond to several different insertion sequences. Now given several insertion sequences, it is your job to determine if they can generate the same binary search tree.

    Input Specification:

    Input consists of several test cases. For each test case, the first line contains two positive integers N (≤10) and L, which are the total number of integers in an insertion sequence and the number of sequences to be tested, respectively. The second line contains N positive integers, separated by a space, which are the initially inserted numbers. Then L lines follow, each contains a sequence of N integers to be checked.

    For convenience, it is guaranteed that each insertion sequence is a permutation of integers 1 to N - that is, all the N numbers are distinct and no greater than N. The input ends with N being 0. That case must NOT be processed.

    Output Specification:

    For each test case, output to the standard output. Print in L lines the checking results: if the sequence given in the i-th line corresponds to the same binary search tree as the initial sequence, output to the i-th line Yes; else output No.

    Sample Input:

    4 2
    3 1 4 2
    3 4 1 2
    3 2 4 1
    2 1
    2 1
    1 2
    0
    
     

    Sample Output:

    Yes
    No
    No

    用数组建树,模仿字典树的建树过程这样结点都在数组中连续,然后对于测试序列,从根节点开始比较如果当前结点标记过了,则用测试结点与之比较确定往左还是往右比较,如果没标记过,直接比较是否相等。
    代码:
    #include <cstdio>
    #include <cstring>
    using namespace std;
    
    int trie[11][2],v[11],vis[11],ind;
    void insert(int d) {
        if(ind == 0) {
            v[ind ++] = d; 
        }
        else {
            int x = 0;
            while(v[x] != d) {
                if(d < v[x]) {
                    if(!trie[x][0]) {
                        trie[x][0] = ind;
                        v[ind ++] = d;
                    }
                    x = trie[x][0];
                }
                else {
                    if(!trie[x][1]) {
                        trie[x][1] = ind;
                        v[ind ++] = d;
                    }
                    x = trie[x][1];
                }
            }
        }
    }
    bool check(int x,int d) {
        if(!vis[x]) {
            if(v[x] == d) {
                vis[x] = 1;
                return true;
            }
            return false;
        }
        return d < v[x] ? check(trie[x][0],d) : check(trie[x][1],d);
    }
    int main() {
        int n,l,d,flag;
        while(scanf("%d",&n) && n) {
            scanf("%d",&l);
            ind = 0;
            memset(trie,0,sizeof(trie));
            for(int i = 0;i < n;i ++) {
                scanf("%d",&d);
                insert(d);
            }
            for(int i = 0;i < l;i ++) {
                flag = 1;
                memset(vis,0,sizeof(vis));
                for(int j = 0;j < n;j ++) {
                    scanf("%d",&d);
                    flag &= check(0,d);
                }
                puts(flag ? "Yes" : "No");
            }
        }
    }
  • 相关阅读:
    Sql server
    分布式爬虫
    爬取某电影网站最新电影
    随笔写一个简单的爬虫
    python的os模块
    基于python的文件处理
    mysql习惯及主从复制参数设置
    git常用命令
    git实习笔记
    微信公众平台开发接口PHP SDK完整版
  • 原文地址:https://www.cnblogs.com/8023spz/p/12271964.html
Copyright © 2011-2022 走看看