比赛 |
20160407树结构练习 |
评测结果 |
TTTTTTTTTT |
题目名称 |
树的层次遍历 |
最终得分 |
0 |
用户昵称 |
ZXCVBNM_1 |
运行时间 |
10.049 s |
代码语言 |
C++ |
内存使用 |
0.26 MiB |
提交时间 |
2016-04-07 20:58:27 |
显示代码纯文本
#include<bits/stdc++.h>
using namespace std;
#define MAXN 310
int val[MAXN],L[MAXN],R[MAXN],q[MAXN],prey[MAXN];
bool vis[MAXN];
int main()
{
freopen("vlevel.in","r",stdin);
freopen("vlevel.out","w",stdout);
int pd,sum,now,SIZE,lr,lp,head,tail,u,i;
char ch,last;
while(1)
{
last=0;
sum=0;
now=1;
SIZE=1;
memset(val,-1,sizeof(val));
memset(L,0,sizeof(L));
memset(R,0,sizeof(R));
pd=0;
while(1)
{
ch=getchar();
if(ch==' '||ch==','){last=ch;continue;}
if(ch==')'&&last=='(')break;
if(ch>='0'&&ch<='9'){sum=sum*10+(ch-'0');continue;}
//if(ch!='L'&&ch!='R'&&(!(ch>='0'&&ch<='9'))&&ch!=' '&&ch!=',')sum=0;
if(ch=='L'||ch=='R')
{
lr=0;
if(ch=='L')
{
if(L[now]==0)L[now]=++SIZE;
now=L[now];
}
if(ch=='R')
{
if(R[now]==0)R[now]=++SIZE;
now=R[now];
}
}
else
{
if(sum!=0)
{
if(val[now]!=-1)pd=1;
val[now]=sum;
sum=0;
}
now=1;
}
last=ch;
}
if(val[now]==-1)pd=1;
if(pd==1){printf("not complete\n");continue;}
head=0;tail=1;q[tail]=now;
memset(vis,false,sizeof(vis));vis[now]=true;
lp=0;
while(head!=tail)
{
head++;if(head==300)head=0;
u=q[head];
prey[++lp]=val[u];
if(val[u]==-1)pd=1;
if(L[u]!=0)
{
if(vis[L[u]]==false)
{
vis[L[u]]=true;
tail++;if(tail==300)tail=0;
q[tail]=L[u];
}
}
if(R[u]!=0)
{
if(vis[R[u]]==false)
{
vis[R[u]]=true;
tail++;if(tail==300)tail=0;
q[tail]=R[u];
}
}
vis[u]=false;
}
if(pd==1){printf("not complete\n");continue;}
else
{
for(i=1;i<lp;i++)printf("%d ",prey[i]);
printf("%d\n",prey[lp]);
}
}
fclose(stdin);
fclose(stdout);
return 0;
}