-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTask6.cpp
More file actions
93 lines (78 loc) · 2.38 KB
/
Task6.cpp
File metadata and controls
93 lines (78 loc) · 2.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
https://www.codingame.com/ide/puzzle/mime-type
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
struct Extension {
std::string _MIMEtype;
std::string _extension;
Extension(std::string ext, std::string mt) :
_MIMEtype(mt), _extension(ext) {
};
};
int indElemInOrder(std::string str) {
char firstLetter = str[0];
int indLetter = int(firstLetter) - int('a');
if (firstLetter < 'a')
indLetter += (int('a') - int('A'));
return indLetter;
}
std::string tolower(std::string input) {
std::string str = input;
for (auto &letter : str) {
if (int(letter) <= int('Z') &&
int(letter) >= int('A'))
letter = char(int(letter) + (int('a') - int('A')));
}
return str;
}
std::string findExtension(std::string fname) {
if (fname[fname.size() - 1] == '.') return "UNKNOWN";
for (int i = fname.size() - 1; i >= 0; --i) {
if (fname[i] == '.') return fname.substr(i + 1);
}
return "UNKNOWN";
}
void printMimeType(std::vector<std::vector<Extension>> extensions,
std::string fileName) {
auto fileExtension = tolower(findExtension(fileName));
if (fileExtension == "unknown")
std::cout << "UNKNOWN" << std::endl;
else {
bool matchExt = false;
int indExt = indElemInOrder(fileExtension);
for (auto extension : extensions[indExt]) {
auto ex = tolower(extension._extension);
if (fileExtension == ex) {
std::cout << extension._MIMEtype << std::endl;
matchExt = !matchExt;
break;
}
}
if (!matchExt) std::cout << "UNKNOWN" << std::endl;
}
}
int main()
{
int n;
std::cin >> n; std::cin.ignore();
int q;
std::cin >> q; std::cin.ignore();
int indType = 0;
int alphabet = int('Z') - int('A') + 1;
std::vector<std::vector<Extension>> extensions(alphabet);
std::vector<int> indTypes(alphabet, 0);
for (int i = 0; i < n; i++) {
std::string ext;
std::string mt;
std::cin >> ext >> mt; std::cin.ignore();
int indExt = indElemInOrder(ext);
Extension extension = Extension(ext, mt);
extensions[indExt].push_back(extension);
}
for (int i = 0; i < q; i++) {
std::string fname;
std::getline(std::cin, fname);
printMimeType(extensions, fname);
}
}