[UVa 122] Trees On the Level

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 坑爹输出格式!!!输出文件的结尾必须有一个空行!!!

转载于:https://www.cnblogs.com/Corona09/p/9532141.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值