HDU 1800(字典树)

简单的字典树水题,统计为单词前缀的单词数,取最大,注意要去前0,具体原理还是自己想想吧,最大数决定了最大分组。

 

 

诶 PKU月赛之后 我发现我只会水的 ..............

 

 

#include < iostream >
#include 
< queue >
using   namespace  std;
struct  node
{
    
long  num;
    
int  next[ 10 ];
    
void  init(){memset(next, - 1 , sizeof (next));num = 0 ;}
}s[
200000 ];

int  p;
char  a[ 100 ];
inline 
void  preprocess(){s[p = 0 ].init();}
long  lmax;
inline 
void  insert()
{
    
int  index = 0 ;
 
//    ++s[index].num;
     bool  begin = false ;
    
for ( int  i = 0 ;a[i] != ' \0 ' ; ++ i)
    {
        
if  (a[i] != ' 0 ' )
        {
            begin
= true ;
        }

        
if  ( ! begin)
        {
            
continue ;
        }

        
int  x = a[i] - ' 0 ' ;
        
if (s[index].next[x] ==- 1 )
        {
            s[
++ p].init();
            s[index].next[x]
= p;
        }
        index
= s[index].next[x];
    }
    s[index].num
++ ;
    
if  (s[index].num > lmax)
    {
        lmax
= s[index].num;
    }
    
}

int  main()
{
   
    
    
long  n;
    
while  (scanf( " %ld " , & n) != EOF)
    {
        lmax
= 0 ;
        getchar();
        preprocess();
        
long  i;
        
for  (i = 0 ;i < n; ++ i)
        {
            gets(a);
            insert();
        } 
        
        printf(
" %ld\n " ,lmax);
    }
    

    
    
return   0 ;
}

转载于:https://www.cnblogs.com/zhuangli/archive/2008/09/01/1281042.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值