코딩테스트
[프로그래머스][C++] 순위 검색
이쿠우우
2022. 3. 25. 17:10
반응형
https://programmers.co.kr/learn/courses/30/lessons/72412
글쓴이의 답
개인적인 풀이 임으로
이것보다 더 좋은 알고리즘은 많음...
이렇게도 풀이하는구나.. 공유하기 위해 올림...
#include <string>
#include <vector>
#include <map>
#include <iostream>
#include <algorithm>
using namespace std;
int getScore(string data, string &stringResult) {
int result;
size_t location = data.find_last_of(" ");
size_t delSize = data.size() - location - 1;
string temp = data.substr(location + 1, delSize);
stringResult = data.substr(0, location);
result = stoi(temp);
return result;
}
vector<string> splitString(string data, string del) {
vector<string> result;
size_t delSize = del.size();
size_t location = data.find(del);
while (string::npos != location) {
string temp = data.substr(0, location);
data.erase(0, location + delSize);
result.push_back(temp);
location = data.find(del);
}
result.push_back(data);
return result;
}
vector<int> solution(vector<string> info, vector<string> query) {
vector<int> answer;
map<string, vector<int>> db;
for (auto data : info) {
vector<string> key = splitString(data, " ");
for (int i = 0; i < 16; ++i) {
string tmp = "";
for (int j = 0; j < 4; ++j) {
tmp += (i & (1 << j)) ? key[j] : "-";
}
db[tmp].push_back(stoi(key[4]));
}
}
for (auto &iter : db) sort(iter.second.begin(), iter.second.end());
for (auto data : query) {
vector<string> key = splitString(data, " and ");
string tempString = key[key.size() - 1];
vector<string> temp = splitString(tempString, " ");
key.pop_back();
key.insert(key.end(), temp.begin(), temp.end());
string k = key[0] + key[1] + key[2] + key[3];
vector<int> res = db[k];
int cnt = res.end() - lower_bound(res.begin(), res.end(), stoi(key[4]));
answer.push_back(cnt);
}
return answer;
}
꾸준히 하다보면 실력이 늘겠지..
반응형