[프로그래머스] 순위 검색
#include <string>
#include <vector>
#include <map>
#include <algorithm>
using namespace std;
typedef long long ll;
vector<int> solution(vector<string> info, vector<string> query) {
vector<int> answer;
map<string, vector<int>> m;
for (int i = 0; i < info.size(); ++i) {
vector<string> jogun;
int score;
string buffer = "";
for (int j = 0; j < info[i].length(); ++j) {
if (info[i][j] == ' ') {
jogun.push_back(buffer);
buffer = "";
}
else buffer += info[i][j];
}
score = stoi(buffer);
vector<string> keys;
string key = "----";
key[0] = jogun[0][0];
key[1] = jogun[1][0];
key[2] = jogun[2][0];
key[3] = jogun[3][0];
keys.push_back(key);
key[3] = '-';
keys.push_back(key);
key[2] = '-';
key[3] = jogun[3][0];
keys.push_back(key);
key[3] = '-';
keys.push_back(key);
key[1] = '-';
key[2] = jogun[2][0];
key[3] = jogun[3][0];
keys.push_back(key);
key[3] = '-';
keys.push_back(key);
key[2] = '-';
key[3] = jogun[3][0];
keys.push_back(key);
key[3] = '-';
keys.push_back(key);
key[0] = '-';
key[1] = jogun[1][0];
key[2] = jogun[2][0];
key[3] = jogun[3][0];
keys.push_back(key);
key[3] = '-';
keys.push_back(key);
key[2] = '-';
key[3] = jogun[3][0];
keys.push_back(key);
key[3] = '-';
keys.push_back(key);
key[1] = '-';
key[2] = jogun[2][0];
key[3] = jogun[3][0];
keys.push_back(key);
key[3] = '-';
keys.push_back(key);
key[2] = '-';
key[3] = jogun[3][0];
keys.push_back(key);
key[3] = '-';
keys.push_back(key);
for (int i = 0; i < keys.size(); ++i) {
if (m.find(keys[i]) == m.end())
m[keys[i]] = vector<int>(1, score);
else
m[keys[i]].push_back(score);
}
}
for (auto it = m.begin(); it != m.end(); ++it) {
sort(it->second.begin(), it->second.end());
}
for (int i = 0; i < query.size(); ++i) {
string jogun = "";
int score;
string buffer = "";
for (int j = 0; j < query[i].length(); ++j) {
if (query[i][j] == ' ') {
if(buffer != "and") jogun = jogun + buffer[0];
buffer = "";
}
else buffer += query[i][j];
}
score = stoi(buffer);
vector<int> hoobo = m[jogun];
answer.push_back(hoobo.size() - (lower_bound(hoobo.begin(), hoobo.end(), score) - hoobo.begin()));
}
return answer;
}
int main() {
solution({ "java backend junior pizza 150", "python frontend senior chicken 210", "python frontend senior chicken 150", "cpp backend senior pizza 260", "java backend junior chicken 80", "python backend senior chicken 50" }, { "java and backend and junior and pizza 100", "python and frontend and senior and chicken 200", "cpp and - and senior and pizza 250", "- and backend and senior and - 150", "- and - and - and chicken 100", "- and - and - and - 150" });
}
📌참고자료