UVA122 Trees on the level

53人阅读 评论(0)

题目链接戳这里

/*  UVA 122
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <queue>
#include <vector>
#include <cmath>
#include <stack>
#include <string>
#include <map>
#include <set>
#define pi acos(-1.0)
#define LL long long
#define ULL unsigned long long
#define inf 0x3f3f3f3f
#define INF 1e18
//#define lson l,mid,rt<<1
//#define rson mid+1,r,rt<<1|1
#define debug(a) printf("---%d---\n", a);
#define mem0(a) memset(a, 0, sizeof(a))
#define memi(a) memset(a, inf, sizeof(a))
#define mem1(a) memset(a, -1, sizeof(a))
using namespace std;
typedef pair<int, int> P;
const double eps = 1e-10;
const int maxn = 1e6 + 5;
const int mod = 1e8;

int failed, node;
struct Node{
int val, flag;
Node* lson;
Node* rson;
}tree[maxn];
Node* NewNode(){
Node* p = &tree[node++];
p->flag = p->val = 0;
p->lson = p->rson = NULL;
return p;
}
void addnode(Node* root, int v, char* s){
Node* p = root;
for (int i = 0; i < strlen(s); i++){ // 从根节点往下走
if (s[i] == 'L'){
if (p->lson == NULL) p->lson = NewNode(); // 结点不存在,建立新结点
p = p->lson;   // 往左走
}
else if (s[i] == 'R'){
if (p->rson == NULL) p->rson = NewNode();
p = p->rson;
}
}
if (p->flag) failed = 1; // 若此结点已经被赋值 failed
p->val = v;
p->flag = 1;
}
void bfs(Node* root, vector<int>* ans)
{
queue<Node*> q;
q.push(root);
while (!q.empty()){
Node* p = q.front(); q.pop();
if (p->flag == 0){
failed = 1;
}
(*ans).push_back(p->val);
if (p->lson != NULL) q.push(p->lson);
if (p->rson != NULL) q.push(p->rson);
}
}
int main(void)
{
//  freopen("C:\\Users\\wave\\Desktop\\NULL.exe\\NULL\\in.txt","r", stdin);
char s[35], str[35];
int v, i;
while (~scanf("%s", s))
{
failed = node = 0;
Node* root = NewNode();  // 根节点
if (strcmp(s, "()") != 0){
sscanf(s, "(%d,%s", &v, str); // 一种写法
}else continue;
while (~scanf("%s", s)){
if (strcmp(s, "()") == 0) break;
sscanf(&s[1], "%d", &v); // 另一种写法
}
vector<int> ans;
if (!failed) bfs(root, &ans);
if (failed) puts("not complete");
else {
for (i = 0; i < ans.size(); i++)
printf("%d%c", ans[i], i+1==ans.size()?'\n':' ');
}
}
return 0;
}

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <queue>
#include <vector>
#include <cmath>
#include <stack>
#include <string>
#include <map>
#include <set>
#define pi acos(-1.0)
#define LL long long
#define ULL unsigned long long
#define inf 0x3f3f3f3f
#define INF 1e18
//#define lson l,mid,rt<<1
//#define rson mid+1,r,rt<<1|1
#define debug(a) printf("---%d---\n", a)
#define mem0(a) memset(a, 0, sizeof(a))
#define memi(a) memset(a, inf, sizeof(a))
#define mem1(a) memset(a, -1, sizeof(a))
using namespace std;
typedef pair<int, int> P;
const double eps = 1e-10;
const int maxn = 1e6 + 5;
const int mod = 1e8;

int failed, node;
struct Node{
int val, flag;
int lson, rson;
}tree[maxn];
int NewNode(){
tree[++node].val = 0;
tree[node].flag = 0;
tree[node].lson = 0;
tree[node].rson = 0;
return node;
}
void addnode(int rt, int v, char* s){
for (int i = 0; i < strlen(s); i++){
if (s[i] == 'L'){
if (!tree[rt].lson)
tree[rt].lson = NewNode();
rt = tree[rt].lson;
}
else if (s[i] == 'R'){
if (!tree[rt].rson)
tree[rt].rson = NewNode();
rt = tree[rt].rson;
}
}
if (tree[rt].flag) failed = 1;
tree[rt].val = v;
tree[rt].flag = 1;
}
vector<int> bfs(int rt){
queue<int> q;
q.push(rt);
vector<int> ans;
while (!q.empty()){
int u = q.front(); q.pop();
if (tree[u].flag == 0){
failed = 1;
break;
}
ans.push_back(tree[u].val);
if (tree[u].lson) q.push(tree[u].lson);
if (tree[u].rson) q.push(tree[u].rson);
}
return ans;
}
int main(void)
{
//  freopen("C:\\Users\\wave\\Desktop\\NULL.exe\\NULL\\in.txt","r", stdin);
char s[35], str[35];
int v, i;
while (~scanf("%s", s))
{
failed = node = 0;
int root = NewNode();    // 根节点
if (strcmp(s, "()") != 0){
sscanf(s, "(%d,%s", &v, str); // 一种写法
}else continue;
while (~scanf("%s", s)){
if (strcmp(s, "()") == 0) break;
sscanf(&s[1], "%d", &v); // 另一种写法
}
vector<int> ans;
if (!failed) ans = bfs(root);
if (failed) puts("not complete");
else {
for (i = 0; i < ans.size(); i++)
printf("%d%c", ans[i], i+1==ans.size()?'\n':' ');
}
}
return 0;
}
0
0

* 以上用户言论只代表其个人观点，不代表CSDN网站的观点或立场
个人资料
• 访问：7997次
• 积分：1150
• 等级：
• 排名：千里之外
• 原创：112篇
• 转载：0篇
• 译文：0篇
• 评论：8条
阅读排行
评论排行