PS

BOJ 4273 : Card Hands

lickelon 2024. 10. 21. 22:47
  • 문제 링크 : boj.kr/4273
  • 난이도 : P4
  • 태그 : 트라이

코드

#include <bits/stdc++.h>

#define all(x) (x).begin(), (x).end()

#define INF 0x7FFFFFFF

using namespace std;

using ll = long long;
using ld = long double;
using pii = pair<int,int>;
using pll = pair<ll, ll>;

class Trie {
public:
    unordered_map<string, Trie*> children;
    bool end;

    Trie() : end(false) {}

    void insert(vector<string>& key) {
        this->_insert(key, 0);
    };
    bool find(vector<string>& key) {
        return this->_find(key, 0);
    };

private:
    void _insert(vector<string>& key, int index) {
        if(index == key.size()) {
            end = true;
            return;
        }
        string next = key[index];
        if(children.find(next) == children.end()) children[next] = new Trie;
        children[next]->_insert(key, index + 1);
    }
    bool _find(vector<string>& key, int depth) {
        if(depth == key.size()) return end;
        string next = key[depth];
        if(children.find(next) == children.end()) return false;
        return children[next]->_find(key, depth + 1);
    }
};

int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);

    while(true) {
        int n;
        cin >> n;
        if(n == 0) break;
        Trie root;
        for(int i = 0; i < n; i++) {
            int k;
            cin >> k;
            vector<string> arr(k);
            for(auto &u : arr) cin >> u;
            reverse(all(arr));
            root.insert(arr);
        }

        queue<Trie*> _q;
        _q.push(&root);
        int ans = 0;
        while(!_q.empty()) {
            auto curr = _q.front(); _q.pop();
            ans++;
            for(auto &u : curr->children) {
                _q.push(u.second);
            }
        }
        cout << ans-1 << "\n";
    }

    return 0;
}

풀이

해시맵을 사용하는 트라이를 구현한 후, 손패의 앞뒤를 뒤집은 배열을 트라이에 넣어준다.

이후 트라이에 존재하는 노드의 수를 세어주면 된다.

'PS' 카테고리의 다른 글

BOJ 29441 : XOR  (1) 2024.10.23
BOJ 30865 : XOR 쿼리  (0) 2024.10.22
BOJ 17306 : 전쟁 중의 삶  (2) 2024.10.20
BOJ 6073 : Secret Message  (0) 2024.10.19
BOJ 13505 : 두 수 XOR  (0) 2024.10.18