Advertisement
Guest User

CG MIME Type

a guest
Oct 2nd, 2020
48
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.06 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <vector>
  4. #include <sstream>
  5.  
  6. using namespace std;
  7.  
  8. /*!!! Just before pasting this code to pastebin it failed the last test once randomly */
  9.  
  10. //Slightly modified djb2 hashing function.
  11. unsigned long int get_hash(const string &extension, const vector<vector<vector<string>>> &table){
  12.     unsigned long int index = 5381;
  13.     for(int i = 0; i < extension.length(); i++){
  14.         index = (((index << 7) + index) + extension[i]);
  15.     }
  16.     index = index % table.size();
  17.  
  18.     return index;
  19. }
  20.  
  21. //Inserts the extension and media type into the hash table.
  22. void insert_hashed(const string &extension, const string &MT, vector<vector<vector<string>>> &table){
  23.     unsigned long int index = get_hash(extension, table);
  24.  
  25.     table[index].push_back({extension, MT});
  26. }
  27.  
  28. //Looks for a media type using the extension as key. If found returns the media type, if not returns "UNKNOWN".
  29. string find_hashed(const string &extension, const vector<vector<vector<string>>> &table){
  30.     unsigned long int index = get_hash(extension, table);
  31.  
  32.     int i = 0;
  33.     int size = table[index].size();
  34.     while(i != size && extension != table[index][i][0]){
  35.             i++;
  36.     }
  37.     if(i == size){
  38.         return "UNKNOWN";
  39.     }
  40.     return table[index][i][1];
  41. }
  42.  
  43. //Converts a string to lower case.
  44. void to_lower(string &str){
  45.     for(int i = 0; i < str.length(); i++){
  46.         str[i] = (char)tolower((unsigned char)str[i]);
  47.     }
  48. }
  49.  
  50. //Debug function. Used to test fitness of the hashing function. COMMENT it's use in main() for full performance
  51. void test(vector<vector<vector<string>>> &table){
  52.     int size = table.size();
  53.     int buckets = 0;
  54.     int min_bucket = table[0].size();
  55.     int max_bucket = 0;
  56.  
  57.     int bucket_size = 0;
  58.     for(int i = 0; i < size; i++){
  59.         bucket_size = table[i].size();
  60.  
  61.         if(bucket_size > 0){
  62.             buckets++;
  63.             if(bucket_size > max_bucket){
  64.                 max_bucket = bucket_size;
  65.             }
  66.         }
  67.         if(bucket_size < min_bucket){
  68.             min_bucket = bucket_size;
  69.         }
  70.     }
  71.     cerr << "Buckets: " << buckets << "\n" <<
  72.             "Empty indices: " << size - buckets << "\n" <<
  73.             "Max size of bucket: " << max_bucket << "\n" <<
  74.             "Min size of bucket: " << min_bucket << "\n" <<
  75.             "Average fill of buckets(load): " << float((float)buckets/(float)size) << endl;
  76. }
  77.  
  78. int main()
  79. {
  80.     int N; // Number of elements which make up the association table.
  81.     cin >> N; cin.ignore();
  82.     int Q; // Number Q of file names to be analyzed.
  83.     cin >> Q; cin.ignore();
  84.  
  85.     vector<vector<vector<string>>> table (N, vector<vector<string>>()); // The hash table.
  86.     string EXT; // File extension.
  87.     string MT; // MIME type.
  88.  
  89.     for (int i = 0; i < N; i++) {
  90.         cin >> EXT >> MT; cin.ignore();
  91.  
  92.         to_lower(EXT);
  93.         insert_hashed(EXT, MT, table);
  94.     }
  95.  
  96.     test(table); // COMMENT this line for full performance!
  97.  
  98.     stringstream out_stream; // Stream to store all the output messages.
  99.     string FNAME;
  100.     string extension;
  101.     for (int i = 0; i < Q; i++) {
  102.         getline(cin, FNAME); // One file name per line.
  103.         extension = FNAME.substr(FNAME.find_last_of('.') + 1); // Extraction of the extension
  104.        
  105.         if(!FNAME.compare(extension)){ // If FNAME and extension are the same then there was no '.' so the FNAME has no extension.
  106.             //cout << "UNKNOWN\n"; // Swapped for the stringstream method.
  107.             out_stream << "UNKNOWN\n";
  108.         } else{
  109.             to_lower(extension);
  110.             //cout << find_hashed(extension, table) << "\n"; // Swapped for the stringstream method.
  111.             out_stream << find_hashed(extension, table) << "\n";
  112.         }
  113.         /*You can swap around the commenting of stringstream and cout in the above section
  114.         and comment the cout below to see the Large dataset fail while outputting the whole answer.*/
  115.     }
  116.  
  117.     cout << out_stream.str() << endl; // Prints all output messages at once saving some time.
  118.     return 0;
  119. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement