/* ACM North Central Region, 1994-95 Problem E, Processing MX Records Ed Karrels, May 1996 */ #include <stdio.h> #include <stdlib.h> typedef struct { char from[80], to[80]; int pri, up; } MX; void Read(MX *mx) { static char from[80]; char c; scanf("%*[^\n]"); c = getchar(); /* newline */ c = getchar(); /* first character, next line */ if (isspace(c)) { strcpy(mx->from, from); scanf("%d %s", &mx->pri, mx->to); } else { ungetc(c, stdin); scanf("%s %d %s", from, &mx->pri, mx->to); strcpy(mx->from, from); } mx->up = 1; } int Match(char *s1, char *s2) { if (!strcmp(s1, s2)) return 1; if (s2[0] == '*') { s2++; s1 += strlen(s1) - strlen(s2); if (!strcmp(s1, s2)) return 1; } return 0; } int main() { int n, i, best_i, best_pri; MX *mx; char cmd, addr[80]; scanf("%d", &n); mx = (MX*)malloc(sizeof(MX) * n); for (i=0; i<n; i++) Read(mx+i); scanf(" %c", &cmd); while (cmd != 'X') { scanf("%s", addr); switch (cmd) { case 'A': best_i = -1; for (i=0; i<n; i++) { if (mx[i].up && Match(addr, mx[i].from)) { if (best_i == -1 || mx[i].pri < best_pri) { best_i = i; best_pri = mx[i].pri; } } } printf("%s => %s\n", addr, (best_i==-1) ? "" : mx[best_i].to); break; case 'D': for (i=0; i<n; i++) { if (!strcmp(addr, mx[i].to)) mx[i].up = 0; } break; case 'U': for (i=0; i<n; i++) { if (!strcmp(addr, mx[i].to)) mx[i].up = 1; } break; } scanf(" %c", &cmd); } return 0; }