hdu 1622 Trees on the level

原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=1622
小白书上的题。。。

#include<algorithm>
#include<iostream>
#include<cstdlib>
#include<cstdio>
#include<vector>
#include<queue>
using std::queue;
using std::vector;
const int Max_N = 260;
struct Node {
    int v, vis;
    Node *ch[2];
    inline void set(int _v, Node *p) {
        vis = 0, v = _v;
        ch[0] = ch[1] = p;
    }
};
struct BinTree {
    int fail;
    char buf[Max_N];
    Node *tail, *root, stack[Max_N];
    void init() {
        fail = 0;
        tail = &stack[0];
    }
    inline Node *newNode(int v = 0) {
        Node *p = tail++;
        p->set(v, NULL);
        return p;
    }
    inline void insert(const char *src, const int v) {
        int n = strlen(src);
        Node *x = root;
        for (int i = 0; i < n; i++) {
            if (src[i] == 'L') {
                if (!x->ch[0]) x->ch[0] = newNode();
                    x = x->ch[0];
            } else if (src[i] == 'R') {
                if (!x->ch[1]) x->ch[1] = newNode();
                x = x->ch[1];
            }
        }
        if (x->vis) fail = 1;
        x->v = v;
        x->vis = 1;
    }
    inline void bfs() {
        vector<int> ans;
        queue<Node *> que;
        que.push(root);
        while (!que.empty()) {
            Node *u = que.front(); que.pop();
            if (!u->vis) {
                fail = 1;
                break;
            }
            ans.push_back(u->v);
            if (u->ch[0]) que.push(u->ch[0]);
            if (u->ch[1]) que.push(u->ch[1]);
        }
        if (fail) {
            puts("not complete");
            return;
        }
        int n = ans.size();
        for (int i = 0; i < n; i++) {
            printf("%d%c", ans[i], i < n - 1 ? ' ' : '\n');
        }
    }
    inline int gogo() {
        init();
        int v = 0;
        root = newNode();
        for (;;) {
            if (scanf("%s", buf) != 1) return 0;
            if (!strcmp(buf, "()")) break;
            sscanf(&buf[1], "%d", &v);
            insert(strchr(buf, ',') + 1, v);
        }
        bfs();
        return 1;
    }
}tree;
int main() {
#ifdef LOCAL
    freopen("in.txt", "r", stdin);
    freopen("out.txt", "w+", stdout);
#endif
    while (tree.gogo());
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值