[Programmers] Lv2. ๋‰ด์Šค ํด๋Ÿฌ์Šคํ„ฐ๋ง | C++

2023. 6. 20. 17:03ใ†Coding Test/Programmers

๐Ÿ”—๋ฌธ์ œ ๋ณด๋Ÿฌ๊ฐ€๊ธฐ
 

ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค

์ฝ”๋“œ ์ค‘์‹ฌ์˜ ๊ฐœ๋ฐœ์ž ์ฑ„์šฉ. ์Šคํƒ ๊ธฐ๋ฐ˜์˜ ํฌ์ง€์…˜ ๋งค์นญ. ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค์˜ ๊ฐœ๋ฐœ์ž ๋งž์ถคํ˜• ํ”„๋กœํ•„์„ ๋“ฑ๋กํ•˜๊ณ , ๋‚˜์™€ ๊ธฐ์ˆ  ๊ถํ•ฉ์ด ์ž˜ ๋งž๋Š” ๊ธฐ์—…๋“ค์„ ๋งค์นญ ๋ฐ›์œผ์„ธ์š”.

programmers.co.kr

 

๐Ÿ‘จ‍๐Ÿ’ปํ’€์ด ๊ณผ์ •

 

์ค‘๋ณต์ด ๊ฐ€๋Šฅํ•œ ์›์†Œ๋“ค์„ ์ง€๋‹ˆ๊ณ  ์žˆ์–ด์•ผ ํ•˜๋ฏ€๋กœ, 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. ๋‹ค์ค‘์ง‘ํ•ฉ์˜ ๊ต์ง‘ํ•ฉ, ํ•ฉ์ง‘ํ•ฉ ๊ตฌํ•˜๊ธฐ

#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
๋ฐ˜์‘ํ˜•