标签:hash
2 ADD 5 34 343 54 6 2 QUERY 4 34 54 33 66
YES YES NO NO
一开始用STL里面的set,超时了。后来才听说要用哈希表。
哈希参考资料:http://blog.csdn.net/v_JULY_v/article/details/6256463
#include<cstdio> #include<cstring> const int N = 1e6 + 10; #define mod 200003 int 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; }
标签:hash
原文地址:http://blog.csdn.net/lyhvoyage/article/details/39203543