Flying to the Mars (hash) 1800 hdu

/*
 参考课件写的程序都有那么多的错误,悲剧了
 http://acm.hdu.edu.cn/forum/read.php?tid=7329
 http://hi.baidu.com/heiyeshuwu/blog/item/3853baa1ed68b488471064b4.html
 http://acm.hdu.edu.cn/forum/read.php?tid=6567
 除去马甲,本题的本质是——求相同级别(level)的人最多是几个。
 如果level的范围不大的话(64位整数可以表示)——本题很简单,简单贪心
 本题的难点:level的范围较大,需用大数或者字符串比较(去首0)
 效率较高、编程简单的方法:Hash!
 此外,字典树也是不错的选择(下回再写,字典树学的不好)

*/
#include<iostream>//2551140 2010-06-21 00:34:20 Accepted 1800 156MS 292K 862 B C++ 悔惜晟
#include<cstdio>
#include<cstring>
using namespace std;
const int Max = 7003;
int hash[Max];
int count[Max];
int max1;
 int Hash(char *url)
{
 unsigned long h = 0, g;//一开始我抄模板是用 unsigned int 的
 while(*url)
 {
  h = (h << 4) + *url++;
  g = h & 0xf0000000L;
  if(g)
   h ^= g >> 24;
  h &= ~g;
 }
 return h;
}

void Fun(char *key)//这个函数处理的很巧妙
{
 int k;
 while(*key == '0') key++;
 k = Hash(key);
 int t = k % Max;
    while( hash[t] != k && hash[t] != -1 )
        t = ( t + 10 ) % Max;
    if( hash[t] == -1 )    count[t] = 1,hash[t] = k;
    else if( ++count[t] > max1 ) max1 = count[t];

}
int main()
{
 int n;
 char str[35];
 while(scanf("%d", &n) != EOF)
 {
  memset(hash, -1, sizeof(hash));//一定要初始化为-1??
  memset(count, 0, sizeof(count));
  max1 = 1;
  while(n--)
  {
   scanf("%s", str);
   Fun(str);
   
  }
  printf("%d/n", max1);
 }
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值