HDU 2146 (Hash)

本来是用字典树的 用了很多种优化竟然都超时 那就只好用hash了 对所有的串进行重构 化为64位整型 然后使用位运算枚举所有K长串

 

然后用hash存储 使用线性扫描法  还可以优化

 

代码如下:

 

 

 

#include  < iostream >
using   namespace  std;

typedef 
struct   
{
    unsigned __int64 key;
    
long  num;
}Info;

Info hash[
68000 ];
char  key[ 80 ];

__int64 prime;
long  lmax = 0 ;
inline 
void  Dohash(unsigned __int64 key)
{
    
long  pos = key % prime;

    
if  (hash[pos].num == 0 )
    {
        hash[pos].num
= 1 ;
        hash[pos].key
= key;
        
if  (hash[pos].num > lmax)
        {
            lmax
= hash[pos].num;
        }
        
return ;
    }

    
if  (hash[pos].key == key)
    {
        
++ hash[pos].num;
        
if  (hash[pos].num > lmax)
        {
            lmax
= hash[pos].num;
        }
        
return ;
    }

    
long  i,j;
    
long  l;
    
for  (i = 1 ;; ++ i)
    {
        
for  (j = 0 ;j < 2 ; ++ j)
        {
            
if  ( ! (j & 1 ))
            {
                l
= (pos + i) % prime;
                
if  (hash[l].num == 0 )
                {
                    hash[l].num
= 1 ;
                    hash[l].key
= key;
                    
if  (hash[pos].num > lmax)
                    {
                        lmax
= hash[pos].num;
                    }
                    
return ;
                }
                
if  (hash[l].key == key)
                {
                    
++ hash[l].num;
                    
if  (hash[pos].num > lmax)
                    {
                        lmax
= hash[pos].num;
                    }
                    
return ;
                }
            }
            
else
            {
                l
= (pos + prime - i) % prime;
                
if  (hash[l].num == 0 )
                {
                    hash[l].num
= 1 ;
                    hash[l].key
= key;
                    
if  (hash[pos].num > lmax)
                    {
                        lmax
= hash[pos].num;
                    }
                    
return ;
                }
                
if  (hash[l].key == key)
                {
                    
++ hash[l].num;
                    
if  (hash[pos].num > lmax)
                    {
                        lmax
= hash[pos].num;
                    }
                    
return ;
                }
            }
        }
    }
}


int  main()
{    
    
long  N,L,K;
    prime
= ( 1 << 16 ) + 1 ;
    
while  (scanf( " %ld %ld %ld " , & N, & L, & K) != EOF)
    {
        lmax
= 0 ;
        memset(hash,
0 , sizeof (hash));
        unsigned __int64 mask
= 0 ;
    
        
long  i;
        
long  j;

        
for  (i = 0 ;i < K; ++ i)
        {
            mask
= (mask << 1 ) + 1 ;
        }


        
for  (i = 0 ;i < N; ++ i)
        {
            scanf(
" %s " ,key);
            unsigned __int64 sum
= 0 ;
            
            
for  (j = 0 ;key[j] != ' \0 ' ; ++ j)
            {
                sum
= (sum << 1 ) + (key[j] - ' 0 ' );
            }

            
long  len = L;
            
while  (len - K >= 0 )
            {
                unsigned __int64 t
= (sum & mask);
                sum
= sum >> 1 ;
                Dohash(t);
                
-- len;
            }
        }
        printf(
" %ld\n " ,lmax);
    }
    
return   0 ;
}

转载于:https://www.cnblogs.com/zhuangli/archive/2008/09/14/1290723.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值