【HDOJ】1800 Flying to the Mars

1. 题目描述
挺简单的一道题,给定$n$个大整数,求出现最多的次数。

2. 基本思路
这题是可以使用哈希做,ELFHash等哈希都可以过。

3. 代码

  1 /* 1800 */
  2 #include <iostream>
  3 #include <sstream>
  4 #include <string>
  5 #include <map>
  6 #include <queue>
  7 #include <set>
  8 #include <stack>
  9 #include <vector>
 10 #include <deque>
 11 #include <bitset>
 12 #include <algorithm>
 13 #include <cstdio>
 14 #include <cmath>
 15 #include <ctime>
 16 #include <cstring>
 17 #include <climits>
 18 #include <cctype>
 19 #include <cassert>
 20 #include <functional>
 21 #include <iterator>
 22 #include <iomanip>
 23 using namespace std;
 24 //#pragma comment(linker,"/STACK:102400000,1024000")
 25 
 26 #define sti                set<int>
 27 #define stpii            set<pair<int, int> >
 28 #define mpii            map<int,int>
 29 #define vi                vector<int>
 30 #define pii                pair<int,int>
 31 #define vpii            vector<pair<int,int> >
 32 #define rep(i, a, n)     for (int i=a;i<n;++i)
 33 #define per(i, a, n)     for (int i=n-1;i>=a;--i)
 34 #define clr                clear
 35 #define pb                 push_back
 36 #define mp                 make_pair
 37 #define fir                first
 38 #define sec                second
 39 #define all(x)             (x).begin(),(x).end()
 40 #define SZ(x)             ((int)(x).size())
 41 #define lson            l, mid, rt<<1
 42 #define rson            mid+1, r, rt<<1|1
 43 
 44 const int maxl = 105;
 45 const int MOD = 7003;
 46 int H[MOD], cnt[MOD];
 47 char s[maxl];
 48 int n, ans;
 49 
 50 inline int ELFHash(char *s) {
 51     unsigned int h = 0;
 52     unsigned int g;
 53     int i = 0;
 54 
 55     while (s[i]) {
 56         h = (h<<4) + s[i];
 57         g = h & 0xf0000000L;
 58         if (g) {
 59             h ^= (g >> 24);
 60             h &= ~g;
 61         }
 62         ++i;
 63     }
 64 
 65     return h & 0x7fffffff;
 66 }
 67 
 68 inline void Hash(char *s) {
 69     int i = 0;
 70 
 71     while (s[i]=='0')    ++i;
 72     int k = ELFHash(s+i);
 73     int t = k % MOD;
 74 
 75     while (H[t]!=-1 && H[t]!=k)
 76         t = (t + 10) % MOD;
 77 
 78     if (H[t] == -1) {
 79         cnt[t] = 1;
 80         H[t] = k;
 81     } else if (++cnt[t] > ans) {
 82         ans = cnt[t];
 83     }
 84 }
 85 
 86 int main() {
 87     ios::sync_with_stdio(false);
 88     #ifndef ONLINE_JUDGE
 89         freopen("data.in", "r", stdin);
 90         freopen("data.out", "w", stdout);
 91     #endif
 92 
 93     while (scanf("%d",&n)!=EOF) {
 94         memset(H, -1, sizeof(H));
 95         ans = 1;
 96         rep(i, 0, n) {
 97             scanf("%s", s);
 98             Hash(s);
 99         }
100         printf("%d\n", ans);
101     }
102 
103     #ifndef ONLINE_JUDGE
104         printf("time = %d.\n", (int)clock());
105     #endif
106 
107     return 0;
108 }

 

posted on 2016-03-16 22:40  Bombe  阅读(192)  评论(0编辑  收藏  举报

导航