zoukankan      html  css  js  c++  java
  • HDU 1622 Trees on the level

    因为已经知道了每个点的路径,所以不需要建树,不需要广搜,直接以路径为关键字对节点进行排序。

    排序之后就是答案了。如果树不合法,或者一条路出现多次,或者没有根节点,输出not complete。

    /*
    HDU 1622
    给出一颗二叉树,输出层次遍历的结果
    层次遍历的顺序就是广搜的顺序
    因此,先建树,再从根节点出发进行一次BFS
    但由于题目已经给出了根节点到每个节点的路径
    所以只需以路径为关键字对节点进行排序,排序之后的结果就是答案
    如果树不合法,或者一条路出现多次,或者没有根节点,输出not complete
    */
    
    #include<stdio.h>
    #include<string.h>
    #include<string>
    #include<iostream>
    #include<map>
    #include<algorithm>
    using namespace std;
    
    struct Node
    {
        string a;
        string p;
    } s[300];
    string tmp;
    int tot;
    
    //按这个规则对节点进行排序
    bool cmp(const Node&a, const Node&b)
    {
        if (a.p.size() == b.p.size()) return a.p<b.p;
        return a.p.size()<b.p.size();
    }
    
    //增加一个节点
    void add()
    {
        string id; id.clear();
        string path; path.clear();
    
        for (int i = 0; i < tmp.size(); i++)
            if (tmp[i] >= '0'&&tmp[i] <= '9')
                id = id + tmp[i];
    
        for (int i = 0; i<tmp.size(); i++)
            if (tmp[i] == 'L' || tmp[i] == 'R')
                path = path + tmp[i];
    
        s[tot].a = id;
        s[tot].p = path;
        tot++;
    }
    
    int main()
    {
        int T = 0;
        while (cin >> tmp)
        {
            tot = 0;
            if (tmp == "()") break;
            add();
    
            while (1)
            {
                cin >> tmp;
                if (tmp == "()") break; 
                add();
            }
    
            sort(s, s + tot, cmp);
    
            bool fail = 0;
    
            if (s[0].p.length()!=0) fail = 1;//如果第一个节点不是根节点,输出not complete
            
            //检查树是否合法
            if (fail == 0)
            {
                for (int i = 1; i < tot; i++)
                {
                    //判断到这个点的路径之前是否出现过
                    bool Find = 0;
                    for (int k = 0; k < i; k++)
                        if (s[k].p == s[i].p) Find = 1;
    
                    if (Find == 1) { fail = 1; break; }
    
                    //判断到这个点的路径是否存在
                    string pa = "";
                    for (int k = 0; k < s[i].p.size() - 1; k++)
                        pa = pa + s[i].p[k];
                    Find = 0;
                    for (int k = 0; k < i; k++)
                        if (s[k].p == pa) Find = 1;
                    if (Find == 0) {fail = 1; break;}
                }
            }
    
            //输出
            if (fail == 0)
            {
                for (int i = 0; i<tot; i++)
                {
                    cout << s[i].a;
                    if (i<tot - 1) printf(" ");
                    else printf("
    ");
                }
            }
            else printf("not complete
    ");
        }
        return 0;
    }
  • 相关阅读:
    设置sqlplus输出格式
    Using CrunchBase API
    Docker ( Is docker really better than VM ?)
    Cross platform GUI for creating SSL certs with OpenSSL
    PHP, Python Nginx works together!
    Your personal Mail Server iRedMail on ubuntu14.04 x64
    iphone/ipad/iOS on Linux Debian7/ubuntu12.04/linuxmint13/ubuntu14.04 compiling from source
    Internet Liberity -- a specific anonymous internet guide
    Organic Solar Cells
    Organic Solar Cells
  • 原文地址:https://www.cnblogs.com/zufezzt/p/5442997.html
Copyright © 2011-2022 走看看