2018 KAKAO BLIND RECRUITMENT (2018 카카오 블라인드 채용 문제)
두개의 문자열을 '자카드 유사도' 방법을 통해서 유사도를 찾는 문제입니다.
문제 해결 방법
1. 입력 받은 문자열을 소문자로 변경
2. 문자열을 2글자씩 끊어서 저장 (특수문자가 포함된 경우 패스) (s1, s2라고 지칭)
3. 2.에서 저장된 문자열들(s1, s2)끼리 비교 (같은게 있을 경우 카운팅(count), 체크 = 중복 체크 제거)
4. 합집합 = s1.size + s2.size - count, 교집합 = count
유사도 = count / (s1.size() + s2.size() - count) * 65536;
코드
#include <iostream>
#include <vector>
#include <string>
using namespace std;
int solution(string str1, string str2)
{
vector<pair<string, bool>> s1;
vector<pair<string, bool>> s2;
double count = 0;
int answer = 0;
for (int i = 0; i < str1.length(); ++i)
{
str1[i] = tolower(str1[i]);
}
for (int i = 0; i < str2.length(); ++i)
{
str2[i] = tolower(str2[i]);
}
for (int i = 0; i < str1.length() - 1; ++i)
{
string temp = "";
if (str1[i] >= 'a' && str1[i] <= 'z' && str1[i + 1] >= 'a' && str1[i + 1] <= 'z')
{
temp += str1[i];
temp += str1[i + 1];
s1.push_back({temp, false});
}
}
for (int i = 0; i < str2.length() - 1; ++i)
{
string temp = "";
if (str2[i] >= 'a' && str2[i] <= 'z' && str2[i + 1] >= 'a' && str2[i + 1] <= 'z')
{
temp += str2[i];
temp += str2[i + 1];
s2.push_back({temp, false});
}
}
for (int i = 0; i < s1.size(); ++i)
{
for (int j = 0; j < s2.size(); ++j)
{
if (s1[i].second || s2[j].second)
{
continue;
}
if (s1[i].first == s2[j].first)
{
s1[i].second = true;
s2[j].second = true;
++count;
}
}
}
if (s1.size() == 0 && s2.size() == 0)
{
return 65536;
}
answer = count / (s1.size() + s2.size() - count) * 65536;
return answer;
}
'알고리즘' 카테고리의 다른 글
[C++] 프로그래머스 - 캐시 (0) | 2021.05.14 |
---|---|
[C++] 프로그래머스 - 프렌즈 4블록 (0) | 2021.05.14 |
[C++] 프로그래머스 - 오픈채팅방 (0) | 2021.05.07 |
[C++] 프로그래머스 - 다트 게임 (0) | 2021.05.04 |
[C++] 프로그래머스 - 비밀지도 (0) | 2021.04.30 |