关闭

hdu1800 Flying to the Mars ELFHash/BKDHash

409人阅读 评论(0) 收藏 举报
分类:

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

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:48003次
    • 积分:1309
    • 等级:
    • 排名:千里之外
    • 原创:86篇
    • 转载:7篇
    • 译文:0篇
    • 评论:6条
    最新评论