hdu 1800 Flying to the Mars (字符串hash)

题目链接

题意:n个人,每个人有一个level值,用一个最长30位的,可能带前缀0的数字串表示,如果i的level大于j的level,那么i可以教j飞行,每个人只能有一个老师,每个人也只能收一个徒弟。师生可以共用一把扫帚飞行。现在问最少需要多少扫帚。

思路:分析发现,影响扫帚多少的是相等的数有多少,因为只要不相等,就肯定可以构成师生关系....

更确切得说,是所有数出现次数的最大值。

有一个trick点,就是带前缀0和不带前缀0的两个level被认为是相等的,hash的时候要处理前缀0.

/* ***********************************************
Author :111qqz
Created Time :2016年11月22日 星期二 19时18分30秒
File Name :code/hdu/1800.cpp
************************************************ */
 1#include <cstdio>
 2#include <cstring>
 3#include <iostream>
 4#include <algorithm>
 5#include <vector>
 6#include <queue>
 7#include <set>
 8#include <map>
 9#include <string>
10#include <cmath>
11#include <cstdlib>
12#include <ctime>
13#define fst first
14#define sec second
15#define lson l,m,rt<<1
16#define rson m+1,r,rt<<1|1
17#define ms(a,x) memset(a,x,sizeof(a))
18typedef long long LL;
19#define pi pair < int ,int >
20#define MP make_pair
 1using namespace std;
 2const double eps = 1E-8;
 3const int dx4[4]={1,0,0,-1};
 4const int dy4[4]={0,-1,1,0};
 5const int inf = 0x3f3f3f3f;
 6unsigned int BKDHash(char *str)
 7{
 8    unsigned int seed = 251;
 9    unsigned int hash = 0 ;
10    while (*str=='0') str++; //带前缀0的和不带前缀0的认为是同一个数,因此要处理前缀0.
11    while (*str) hash = hash*seed+(*str++);
12    return (hash&0x7fffffff);
13}
14int n;
15map<int,int>mp;
16char st[305];
17int main()
18{
19	#ifndef  ONLINE_JUDGE 
20//	freopen("code/in.txt","r",stdin);
21  #endif
22	while (~scanf("%d",&n))
23	{
24	    mp.clear();
25	    for ( int i = 1; i <= n ; i++)
26	    {
27		scanf("%s",st);
28		int id = BKDHash(st);
29		if (!mp[id]) mp[id] = 1;
30		else mp[id]++;
31	    }
32	    int ans = 0;
33	    for ( auto it = mp.begin(); it !=mp.end();  it++)
34	    {
35		ans = max(ans,it->sec);
36	    }
37	    printf("%d\n",ans);
38	}
1  #ifndef ONLINE_JUDGE  
2  fclose(stdin);
3  #endif
4    return 0;
5}