본문 바로가기
카카오 코딩 테스트 풀이

순위 검색 C++

by paysmile 2021. 3. 16.

https://programmers.co.kr/learn/courses/30/lessons/72412

 

코딩테스트 연습 - 순위 검색

["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","pyt

programmers.co.kr

 

#include <string>
#include <vector>
#include <algorithm>

using namespace std;

int SkipNum(int num, char s) {
	int skip=0;

	if (num == 0) {
		if (s == 'c')	skip += 4;
		else if (s == 'j')	skip += 5;
		else if (s == 'p')	skip += 7;
	}
	else if (num == 1) {
		if (s == 'b')	skip += 8;
		else if (s == 'f')	skip += 9;
	}
	else if (num == 2) {
		if (s == 'j')	skip += 7;
		else if (s == 's')	skip += 7;
	}
	else if (num == 3) {
		if (s == 'c')	skip += 8;
		else if (s == 'p')	skip += 6;
	}
	return skip;
}

vector<int> solution(vector<string> info, vector<string> query) {
	vector<int> answer;
	vector<int> check[3][2][2][2];

	for (int i = 0; i < query.size(); i++)
		answer.push_back(0);

	int index = 0;
	int num = 0;
	pair<pair<int, int>, pair<int, int>> a;
	for (int i = 0; i < info.size(); i++) {
		index = 0;
		num = 0;

		while(index < info[i].size()){
			if (num == 0) {
				if (info[i][index] == 'c')	a.first.first = 0;
				else if (info[i][index] == 'j')	a.first.first = 1;
				else if (info[i][index] == 'p')	a.first.first = 2;
			}
			else if (num == 1) {
				if (info[i][index] == 'b')	a.first.second = 0;
				else if (info[i][index] == 'f')	a.first.second = 1;
			}
			else if (num == 2) {
				if (info[i][index] == 'j')	a.second.first = 0;
				else if (info[i][index] == 's')	a.second.first = 1;
			}
			else if (num == 3) {
				if (info[i][index] == 'c')	a.second.second = 0;
				else if (info[i][index] == 'p')	a.second.second = 1;
			}
			else {
				int count = atoi(info[i].substr(index, info[i].size() - index).c_str());
				check[a.first.first][a.first.second][a.second.first][a.second.second].push_back(count);
				break;
			}
			index += SkipNum(num, info[i][index]);
			num++;
		}
	}

	for (int i = 0; i < 3; i++) {
		for (int j = 0; j < 2; j++) {
			for (int k = 0; k < 2; k++) {
				for (int l = 0; l < 2; l++) {
					sort(check[i][j][k][l].begin(), check[i][j][k][l].end());
				}
			}
		}
	}

	for (int i = 0; i < query.size(); i++) {
		index = 0;
		num = 0;

		while (index < query[i].size()) {
			bool flag = true;
			if (num == 0) {
				if (query[i][index] == '-') {
					a.first.first = -1; flag = false;
				}
				else if (query[i][index] == 'c')	a.first.first = 0;
				else if (query[i][index] == 'j')	a.first.first = 1;
				else if (query[i][index] == 'p')	a.first.first = 2;
			}
			else if (num == 1) {
				if (query[i][index] == '-') {
					a.first.second = -1; flag = false;
				}
				else if (query[i][index] == 'b')	a.first.second = 0;
				else if (query[i][index] == 'f')	a.first.second = 1;
			}
			else if (num == 2) {
				if (query[i][index] == '-'){
					a.second.first = -1; flag = false;
				}
				else if (query[i][index] == 'j')	a.second.first = 0;
				else if (query[i][index] == 's')	a.second.first = 1;
			}
			else if (num == 3) {
				if (query[i][index] == '-'){ 
					a.second.second = -1; flag = false;
				}
				else if (query[i][index] == 'c')	a.second.second = 0;
				else if (query[i][index] == 'p')	a.second.second = 1;
			}
			else {
				int value = atoi(query[i].substr(index, query[i].size() - index).c_str());

				for (int aa = 0; aa < 3; aa ++) {
					if (a.first.first != -1 && a.first.first != aa)	continue;
					for (int bb = 0; bb < 2; bb++) {
						if (a.first.second != -1 && a.first.second != bb) continue;
						for (int cc = 0; cc < 2; cc++) {
							if (a.second.first != -1 && a.second.first != cc) continue;
							for (int dd = 0; dd < 2; dd++) {
								if (a.second.second != -1 && a.second.second != dd) continue;
								int ans = 0;
								auto low = lower_bound(check[aa][bb][cc][dd].begin(), check[aa][bb][cc][dd].end(), value);
								for (; low != check[aa][bb][cc][dd].end(); ++low) ans++;
								answer[i] += ans;
							}
						}
					}
				}
				break;
			}
			if(flag == true)	index += SkipNum(num, query[i][index]);
			else
			{
				index += 2;
			}
			if(num!= 3) index += 4;
			num++;
		}
	}
	return answer;
}

'카카오 코딩 테스트 풀이' 카테고리의 다른 글

신규 아이디 C++  (0) 2021.03.14
매칭 점수 풀이  (0) 2019.09.04
길 찾기 게임 풀이  (0) 2019.09.03
무지의 먹방 라이브  (0) 2019.09.02
후보키 풀이  (0) 2019.09.02