Skip to content

Instantly share code, notes, and snippets.

@remusjones
Created November 30, 2023 00:06
Show Gist options
  • Select an option

  • Save remusjones/390de269b3025499c1710397412bb19e to your computer and use it in GitHub Desktop.

Select an option

Save remusjones/390de269b3025499c1710397412bb19e to your computer and use it in GitHub Desktop.
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
string ToLower(string str)
{
for(auto& c : str)
{
c = tolower(c);
}
return str;
}
string GetFileExtension(string fileName)
{
const char fileExtensionDelimiter = '.';
int delimLocation = fileName.find_last_of(fileExtensionDelimiter);;
if (delimLocation == -1)
return "";
else return fileName.substr(delimLocation + 1);
}
std::unordered_map<string, string> MimeTypes;
const string UnknownType = "UNKNOWN";
string LookupMimeType(string extension)
{
auto mimeTypeIter = MimeTypes.find(extension);
string found = mimeTypeIter != MimeTypes.end() ? mimeTypeIter->second : UnknownType;
return mimeTypeIter != MimeTypes.end() ? mimeTypeIter->second : UnknownType;
}
/**
* Auto-generated code below aims at helping you parse
* the standard input according to the problem statement.
**/
int main()
{
int n; // Number of elements which make up the association table.
cin >> n; cin.ignore();
int q; // Number Q of file names to be analyzed.
cin >> q; cin.ignore();
for (int i = 0; i < n; i++) {
string ext; // file extension
string mt; // MIME type.
cin >> ext >> mt; cin.ignore();
ext = ToLower(ext);
MimeTypes.insert_or_assign(ext, mt);
}
for (int i = 0; i < q; i++) {
string fname;
getline(cin, fname); // One file name per line.
fname = ToLower(GetFileExtension(fname));
cout << LookupMimeType(fname) << endl;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment