ACM篇:Uva 122 -- Trees on the level
来源:互联网 发布:jsp端口更改 编辑:程序博客网 时间:2024/04/30 14:27
输出末尾不能有空格。
坑。
#include <iostream>#include <cstdio>#include <cstdlib>#include <algorithm>#include <cstring>#include <queue>#include <vector>using namespace std;const int MAXN = 128;char s[MAXN];bool failed;struct Node{ bool have_value; int val; Node *left; Node *right; Node() : have_value(false), left(NULL), right(NULL){}};Node *root = NULL;void add_node(int val, char *str){ Node *cur = root; while (*str != ')' && *str) { if (*str == 'L') { if (!cur->left) cur->left = new Node(); cur = cur->left; } else if (*str == 'R') { if (!cur->right) cur->right = new Node(); cur = cur->right; } str++; } if (cur->have_value) failed = true; cur->have_value = true; cur->val = val;}void remove_node(Node *u){ if (!u) return; remove_node(u->left); remove_node(u->right); delete u;}bool read_input(){ failed = false; remove_node(root); root = new Node(); while (true) { if (scanf("%s", s) != 1) return false; if (!strcmp(s, "()")) return true; int v; sscanf(s+1, "%d", &v); add_node(v, strchr(s, ',') + 1); }}void _print(){ queue<Node*> q; vector<int>ans; q.push(root); while (!q.empty() && !failed) { Node *head = q.front(); q.pop(); if (head->left) q.push(head->left); if (head->right) q.push(head->right); if (!head->have_value) failed = true; ans.push_back(head->val); } if (failed) printf("not complete\n"); else { int sz = ans.size(); for (int i = 0; i < sz; i++) printf("%d%s", ans[i], (i == sz-1) ? "\n" : " "); }}int main(){ while (read_input()) _print(); return 0;}
0 0
- ACM篇:Uva 122 -- Trees on the level
- UVa Problem 122 - Trees on the level
- UVA 122 - Trees on the level
- uva 122 - Trees on the level
- uva 122 Trees on the level
- UVA-122-Trees on the level
- UVA - 122 Trees on the level
- UVa 122 - Trees on the level
- UVa 122 - Trees on the level
- Uva 122 Trees on the level
- UVA - 122 Trees on the level
- UVa 122 - Trees on the level
- UVA - 122 Trees on the level Map
- Uva - 122 - Trees on the level
- uva 122 - Trees on the level
- UVa 122 Trees on the level
- uva 122 Trees on the level
- UVa 122 - Trees on the level
- 20170109Python学习规划
- java基础黑马程序员经典题目
- Java 文件上传 以及 springmvc实现文件上传
- Spring中的事务管理
- C 数据结构 大中小三种轮渡计算过河时间问题实现
- ACM篇:Uva 122 -- Trees on the level
- 深入理解Java之线程池
- java环境变量配置
- Python中__builtin__模块和__builtins__模块的区别和联系
- android_53_进程优先级
- Problem A. Password Attacker
- web.xml中配置session属性
- Unity优化知识小结
- PHP制作分页函数(上)