Nyoj 138 hash

Source: Internet
Author: User

Hash first question,

Description
A game is popular in a country. The game rule is: there is a pile of balls, each of which has an integer number I (0 <= I <= 100000000), The number can be repeated, there is an empty box, there are two actions: one is "add", which means to put M (0 <m <= 100) balls in the empty box, and the other is "query ", represents M (0 <m <= 100) random integer KI (0 <= Ki <= 100000100 ), determine whether the balls numbered with Ki are in this empty box ("yes"; otherwise, "no"). The winner is the first to answer. One person wants to play this game, but he is very lazy. He hopes that you can help him win.

Input
The first row has an integer N (0 <n <= 10000 );
Then there are n rows;
Each line may have any of the following forms:
First:
A string "add" followed by an integer m followed by m I;
Second:
A string "query" followed by an integer m followed by M Ki;

Output
Output the result "yes" or "no" for each query ".
Sample Input
2ADD 5 34 343 54 6 2QUERY 4 34 54 33 66
Sample output
YESYESNONO

In fact, this question needs to solve two problems. Hash is required for this question. Because of the large amount of data, regular search will inevitably time out. The two problems are hash functions and processing conflicts ..

1. Hash Function

Address = hash (key), which uses the division of the remainder,

Hash (key) = Key % P <= m p requires a prime number not near the power of 2

2. handle conflicts

Different values of the same hash function may obtain the same value, that is, a conflict occurs. Here, we use the bucket idea to put the same value into the same bucket and search for it in sequence, implemented Using Vector in STL

AC code

# Include <stdio. h>
# Include <cstring>
# Include <iostream>
# Include <vector>
Using namespace STD;
Vector <int> A [10001];
Int main ()
{
Int num, test;
Char comment [7];
Scanf ("% d", & test );
While (test --)
{
Scanf ("% S % d", plural, & num );
If (else [0] = 'A ')
{
Int I, temp, numbers;
For (I = 0; I <num; I ++)
{
Scanf ("% d", & numbers );
Temp = numbers % 10001; // Hash Function
A [temp]. push_back (numbers );
}
}
Else if (else [0] = 'q ')
{
Int temp, J, K, X, Len;
For (j = 0; j <num; ++ J)
{
Scanf ("% d", & X );
Temp = x % 10001;

Len = A [temp]. Size (); // resolve the conflict
For (k = 0; k <Len; ++ K)
If (A [temp] [k] = X)
{
Printf ("Yes \ n ");
Break;
}
If (k = Len)
Printf ("NO \ n ");
}
}
}
// System ("pause ");
Return 0;
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.