HOJ 1168 Trees on the level

HOJ 1168 Trees on the level
给出一个图看是不是一个二叉树
这可是我第一次用gedit编程,g++ gdb编译运行的程序,操作有点麻烦,不过好处是 输入可以直接粘贴方便很多。


#include  < cstdio >
#include 
< string >
#include 
< algorithm >
#include 
< map >

using   namespace  std;

struct  Node
{
    
char  name[ 1000 ],num[ 1000 ];
}node[
500 ];
map 
< string int >  huhupao;                                  //方便寻找某一个节点的父节点是否存在
int  end;
char  temp_end[ 1000 ];

bool  cmp(Node a1,Node a2)
{
    
int  len1,len2;
    len1 
=  strlen(a1.name);
    len2 
=  strlen(a2.name);
    
if (len1  !=  len2)  return  len1  <  len2;
    
return  strcmp(a1.name, a2.name)  <   0 ;
}
void  output()
{
    sort(node,node 
+  end,cmp);                                                    //按要求排序
    
int  i,j,len,flag  =   0 ;
    
char  temp[ 1000 ];
    
    
for (i  =   0 ; i  <  end; i ++ )
    {
        
if (i  &&  strcmp(node[i].name, node[i  -   1 ].name)  ==   0 )
        {
            flag 
=   1 ;
            
break ;
        }
        strcpy(temp,node[i].name);
        len 
=  strlen(temp);
        
if (len  >   1 )
        {
            temp[len 
-   1 =   ' \0 ' ;
            
string  temp1(temp);
            
if ( ! huhupao[temp1])
            {
                flag 
=   1 ;
                
break ;
            }
        }
        
if (flag)  break ;
    }
    
if (flag) {
        printf(
" not complete\n " );
        
return ;
    }
    printf(
" %s " ,temp_end);
    
for (i  =   0 ; i  <  end; i ++ ){
        printf(
"   " );
        printf(
" %s " ,node[i].num);
    }
    printf(
" \n " );
}
int  main()
{
    
char   in [ 1000 ];
    
int  i,j,head  =   0 ;
    
    end 
=   0 ;
    
while (scanf( "  %s " , in ==   1 )
    {
        
if (strcmp( in , " () " ==   0 )
        {
            
if (head  !=   1
            {
                printf(
" not complete\n " );
                end 
=   0 ;
                head 
=   0 ;
                huhupao.clear();
                
continue ;
            }
            
            output();    
            huhupao.clear();
            end 
=   0 ;
            head 
=   0 ;
        }
        
else
        {
            
int  len;
            len 
=  strlen( in );
            len
-- ;
            
for (i  =   0 ; i  <  len ;i ++ if ( in [i]  ==   ' , ' break ;
            
int  k  =   0 ;
            
for (j  =   1 ; j  <  i; j ++ ) node[end].num[k ++ =   in [j];
            node[end].num[k] 
=   ' \0 ' ;
            k 
=   0 ;
            
for (j  =  i  +   1 ; j  <  len; j ++ ) node[end].name[k ++ =   in [j];
            
if (j  ==  i  +   1 )              //'\0'好像没法往map里存,只好手动存一下,存储根节点
            {
                head
++ ;
                strcpy(temp_end,node[end].num);
            }
            
else  
            {
                node[end].name[k] 
=   ' \0 ' ;
                huhupao[
string (node[end].name)]  =   1 ;
                end
++ ;
            }
        }
    }
    
return   0 ;
}

你可能感兴趣的:(HOJ 1168 Trees on the level)