算法竞赛入门经典第六章例题6-7 Trees on the level UVA - 122
2018-01-21 20:45
585 查看
https://vjudge.net/problem/UVA-122
#include<iostream> #include<string> #include<deque> #include<cstring> using namespace std; #pragma warning(disable:4996) struct node { int data; int l, r; int cnt; }; node all[500]; char ss[500]; int d,top; int main() { #ifdef _DEBUG //freopen("in", "r", stdin); //freopen("out", "wb", stdout); #endif // _DEBUG string s;bool _t = true; while (cin>>s) { if (s == "()") { deque<int> que,ans; que.push_back(0); if (all[0].cnt != 1) _t = false; while (!que.empty()) { if (!_t) break; ans.push_back(que.front()); que.pop_front(); if (all[ans.back()].l) { if (all[all[ans.back()].l].cnt != 1) _t = false; que.push_back(all[ans.back()].l); } if (all[ans.back()].r) { if (all[all[ans.back()].r].cnt != 1) _t = false; que.push_back(all[ans.back()].r); } } if (_t) { int flag = 0; for (auto x : ans) { if (flag++) printf(" %d", all[x].data); else printf("%d", all[x].data); } printf("\n"); } else printf("not complete\n"); memset(all, 0, sizeof(all)); _t = true; top = 0; } else { s[s.find(',')] = ' '; sscanf(s.c_str()+1,"%d %s", &d, ss); int Len = strlen(ss),p=0; for (int i = 0; i < Len-1; ++i) { if (ss[i] == 'L') { if (all[p].l) p = all[p].l; else p = all[p].l = ++top; } else { if (all[p].r) p = all[p].r; else p = all[p].r = ++top; } } all[p].data = d, ++all[p].cnt; } } }
相关文章推荐
- 例题6-7 UVa122 Trees on the level(树&&队列BFS&&sscanf字符串转整数)
- 例题6-7 二叉树的层次遍历 UVa 122 Trees on the level
- 【例题 6-7 UVA - 122 】Trees on the level
- 算法竞赛入门经典第六章例题6-3 Matrix Chain Multiplication UVA - 442
- 例题6-7 UVA 122 - Trees on the level 树的层次遍历
- 算法竞赛入门经典第五章例题5-5 The SetStack Computer UVA - 12096
- 例题6-7 树的层次遍历(Trees on the level, Duke 1993, UVa 122)
- uva10562 - Undraw the Trees 入门经典II 第六章数据结构基础 例题6-17
- 算法竞赛入门经典 第二版 例题11-4 电话圈 Calling Circles uva247
- uva 122 Trees on the level
- UVa 122 Trees on the level(链式二叉树的建立和层次遍历)
- UVA122 trees on the level(BFS)
- (二叉树)UVA - 122 Trees on the level
- UVa 122 - Trees on the level
- UVA122 Trees on the level【二叉树】【BFS】
- UVa122:Trees on the level
- UVA - 122 Trees on the level
- 算法竞赛入门经典第四章例题4-2 Hangman Judge UVA - 489
- UVa 133 算法竞赛入门经典4-3例题(约瑟夫环类型)
- 刘汝佳 算法竞赛-入门经典 第二部分 算法篇 第六章 2(Binary Trees)