uva122

25 篇文章 0 订阅
4 篇文章 0 订阅
#include <iostream>
#include <stdio.h>
#include <vector>
#include <queue>
#include <string.h>
using namespace std;
struct Node
{
    int data;
    bool vis;
    Node* ls;
    Node* rs;
    Node()
    {
        vis=false;
        ls=NULL;
        rs=NULL;
    }
};
char s[300];
Node *root;
bool flag;
void addnode()
{
    Node * p=root;
    int data_;
    sscanf(&s[1],"%d",&data_);//必须从第一个是数字的算起
    int i;
    for(i=2; s[i]!='L' && s[i]!='R' && s[i]!=')'; i++);
    if(s[i]==')')
    {
        root->data=data_;
        root->vis=true;
        return ;
    }
    for(; s[i]!=')'; i++)
    {
        if(s[i]=='L')
        {
            if(p->ls==NULL)
                p->ls=new Node();
            p=p->ls;
        }
        else
        {
            if(p->rs==NULL)
                p->rs=new Node();
            p=p->rs;
        }
    }
    if(p->vis) flag=false;
    p->data=data_;
    p->vis=true;
}
vector<int> ans;
queue<Node*> q;
int read()
{
    while(!q.empty())
        q.pop();
    ans.clear();
    flag=true;
    root = new Node();
    if(scanf("%s",s)==0 || strcmp(s,"()")==0) return 0;
    addnode();
    while(scanf("%s",s) && strcmp(s,"()")!=0)
        addnode();
    return 1;
}

bool bfs()
{
    Node*tmp;
    q.push(root);
    while(!q.empty())//如果程序在中间就return了,q就不为空!
    {
        tmp=q.front();
        if(tmp->ls!=NULL)
        {
            if(tmp->vis)
                q.push(tmp->ls);
            else
                return false;
        }
        if(tmp->rs!=NULL)
        {
            if(tmp->vis)
                q.push(tmp->rs);
            else
                return false;
        }
        ans.push_back(tmp->data);
        q.pop();
    }
    return true;
}
int main()
{
    while(read())
    {
        if(!bfs() || !flag)
            printf("not complete\n");
        else
        {
            for(int i=0; i<ans.size(); i++)
                printf("%d%c",ans[i],i==ans.size()-1?'\n':' ');
        }
    }
    return 0;
}
/*
(129,) (399,R) (556,L) (77,RL) ()
(382,RLLLRL) (641,RLL) (571,R) (460,RLLLRLL) (335,RL) (643,RLLLRR) (797,RLLLR) (309,RLLLL) ()
*/

这题TLE原因是q未清空,后改正AC

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值