算法笔记--哈希
问题A:谁是你的潜在朋友
#include<cstdio> #include<cstring> int main(){ const int maxn = 210; int N, M; int hashTable[maxn] = {0}; int stu[maxn] = {0}; while(scanf("%d%d", &N, &M) != EOF){ memset(hashTable, 0, sizeof(hashTable)); for(int i=1; i <= N; i++){ int P; do{ scanf("%d", &P); stu[i-1] = P; hashTable[P] ++; }while(P < 1 || P > M); } for(int i=1; i <= N; i++){ int k = stu[i-1]; if(hashTable[k] >= 2){ printf("%d\n", hashTable[k]-1); }else{ printf("BeiJu\n"); } }} return 0; }
注意:codeup是多点测试,所以每一次hash表得初始化为0,否则会一直出错的啊!!!
因为一行出错的心态崩 /(ㄒoㄒ)/~~,hash表初始化建议:
#include<cstring> memset(hashTable, 0, sizeof(hashTable));