码迷,mamicode.com
首页 > 其他好文 > 详细

Trees On the Level

时间:2018-10-31 01:16:46      阅读:149      评论:0      收藏:0      [点我收藏+]

标签:als   else   ret   std   int   r++   eof   let   scan   

Trees On the Level


竟然还有 (,) () 这种东西!!!

#include <cstdio>
#include <cstring>
#include <cctype>
const int MAXN=256+1;
char s[MAXN];
struct Node {
    int val;
    bool have;
    Node *left,*right;
    Node(int v=0,Node* l=NULL,Node* r=NULL):val(v),left(l),right(r) {
        have=false;
    }
}*root,*q[MAXN];
int front=0,rear=0;
void printAns(Node* root,int ok) {
    if(ok==0) {
        printf("not complete\n");
        return;
    }
    front=rear=0;
    q[rear++]=root;
    while(front<rear) {
        Node *u=q[front++];
        if(u->have==false) {
            ok=0;
            break;
        }
        if(u->left!=NULL)q[rear++]=u->left;
        if(u->right!=NULL)q[rear++]=u->right;
    }
    if(ok==0) {
        printf("not complete\n");
        return;
    }
    front=0;
    printf("%d",q[front++]->val);
    while(front<rear)printf(" %d",q[front++]->val);
    printf("\n");
}
Node* newNode() {
    return new Node(0,NULL,NULL);
}
void insert(int val,char s[],int& ok) {
    Node *u=root;
    int m=strlen(s);
    for(int i=0; i<m; i++) {
        if(s[i]==‘L‘) {
            if(u->left==NULL)u->left=newNode();
            u=u->left;
        }
        if(s[i]==‘R‘) {
            if(u->right==NULL)u->right=newNode();
            u=u->right;
        }
    }
    if(u->have==true)ok=0;
    else {
        u->val=val;
        u->have=true;
    }
    return;
}
void del(Node* u) {
    if(u->left!=NULL)del(u->left);
    if(u->right!=NULL)del(u->right);
    delete u;
}
int main() {
    int ok=1,val=0;
    root=newNode();
    while(scanf("%s",s)==1) {
        if(!strcmp(s,"()")) {
            printAns(root,ok);
            ok=1;
            del(root);
            root=newNode();
            memset(s,0,sizeof(s));
            memset(q,0,sizeof(q));
        } else {
            if(!isdigit(s[1]))ok=0;
            else sscanf(s+1,"%d",&val);
            int pos=strchr(s,‘,‘)-s+1;
            insert(val,s+pos,ok);
        }
    }
    return 0;
}

2018 年 10 月 30 日,真是一个不幸的日子.

在这一天,我终于把这道题 A 掉了,反反复复提交了十多次,终于在今天,我明白了为什么错:

UVa 坑爹输出格式!!!输出文件的结尾必须有一个空行!!!

Trees On the Level

标签:als   else   ret   std   int   r++   eof   let   scan   

原文地址:https://www.cnblogs.com/Corona09/p/9532141.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!