Advertisement
MonsterScripter

CodinGame_2023_09_01__17_50_03__mime_type.c

Sep 1st, 2023
797
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.78 KB | None | 0 0
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include <stdbool.h>
  5.  
  6. // Structure pour stocker une extension et un type MIME
  7. struct MimeEntry {
  8.     char ext[11];
  9.     char mt[51];
  10. };
  11.  
  12. int main()
  13. {
  14.     // Nombre d'éléments dans la table d'association.
  15.     int N;
  16.     scanf("%d", &N);
  17.     // Nombre de noms de fichiers à analyser.
  18.     int Q;
  19.     scanf("%d", &Q);
  20.     // Alloue de la mémoire pour stocker les entrées MIME en utilisant malloc
  21.     struct MimeEntry *mtMap = (struct MimeEntry *)malloc(N * sizeof(struct MimeEntry));
  22.     for (int i = 0; i < N; i++) {
  23.         scanf("%s %s", mtMap[i].ext, mtMap[i].mt);
  24.         for (int j = 0; mtMap[i].ext[j]; j++) {
  25.             mtMap[i].ext[j] = tolower(mtMap[i].ext[j]);
  26.         }
  27.     }
  28.     for (int i = 0; i < Q; i++) {
  29.         // Un nom de fichier par ligne.
  30.         char FNAME[257];
  31.         scanf(" %[^\n]", FNAME);
  32.         fgetc(stdin);
  33.         // Trouve la dernière occurrence du point dans le nom de fichier.
  34.         char *dotPos = strrchr(FNAME, '.');
  35.         if (dotPos == NULL || dotPos[1] == '\0') {
  36.             printf("UNKNOWN\n");
  37.         } else {
  38.             // Convertit l'extension en minuscules
  39.             for (int j = 0; dotPos[j]; j++) {
  40.                 dotPos[j] = tolower(dotPos[j]);
  41.             }
  42.             // Recherche le type MIME correspondant dans la table d'association
  43.             int found = 0;
  44.             for (int j = 0; j < N; j++) {
  45.                 if (strcmp(dotPos+1, mtMap[j].ext) == 0) {
  46.                     printf("%s\n", mtMap[j].mt);
  47.                     found = 1;
  48.                     break;
  49.                 }
  50.             }
  51.  
  52.             if (!found) {
  53.                 printf("UNKNOWN\n");
  54.             }
  55.         }
  56.     }
  57.     free(mtMap);
  58.     return EXIT_SUCCESS;
  59. }
  60.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement