-
Notifications
You must be signed in to change notification settings - Fork 0
/
순위 검색 (시간 초과).cpp
113 lines (90 loc) · 2.97 KB
/
순위 검색 (시간 초과).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
112
113
// 문제 링크: https://programmers.co.kr/learn/courses/30/lessons/72412
#include <string>
#include <vector>
#include <unordered_map>
#include <sstream>
#include <algorithm>
using namespace std;
unordered_map<string, unordered_map<string, unordered_map<string, unordered_map<string, vector<int> > > > > db;
vector<vector<string> > vals = {{"cpp", "java", "python"}, {"backend", "frontend"}, {"junior", "senior"}, {"chicken", "pizza"}};
vector<int> answer;
void dbInit() {
vector<int> score;
unordered_map<string, vector<int> > food;
food.insert({"chicken", score});
food.insert({"pizza", score});
unordered_map<string, unordered_map<string, vector<int> > > exp;
exp.insert({"junior", food});
exp.insert({"senior", food});
unordered_map<string, unordered_map<string, unordered_map<string, vector<int> > > > group;
group.insert({"backend", exp});
group.insert({"frontend", exp});
db.insert({"cpp", group});
db.insert({"java", group});
db.insert({"python", group});
return;
}
//score 이상 원소 수를 센다
int moreThan(vector<int> &nums, int score) {
if (nums.size() == 0) {
return 0;
}
sort(nums.begin(), nums.end());
int pos = lower_bound(nums.begin(), nums.end(), score) - nums.begin();
return nums.size() - pos;
}
int dfs (vector<string> &queryIn, vector<string> queryOut) {
// 쿼리를 완성하였다.
int ans = 0;
if (queryOut.size() == 5) {
ans += moreThan(db[queryOut[0]][queryOut[1]][queryOut[2]][queryOut[3]], stoi(queryIn[4]));
}
else {
int nextIter = queryOut.size();
if (queryIn[nextIter] == "-") {
for (auto &next : vals[nextIter]) {
queryOut.push_back(next);
ans += dfs(queryIn, queryOut);
queryOut.pop_back();
}
}
else {
queryOut.push_back(queryIn[nextIter]);
ans += dfs(queryIn, queryOut);
}
}
return ans;
}
void queryInsert(vector<string> &result) {
db[result[0]][result[1]][result[2]][result[3]].push_back(stoi(result[4]));
}
void findUser(vector<string> &result) {
vector<string> queryOut;
int score = dfs(result, queryOut);
answer.push_back(score);
}
vector<int> solution(vector<string> info, vector<string> query) {
dbInit();
// info를 파싱하여 db에 삽입
for (auto &next : info) {
istringstream ss(next);
string buffer;
vector<string> result;
while(getline(ss, buffer, ' ')) {
result.push_back(buffer);
}
queryInsert(result);
}
// 각 조건에 맞게 검색
for (auto &next : query) {
istringstream ss(next);
string buffer;
vector<string> result;
while(getline(ss, buffer, ' ')) {
if (buffer != "and")
result.push_back(buffer);
}
findUser(result);
}
return answer;
}