[BOJ] 14725๋ฒˆ | ๊ฐœ๋ฏธ๊ตด (C++)

2024. 2. 15. 19:00ใ†Coding Test/BOJ

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

14725๋ฒˆ: ๊ฐœ๋ฏธ๊ตด

์ฒซ ๋ฒˆ์งธ ์ค„์€ ๋กœ๋ด‡ ๊ฐœ๋ฏธ๊ฐ€ ๊ฐ ์ธต์„ ๋”ฐ๋ผ ๋‚ด๋ ค์˜ค๋ฉด์„œ ์•Œ๊ฒŒ ๋œ ๋จน์ด์˜ ์ •๋ณด ๊ฐœ์ˆ˜ N (1 ≤ N ≤ 1000)๊ฐœ๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. ๋‘ ๋ฒˆ์งธ ์ค„๋ถ€ํ„ฐ N+1 ๋ฒˆ์งธ ์ค„๊นŒ์ง€, ๊ฐ ์ค„์˜ ์‹œ์ž‘์€ ๋กœ๋ด‡ ๊ฐœ๋ฏธ ํ•œ๋งˆ๋ฆฌ๊ฐ€ ๋ณด๋‚ด์ค€ ๋จน์ด ์ •

www.acmicpc.net

 

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

 

์ถœ์ฒ˜ : ๋ฉ”์ดํ”Œ์Šคํ† ๋ฆฌ ์œ„ํ‚ค - ๊ฐœ๋ฏธ๊ตด 1

 

๋ฉ”์ดํ”Œ์Šคํ† ๋ฆฌ ๊ฐœ๋ฏธ๊ตด์ด ์ƒ๊ฐ๋‚˜๋Š” ๋ฌธ์ œ์˜€์Šต๋‹ˆ๋‹ค. ํŠธ๋ผ์ด(Trie) ์ž๋ฃŒ๊ตฌ์กฐ๋ฅผ ํ™œ์šฉํ•ด์„œ ํ’€ ์ˆ˜ ์žˆ์—ˆ๋Š”๋ฐ, ํŠธ๋ผ์ด์™€ ๊ด€๋ จ๋œ ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•˜๋Š” ๊ฒŒ ์ต์ˆ™ํ•˜์ง€ ์•Š์•„ ์กฐ๊ธˆ ์˜ค๋ž˜ ๊ฑธ๋ ธ์Šต๋‹ˆ๋‹ค. ๊ฐ™์€ ์ธต์— ์—ฌ๋Ÿฌ ๊ฐœ์˜ ๋ฐฉ์ด ์žˆ์„ ๊ฒฝ์šฐ์—๋Š” ์‚ฌ์ „์ˆœ์œผ๋กœ ๋จน์ด ์ •๋ณด๋ฅผ ์ถœ๋ ฅํ•ด์•ผ ํ•˜๊ธฐ์— map์„ ํ™œ์šฉํ•˜์—ฌ ํŠธ๋ผ์ด๋ฅผ ๊ตฌํ˜„ํ•˜์˜€์Šต๋‹ˆ๋‹ค.

class Trie
{
public:
    Trie(int depth) : depth(depth) { }
    ~Trie();
    void Insert(const vector<string>& foodNames, int index);
    void Show();

private:
    int depth;
    map<string, Trie*> foods;
};

 

๋™์  ํ• ๋‹น์œผ๋กœ ํŠธ๋ผ์ด ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•ด์ค„ ๊ฒƒ์ด๊ธฐ ๋•Œ๋ฌธ์—, ์†Œ๋ฉธ์ž์—์„œ ๋ฉ”๋ชจ๋ฆฌ ํ•ด์ œ ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•ด์ค๋‹ˆ๋‹ค.

Trie::~Trie()
{
    for (auto& food : foods)
        if (food.second != nullptr)
            delete food.second;
}

 

๋กœ๋ด‡ ๊ฐœ๋ฏธ๊ฐ€ ๋ฐฉ์— ๋“ค์–ด๊ฐ„ ์ˆœ์„œ๋Œ€๋กœ ์ž…๋ ฅ์„ ์ฃผ๊ธฐ ๋•Œ๋ฌธ์—, ์ด๋ฅผ ๋ฐฐ์—ด์— ๋ฐ›์•„์„œ ์žฌ๊ท€ ํ˜•ํƒœ๋กœ ํŠธ๋ผ์ด์— ๋„ฃ๋Š” ๋ฐฉ์‹์œผ๋กœ ๊ตฌํ˜„ํ•˜์˜€์Šต๋‹ˆ๋‹ค.

void Trie::Insert(const vector<string>& foodNames, int index)
{
    if (index >= foodNames.size())
        return;
    
    auto findIt = foods.find(foodNames[index]);
    if (findIt == foods.end())
    {
        Trie* newFood = new Trie(depth + 1);
        foods.insert(make_pair(foodNames[index], newFood));
        newFood->Insert(foodNames, index + 1);
        return;
    }

    Trie* nextFood = findIt->second;
    nextFood->Insert(foodNames, index + 1);
}

 

์ถœ๋ ฅํ•˜๋Š” ์ฝ”๋“œ ๋˜ํ•œ, ํ˜„์žฌ ๊นŠ์ด(depth) * 2์— ํ•ด๋‹นํ•˜๋Š” ๊ฐœ์ˆ˜๋งŒํผ "-"์„ ๋จผ์ € ์ถœ๋ ฅํ•ด์ฃผ๊ณ , DFS ๋ฐฉ์‹์œผ๋กœ ์ถœ๋ ฅํ•ด์ฃผ๋ฉด ๋ฉ๋‹ˆ๋‹ค.

void Trie::Show()
{
    string line(2 * depth, '-');
    for (const auto& food : foods)
    {
        cout << line << food.first << "\n";
        food.second->Show();
    }
}

 

 

โœ๏ธ์†Œ์Šค ์ฝ”๋“œ ๋ฐ ๊ฒฐ๊ณผ

#include <iostream>
#include <vector>
#include <string>
#include <map>
using namespace std;

class Trie
{
public:
    Trie(int depth) : depth(depth) { }
    ~Trie();
    void Insert(const vector<string>& foodNames, int index);
    void Show();

private:
    int depth;
    map<string, Trie*> foods;
};

Trie::~Trie()
{
    for (auto& food : foods)
        if (food.second != nullptr)
            delete food.second;
}

void Trie::Insert(const vector<string>& foodNames, int index)
{
    if (index >= foodNames.size())
        return;
    
    auto findIt = foods.find(foodNames[index]);
    if (findIt == foods.end())
    {
        Trie* newFood = new Trie(depth + 1);
        foods.insert(make_pair(foodNames[index], newFood));
        newFood->Insert(foodNames, index + 1);
        return;
    }

    Trie* nextFood = findIt->second;
    nextFood->Insert(foodNames, index + 1);
}

void Trie::Show()
{
    string line(2 * depth, '-');
    for (const auto& food : foods)
    {
        cout << line << food.first << "\n";
        food.second->Show();
    }
}

int main()
{
    ios::sync_with_stdio(false); cout.tie(NULL); cin.tie(NULL);
    int numOfData;
    cin >> numOfData;
    
    Trie* root = new Trie(0);

    while (numOfData--)
    {
        int N;
        cin >> N;

        vector<string> foods(N);
        for (int i = 0; i < N; i++) cin >> foods[i];

        root->Insert(foods, 0);
    }

    root->Show();
    delete root;
    return 0;
}

 

 

728x90
๋ฐ˜์‘ํ˜•