hdu1800 Flying to the Mars ELFHash/BKDHash

we assume that one soldier has one level number indicating his degree. The soldier who has a higher level could teach the lower , that is to say the former’s level > the latter’s . But the lower can’t teach the higher. One soldier can have only one teacher at most , certainly , having no teacher is also legal. Similarly one soldier can have only one student at most while having no student is also possible. Teacher can teach his student on the same broomstick .Certainly , all the soldier must have practiced on the broomstick before they fly to the Mars! Magic broomstick is expensive !So , can you help PPF to calculate the minimum number of the broomstick needed .

题意:士兵要学骑扫帚。每个士兵有一个level,level高的能在同一把扫帚上教level低的怎么骑。一个人最多有一个老师,一个学生。也可以没有。给n个士兵的level值,问最少需要多少扫帚。

题解:两个士兵level为3的,就不可能在一根扫帚上,也就是说统计数字出现的个数就好了。利用Hash表解决。给你的每个数不超过30位,非常大要利用字符串hash算法如BKDHash或者其他去处理。hash预定义为-1。

#include<cstdlib>
#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
#define maxn 2000003
using namespace std;
int hash[3003];
int Count[3003];
int ans,n;
int BKDHash(char *key)
{
    unsigned int hash=0;
    unsigned int seed=131;
    while(*key)
    {
        hash=hash*seed+*key++;
    }
    return (hash&0x7FFFFFFF);
}
/*
ELFhash函数是对字符串的散列。
它对于长字符串和短字符串都很有效,
字符串中每个字符都有同样的作用,
它巧妙地对字符的ASCII编码值进行计算,
ELFhash函数对于能够比较均匀地把字符串分布在散列表中。
*/
int ELFHash(char *key)
{
    unsigned int hash=0;
    unsigned int seed=0;
    while(*key)
    {
        hash=(hash<<4)+*key++;
        if((seed=hash&0xf0000000L)!=0)
        {
            hash^=  seed>>24;
        }
        hash&=~seed;
    }
    return hash;
}
void hashit(char *c)
{
    int k,t;
    while(*c=='0')
    {
        c++;
    }
    k=BKDHash(c);
    t=k%3003;
    while(hash[t]!=k&&hash[t]!=-1)
    {
        t=(t+10)%3003;
    }
    if(hash[t]==-1)
    {
        hash[t]=k;
        Count[t]=1;
    }
    else if(++Count[t]>ans)
        ans=Count[t];
}
int main()
{

    char c[30];
    while(cin>>n)
    {
        ans=1;
        memset(hash,-1,sizeof(hash));
        for(gets(c);n>0;n--)
        {
            gets(c);
            hashit(c);
        }
        cout<<ans<<endl;
    }
}



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值