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 = ;
struct Node {
int v, vis;
Node *ch[];
inline void set(int _v, Node *p) {
vis = , v = _v;
ch[] = ch[] = p;
}
};
struct BinTree {
int fail;
char buf[Max_N];
Node *tail, *root, stack[Max_N];
void init() {
fail = ;
tail = &stack[];
}
inline Node *newNode(int v = ) {
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 = ; i < n; i++) {
if (src[i] == 'L') {
if (!x->ch[]) x->ch[] = newNode();
x = x->ch[];
} else if (src[i] == 'R') {
if (!x->ch[]) x->ch[] = newNode();
x = x->ch[];
}
}
if (x->vis) fail = ;
x->v = v;
x->vis = ;
}
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 = ;
break;
}
ans.push_back(u->v);
if (u->ch[]) que.push(u->ch[]);
if (u->ch[]) que.push(u->ch[]);
}
if (fail) {
puts("not complete");
return;
}
int n = ans.size();
for (int i = ; i < n; i++) {
printf("%d%c", ans[i], i < n - ? ' ' : '\n');
}
}
inline int gogo() {
init();
int v = ;
root = newNode();
for (;;) {
if (scanf("%s", buf) != ) return ;
if (!strcmp(buf, "()")) break;
sscanf(&buf[], "%d", &v);
insert(strchr(buf, ',') + , v);
}
bfs();
return ;
}
}tree;
int main() {
#ifdef LOCAL
freopen("in.txt", "r", stdin);
freopen("out.txt", "w+", stdout);
#endif
while (tree.gogo());
return ;
}
上一篇:学习ASP.NET Core Razor 编程系列十八——并发解决方案


下一篇:Mysql中的count()与sum()区别