Ball seek (2) time limit: 1000 MS | memory limit: 65535 kb difficulty: 5
-
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
The set in STL has timed out. Later I heard that a hash table is used.
Hash references: http://blog.csdn.net/v_JULY_v/article/details/6256463
#include<cstdio>#include<cstring>const int N = 1e6 + 10;#define mod 200003int Head[N], Next[N], Hash[N];int top;void add(int num){ int key = num % mod; Hash[top] = num; Next[top] = Head[key]; Head[key] = top; top++; }int main(){ int n, m, a; char op[10]; while(~scanf("%d",&n)) { memset(Head, -1, sizeof(Head)); top = 0; while(n--) { scanf("%s%d", op, &m); if(op[0] == 'A') { for(int i = 0; i < m; i++) { scanf("%d",&a); add(a); } } else { while(m--) { int flag = 0; scanf("%d",&a); for(int i = Head[a%mod]; i != -1; i = Next[i]) { if(Hash[i] == a) { flag = 1; break; } } if(flag) printf("YES\n"); else printf("NO\n"); } } } } return 0;}
Nyoj 138 ball finding (2) hash table