hdu 1800 Flying to the Mars (字符串hash)
题意:n个人,每个人有一个level值,用一个最长30位的,可能带前缀0的数字串表示,如果i的level大于j的level,那么i可以教j飞行,每个人只能有一个老师,每个人也只能收一个徒弟。师生可以共用一把扫帚飞行。现在问最少需要多少扫帚。
思路:分析发现,影响扫帚多少的是相等的数有多少,因为只要不相等,就肯定可以构成师生关系….
更确切得说,是所有数出现次数的最大值。
有一个trick点,就是带前缀0和不带前缀0的两个level被认为是相等的,hash的时候要处理前缀0.
1/* ***********************************************
2Author :111qqz
3Created Time :2016年11月22日 星期二 19时18分30秒
4File Name :code/hdu/1800.cpp
5************************************************ */
6
7#include <cstdio>
8#include <cstring>
9#include <iostream>
10#include <algorithm>
11#include <vector>
12#include <queue>
13#include <set>
14#include <map>
15#include <string>
16#include <cmath>
17#include <cstdlib>
18#include <ctime>
19#define fst first
20#define sec second
21#define lson l,m,rt<<1
22#define rson m+1,r,rt<<1|1
23#define ms(a,x) memset(a,x,sizeof(a))
24typedef long long LL;
25#define pi pair < int ,int >
26#define MP make_pair
27
28using namespace std;
29const double eps = 1E-8;
30const int dx4[4]={1,0,0,-1};
31const int dy4[4]={0,-1,1,0};
32const int inf = 0x3f3f3f3f;
33unsigned int BKDHash(char *str)
34{
35 unsigned int seed = 251;
36 unsigned int hash = 0 ;
37 while (*str=='0') str++; //带前缀0的和不带前缀0的认为是同一个数,因此要处理前缀0.
38 while (*str) hash = hash*seed+(*str++);
39 return (hash&0x7fffffff);
40}
41int n;
42map<int,int>mp;
43char st[305];
44int main()
45{
46 #ifndef ONLINE_JUDGE
47// freopen("code/in.txt","r",stdin);
48 #endif
49 while (~scanf("%d",&n))
50 {
51 mp.clear();
52 for ( int i = 1; i <= n ; i++)
53 {
54 scanf("%s",st);
55 int id = BKDHash(st);
56 if (!mp[id]) mp[id] = 1;
57 else mp[id]++;
58 }
59 int ans = 0;
60 for ( auto it = mp.begin(); it !=mp.end(); it++)
61 {
62 ans = max(ans,it->sec);
63 }
64 printf("%d\n",ans);
65 }
66
67
68 #ifndef ONLINE_JUDGE
69 fclose(stdin);
70 #endif
71 return 0;
72}