Fafu 1266数(hashアプリケーション)
http://acm.fafu.edu.cn/problem.php?id=1266
2^31の範囲内の数%10000を真hash値に求める
値をshチェーンに保存する
2^31の範囲内の数%10000を真hash値に求める
値をshチェーンに保存する
#include <stdio.h>
#include <string.h>
struct node{
int val;
node * next;
}hash[10000];
int main()
{
freopen("E:\\input.txt", "r", stdin);
int n;
while(scanf("%d", &n) != EOF)
{
memset(hash, 0, sizeof(hash));
int hashVal;
while(n--)
{
int v;
scanf("%d", &v);
hashVal = v%10000;
node * last = hash[hashVal].next; //save
hash[hashVal].next = new node;
hash[hashVal].next->val = v;
hash[hashVal].next->next = last;
}
int m;
scanf("%d", &m);
while(m--)
{
int query;
scanf("%d", &query);
hashVal = query % 10000;
node * pointer = hash[hashVal].next;
int times = 0;
while(pointer != NULL)
{
if(pointer->val == query)
times++;
pointer = pointer->next;
}
if(m != 0)
printf("%d ", times);
else
printf("%d
", times);
}
}
return 0;
}