[Programmers] Lv2. ๋ด์ค ํด๋ฌ์คํฐ๋ง | C++
2023. 6. 20. 17:03ใCoding Test/Programmers
๐๋ฌธ์ ๋ณด๋ฌ๊ฐ๊ธฐ
๐จ๐ปํ์ด ๊ณผ์
์ค๋ณต์ด ๊ฐ๋ฅํ ์์๋ค์ ์ง๋๊ณ ์์ด์ผ ํ๋ฏ๋ก, multiset์ ์ฌ์ฉํ์์ต๋๋ค.
1. ์ฃผ์ด์ง ๋ฌธ์์ด์ ๋ ๊ธ์ง์ฉ ๋์ด, ๋ค์ค์งํฉ ๋ง๋ค๊ธฐ
- ๋์ด์ ๋ง๋ ๋ ๊ธ์ ์ค ํ๋๋ผ๋ ์ํ๋ฒณ์ด ์๋๋ผ๋ฉด ๋ค์ ๋ฌธ์๋ก ๋์ด๊ฐ๋ค.
- ๋ ๊ธ์ ๋ชจ๋ ์ํ๋ฒณ์ด๋ผ๋ฉด ๋ค์ค์งํฉ์ ์์ ์ถ๊ฐ
#include <string>
#include <set>
using namespace std;
multiset<string> CreateMultipleSet(const string& str) {
multiset<string> result;
int strLength = str.length() - 1;
int extractionNum = 2;
for (int i = 0; i < strLength; i++) {
string element;
bool isDiscardSet = false;
for (int j = i; j < i + extractionNum; j++) {
if (!isalpha(str[j])) {
isDiscardSet = true;
break;
}
element.push_back(tolower(str[j]));
}
if (!isDiscardSet)
result.insert(element);
}
return result;
}
2. ๋ค์ค์งํฉ์ ๊ต์งํฉ, ํฉ์งํฉ ๊ตฌํ๊ธฐ
- <algorithm>๊ณผ <iterator> ํค๋์ ์๋ set_union(), set_intersection(), inserter()๋ฅผ ํ์ฉ
- inserter()์ ๋ํ ๐์ฐธ๊ณ ๊ธ
- set_intersection(), set_union()์ ๋ํ ๐์ฐธ๊ณ ๊ธ
#include <string>
#include <iterator>
#include <algorithm>
#include <set>
using namespace std;
multiset<string> SetIntersection(const multiset<string>& set1, const multiset<string>& set2) {
multiset<string> result;
set_intersection(set1.begin(), set1.end(), set2.begin(), set2.end(), inserter(result, result.begin()));
return result;
}
multiset<string> SetUnion(const multiset<string>& set1, const multiset<string>& set2) {
multiset<string> result;
set_union(set1.begin(), set1.end(), set2.begin(), set2.end(), inserter(result, result.begin()));
return result;
}
3. ์์นด๋ ์ ์ฌ๋ ๊ตฌํ๊ธฐ
const int MULTIPLY = 65536;
int FindJaccardSimilarity(const multiset<string>& intersections, const multiset<string>& unions) {
if (intersections.empty() and unions.empty())
return 1 * MULTIPLY;
return ((double)intersections.size() / unions.size()) * MULTIPLY;
}
โ๏ธ์์ค ์ฝ๋ ๋ฐ ๊ฒฐ๊ณผ
#include <string>
#include <iterator>
#include <algorithm>
#include <set>
using namespace std;
const int MULTIPLY = 65536;
/* https://stackoverflow.com/questions/41782233/i-want-to-perform-a-multi-set-intersection-using-c */
multiset<string> CreateMultipleSet(const string& str) {
multiset<string> result;
int strLength = str.length() - 1;
int extractionNum = 2;
for (int i = 0; i < strLength; i++) {
string element;
bool isDiscardSet = false;
for (int j = i; j < i + extractionNum; j++) {
if (!isalpha(str[j])) {
isDiscardSet = true;
break;
}
element.push_back(tolower(str[j]));
}
if (!isDiscardSet)
result.insert(element);
}
return result;
}
multiset<string> GetIntersection(const multiset<string>& set1, const multiset<string>& set2) {
multiset<string> result;
set_intersection(set1.begin(), set1.end(), set2.begin(), set2.end(), inserter(result, result.begin()));
return result;
}
multiset<string> GetUnion(const multiset<string>& set1, const multiset<string>& set2) {
multiset<string> result;
set_union(set1.begin(), set1.end(), set2.begin(), set2.end(), inserter(result, result.begin()));
return result;
}
int FindJaccardSimilarity(const multiset<string>& intersections, const multiset<string>& unions) {
if (intersections.empty() and unions.empty())
return 1 * MULTIPLY;
return ((double)intersections.size() / unions.size()) * MULTIPLY;
}
int solution(string str1, string str2) {
multiset<string> multiSet1 = CreateMultipleSet(str1);
multiset<string> multiSet2 = CreateMultipleSet(str2);
auto intersections = GetIntersection(multiSet1, multiSet2);
auto unions = GetUnion(multiSet1, multiSet2);
int answer = FindJaccardSimilarity(intersections, unions);
return answer;
}
728x90
๋ฐ์ํ
'Coding Test > Programmers' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Programmers] Lv2. k์ง์์์ ์์ ๊ฐ์ ๊ตฌํ๊ธฐ | C++ (0) | 2023.06.21 |
---|---|
[Programmers] Lv2. ํ๊ฒ ๋๋ฒ | C++ (0) | 2023.06.20 |
[Programmers] Lv2. ๊ธฐ๋ฅ๊ฐ๋ฐ | C++ (0) | 2023.06.16 |
[Programmers] Lv2. ์์ | C++ (0) | 2023.06.15 |
[Programmers] Lv2. ํ๋ ฌ์ ๊ณฑ์ | C++ (0) | 2023.06.15 |