SDUT 1480 数据结构试验: 哈希表

  取余,邻接表。

  

 1 #include <iostream>
 2 #include <algorithm>
 3 #include <cstdio>
 4 #include <cstring>
 5 #include <cstdlib>
 6 #include <cmath>
 7 
 8 using namespace std;
 9 
10 struct N
11 {
12     int data,ans;
13     N *next;
14 }*hash[2000001] = {NULL};
15 
16 int Max = 0,Max_num;
17 
18 N *creat()
19 {
20     N *p = (N *)malloc(sizeof(N));
21     p->next = NULL;
22     return p;
23 }
24 
25 void insert(int OriginalNum,int Num)
26 {
27     N *p,*q;
28     if(hash[Num] == NULL)
29     {
30         hash[Num] = creat();
31     }
32 
33     for(p = hash[Num]; p->next != NULL && OriginalNum < p->next->data; p = p->next)
34         ;
35     if(p->next == NULL || OriginalNum < p->next->data)
36     {
37         q = creat();
38         q->ans = 1;
39         q->data = OriginalNum;
40         if(q->ans > Max)
41         {
42             Max = p->ans;
43             Max_num = OriginalNum;
44         }
45         q->next = p->next;
46         p->next = q;
47     }
48     else
49     {
50         p = p->next;
51         if(p->ans > Max)
52         {
53             Max = p->ans;
54             Max_num = OriginalNum;
55         }
56     }
57 }
58 
59 int main()
60 {
61     int n,a;
62     scanf("%d",&n);
63     while(n--)
64     {
65         scanf("%d",&a);
66         if(a > 2000000)
67         {
68             insert(a,a%2000000);
69         }
70         else
71         {
72             insert(a,a);
73         }
74     }
75     printf("%d\n",Max_num);
76     return 0;
77 }
View Code

 

你可能感兴趣的:(SDUT 1480 数据结构试验: 哈希表)