-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathC++.cpp
111 lines (106 loc) · 2.32 KB
/
C++.cpp
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#include <algorithm>
#include <iostream>
#include <string>
#include <unordered_map>
#include <vector>
using namespace std;
class MusicStyle
{
public:
string name;
int minM;
int maxM;
int minF;
int maxF;
MusicStyle() = default;
MusicStyle(string name) : name(name)
{
minM = -1;
maxM = -1;
minF = -1;
maxF = -1;
}
void computeMinMax(bool isM, int age)
{
if (isM)
{
if (minM == -1)
{
minM = age;
maxM = age;
}
if (age > maxM)
{
maxM = age;
}
}
else
{
if (minF == -1)
{
minF = age;
maxF = age;
}
if (age > maxF)
{
maxF = age;
}
}
}
bool isInMinMax(bool isM, int age)
{
return (isM && age >= minM && age <= maxM) || (!isM && age >= minF && age <= maxF);
}
};
vector<string> splitString(string s, string d)
{
size_t pos = 0;
string token;
vector<string> result;
while ((pos = s.find(d)) != string::npos)
{
token = s.substr(0, pos);
result.push_back(token);
s.erase(0, pos + d.length());
}
result.push_back(s);
return result;
}
int main()
{
unordered_map<string, MusicStyle> map;
int n;
cin >> n;
cin.ignore();
for (int i = 0; i < n; i++)
{
string answer;
getline(cin, answer);
vector<string> splitedString = splitString(answer, " ");
int age = stoi(splitedString[0]);
bool gender = splitedString[1] == "male" ? true : false;
if (splitedString.size() == 3)
{
string style = splitedString[2];
if (map.find(style) == map.end())
{
map[style] = {style};
}
map[style].computeMinMax(gender, age);
}
else
{
string answer = "None";
for (auto &it : map)
{
MusicStyle value = it.second;
if (value.isInMinMax(gender, age))
{
answer = it.first;
break;
}
}
cout << answer << '\n';
}
}
}