반응형
https://programmers.co.kr/learn/courses/30/lessons/17677
빡구현 문제
#include<iostream>
#include <string>
#include<vector>
#include<cmath>
using namespace std;
int solution(string str1, string str2) {
int answer = 0;
for (int i = 0; i < str1.size(); i++)
{
if (str1[i] >= 'A' && str1[i] <= 'Z')
{
str1[i] -= 'A' - 'a';
}
}
for (int i = 0; i < str2.size(); i++)
{
if (str2[i] >= 'A' && str2[i] <= 'Z')
{
str2[i] -= 'A' - 'a';
}
}
vector<pair<string, int> > vec1;
vector<pair<string, int> > vec2;
string temp = "";
for (int i = 0; i < str1.size() - 1; i++)
{
if ((str1[i] >= 'a' && str1[i] <= 'z') && (str1[i + 1] >= 'a' && str1[i + 1] <= 'z'))
{
temp += str1[i];
temp += str1[i + 1];
bool flag = false;
for (int j = 0; j < vec1.size(); j++)
{
if (vec1[j].first == temp)
{
flag = true;
vec1[j].second++;
break;
}
}
if (!flag)
{
vec1.push_back({ temp,1 });
}
temp = "";
}
}
temp = "";
for (int i = 0; i < str2.size() - 1; i++)
{
if ((str2[i] >= 'a' && str2[i] <= 'z') && (str2[i + 1] >= 'a' && str2[i + 1] <= 'z'))
{
temp += str2[i];
temp += str2[i + 1];
bool flag = false;
for (int j = 0; j < vec2.size(); j++)
{
if (vec2[j].first == temp)
{
flag = true;
vec2[j].second++;
break;
}
}
if (!flag)
{
vec2.push_back({ temp,1 });
}
temp = "";
}
}
vector<string> inter;
vector<string> uni;
for (int i = 0; i < vec1.size(); i++)
{
for (int j = 0; j < vec2.size(); j++)
{
if (vec1[i].first == vec2[j].first)
{
int in, un;
if (vec1[i].second > vec2[j].second)
{
in = vec2[j].second;
un = vec1[i].second;
}
else
{
in = vec1[i].second;
un = vec2[j].second;
}
for (int k = 0; k < in; k++)
{
inter.push_back(vec2[j].first);
}
for (int k = 0; k < un; k++)
{
uni.push_back(vec1[i].first);
}
break;
}
}
}
for (int i = 0; i < vec1.size(); i++)
{
bool flag = false;
for (int j = 0; j < uni.size(); j++)
{
if (vec1[i].first == uni[j])
{
flag = true;
break;
}
}
if (!flag)
{
uni.push_back(vec1[i].first);
}
}
for (int i = 0; i < vec2.size(); i++)
{
bool flag = false;
for (int j = 0; j < uni.size(); j++)
{
if (vec2[i].first == uni[j])
{
flag = true;
break;
}
}
if (!flag)
{
uni.push_back(vec2[i].first);
}
}
if (!vec1.size() && !vec2.size() || !uni.size())
{
answer = 65536;
}
else
{
double x = (double)inter.size() / (double)uni.size() ;
answer = (int)(x * 65536);
}
cout << answer << endl;
return answer;
}
반응형
'Algorithm' 카테고리의 다른 글
6782. 현주가 좋아하는 제곱근 놀이 (0) | 2020.05.28 |
---|---|
삼성이의 쇼핑 (0) | 2020.05.28 |
2018 KAKAO BLIND RECRUITMENT[1차] 캐시 (0) | 2020.05.08 |
2018 KAKAO BLIND RECRUITMENT[1차] 다트 게임 (0) | 2020.05.08 |
2018 KAKAO BLIND RECRUITMENT[1차] 비밀지도 (0) | 2020.05.08 |