Guest User

CodinGame Mime Type

a guest
Feb 28th, 2025
27
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.78 KB | Source Code | 0 0
  1. #include <algorithm>
  2. #include <chrono>
  3. #include <iostream>
  4. #include <sstream>
  5. #include <string>
  6. #include <unordered_map>
  7.  
  8. void convert_str_tolower(std::string *str) {
  9.   std::transform(str->begin(), str->end(), str->begin(), ::tolower);
  10. }
  11.  
  12. int main() {
  13.   const auto start = std::chrono::high_resolution_clock::now();
  14.  
  15.   int n; // Number of elements which make up the association table.
  16.   int q; // Number Q of file names to be analyzed.
  17.   std::cin >> n >> q;
  18.   std::cin.ignore();
  19.   std::string ext; // file extension
  20.   std::string mt;  // MIME type.
  21.   std::unordered_map<std::string, std::string> assocTable;
  22.   std::unordered_map<std::string, std::string>::const_iterator
  23.       assocTableIterator;
  24.  
  25.   for (int i = 0; i < n; i++) {
  26.     std::cin >> ext >> mt;
  27.     std::cin.ignore();
  28.     convert_str_tolower(&ext);
  29.     assocTable.insert({ext, mt});
  30.   }
  31.  
  32.   std::string fName;
  33.   std::string extension;
  34.   int indexOfExtensionSeperator = -1;
  35.   std::string output = "UNKNOWN";
  36.   std::ostringstream out;
  37.   std::string outout;
  38.   for (int i = 0; i < q; i++) {
  39.     output = "UNKNOWN";
  40.     getline(std::cin, fName);
  41.     indexOfExtensionSeperator = fName.find_last_of('.');
  42.     if (indexOfExtensionSeperator > -1) {
  43.       extension = fName.substr(indexOfExtensionSeperator + 1);
  44.       convert_str_tolower(&extension);
  45.       assocTableIterator = assocTable.find(extension);
  46.       if (assocTableIterator != assocTable.end()) {
  47.         output = assocTableIterator->second;
  48.       }
  49.     }
  50.     out << output << "\n";
  51.   }
  52.   const auto end = std::chrono::high_resolution_clock::now();
  53.  
  54.   const auto duration =
  55.       std::chrono::duration_cast<std::chrono::milliseconds>(end - start);
  56.   std::cerr << "Time: " << duration.count() << "ms" << std::endl;
  57.  
  58.   std::cout << out.str() << std::flush;
  59. }
Advertisement
Add Comment
Please, Sign In to add comment