#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<malloc.h>
#define MAXSIZE 100000
typedef struct Node{
char id[19];
char name[24];
Node* prev;
Node* next;
};
Node HashPoolID[MAXSIZE];
Node HashId[MAXSIZE];
Node HashPoolName[MAXSIZE];
Node HashName[MAXSIZE];
unsigned int idIndex;
unsigned int nameIndex;
Node* getNewNodeId() {
return &HashPoolID[idIndex++];
}
Node* getNewNodeName() {
return &HashPoolName[nameIndex++];
}
void insertNodeId(int key, Node* newNode) {
Node* head = &HashId[key];
newNode->next = head->next;
newNode->prev = head;
head->next = newNode;
newNode->next->prev = newNode;
}
void insertNodeName(int key, Node* newNode) {
Node* head = &HashName[key];
newNode->next = head->next;
newNode->prev = head;
head->next = newNode;
newNode->next->prev = newNode;
}
void copys1tos2(char *s1, char *s2) {
for (int i = 0; s2[i]; i++) {
s2[i] = '\0';
}
for (int i = 0; s1[i]; i++) {
s2[i] = s1[i];
}
}
int ChangeToHash(char *str) {
char *key = str;
unsigned long seed1 = 0x7FED7FED, seed2 = 0xEEEEEEEE;
int ch;
for (int i = 0; i < 10; i++) {
ch = key[i];
seed1 = ch ^ (seed1 + seed2);
seed2 = ch + seed1 + seed2 + (seed2 << 5) + 3;
}
if (seed1 < 0)
seed1 = seed1*(-1);
seed1 = seed1%MAXSIZE;
return seed1;
}
bool isSame(char s1[], char s2[]) {
int i = 0;
for (i = 0; s1[i] && s2[i]; i++) {
if (s1[i] != s2[i]) {
return false;
}
}
return true;
}
void init() {
for (int i = 0; i < MAXSIZE; i++) {
HashId[i].next = &HashId[i];
HashId[i].prev = &HashId[i];
HashName[i].next = &HashName[i];
HashName[i].prev = &HashName[i];
}
}
void readNode() {
char name[24];
char id[19];
int idKey = 0;
int nameKey = 0;
for (int i = 0; i < MAXSIZE; i++) {
scanf("%s", &name);
scanf("%s", &id);
idKey = ChangeToHash(id);
Node* newNodeID = getNewNodeId();
copys1tos2(name, newNodeID->name);
copys1tos2(id, newNodeID->id);
insertNodeId(idKey, newNodeID);
Node* newNodeName = getNewNodeName();
nameKey = ChangeToHash(name);
copys1tos2(name, newNodeName->name);
copys1tos2(id, newNodeName->id);
insertNodeName(nameKey, newNodeName);
}
}
void readIDOutPutName() {
char id[19];
int idKey = 0;
for (int i = 0; i < MAXSIZE; i++) {
scanf("%s", &id);
idKey = ChangeToHash(id);
Node* nodetemp = HashId[idKey].next;
while (nodetemp != &HashId[idKey])
{
if (isSame(id, nodetemp->id)) {
// printf("id=%s nodeid=%s nodename=%s\n", id,nodetemp->id ,nodetemp->name);
printf("%s\n", nodetemp->name);
break;
}
nodetemp = nodetemp->next;
}
}
}
void readNameOutPutNum() {
char name[24];
int nameKey = 0;
int account =0 ;
for (int i = 0; i < MAXSIZE; i++) {
scanf("%s", &name);
nameKey = ChangeToHash(name);
account = 0;
Node* nodetemp = HashName[nameKey].next;
while (nodetemp != &HashName[nameKey])
{
if (isSame(name, nodetemp->name)) {
account++;
}
nodetemp = nodetemp->next;
}
// printf("name = %s num =%d\n", name, account);
printf("%d\n",account);
}
}
int main() {
freopen("input.txt","r",stdin);
freopen("result.txt","w", stdout);
init();
readNode();
readIDOutPutName();
readNameOutPutNum();
}