본문 바로가기

code/programmers

2018 카카오 블라인드 테스트 5번

문제 링크:

 

카카오 신입 공채 1차 코딩 테스트 문제 해설

‘블라인드’ 전형으로 실시되어 시작부터 엄청난 화제를 몰고 온 카카오 개발 신입 공채. 그 첫 번째 관문인 1차 코딩 테스트가 지난 9월 16일(토) 오후 2시부터 7시까지 장장 5시간 동안 온라인으로 치러졌습니다. 지원자들의 개발 능력을 잘 검증하기 위해 출제 위원들이 한 땀 한 땀 독창적이고 다양한 문제들을 만들어 냈고 문제에 이상은 없는지, 테스트케이스는 정확한지 풀어보고 또 풀어보며 […]

tech.kakao.com

입출력:

TC# 입력 출력
1 FRANCE
french
16384
2 handshake
shake hands
65536
3 aa1+aa2
AAAA12
43690
4 E=M*C^2
e=m*c^2
65536

코드:

// 소요시간 57분

/* 1. alphabet을 제외한 문자를 제거하지 말고 애초에 받지 않으면 된다. */
/* 2. !alphabet도 입력 받아야 양 옆 문자와 조합해서 만든 key를 제외시킬 수 있다. */

#include <iostream>
#include <string>
#include <unordered_map>
#include <algorithm>

using namespace std;

string str1, str2;
unordered_map<string, int> setA, setB;
int set_sizeA, set_sizeB;

void ch_to_str(char ch, string &str) {	/* 1 */
	if ('a' <= ch && ch <= 'z')
		str += ch;
	else if ('A' <= ch && ch <= 'Z')
		str += ch + ('a' - 'A');
	else	/* 2 */
		str += '0';
}

void str_to_set(string str, unordered_map<string, int> &set, int &set_size) {
	for (int i = 1; i < str.size(); ++i) {
		if (str[i - 1] == '0' || str[i] == '0') {	/* 2 */
			continue;
		}

		string key;
		key += str[i - 1];
		key += str[i];

		++set_size;
		if (set.find(key) != set.end()) {
			++set[key];
		}
		else {
			set[key] = 1;
		}
	}
}

int A_dif_B(unordered_map<string, int> setA, unordered_map<string, int> setB) {
	int dif = 0;
	for (auto elemA : setA) {
		auto elemB = setB.find(elemA.first);
		if (elemB != setB.end()) {	// match
			dif += max(0, elemA.second - (*elemB).second);
		}
		else {	// miss
			dif += elemA.second;
		}
	}
	return dif;
}

int main() {
	ios_base::sync_with_stdio(0); cin.tie(0);
	int ch;
	while ((ch = cin.get()) != '\n') {
		ch_to_str(ch, str1);
	}
	while (!cin.eof()) {
		ch_to_str(cin.get(), str2);
	}

	str_to_set(str1, setA, set_sizeA);
	str_to_set(str2, setB, set_sizeB);

	int dif = A_dif_B(setA, setB);
	int itr = set_sizeA - dif;
	int un = set_sizeB + dif;

	if (un == 0)
		cout << 65536;
	else
		cout << (itr * 65536) / un;

	return 0;
}