BOJ 14725 개미굴

1 개요[ | ]

BOJ 14725 개미굴

2 C++[ | ]

#include <bits/stdc++.h>
using namespace std;

int N, K;
string food[16];

struct Trie {
    map<string,Trie*> m;
    ~Trie() {
        map<string,Trie*>::iterator it;
        for (it=m.begin(); it!=m.end(); it++) {
            delete (*it).second;
        }
    }
    void insert(int idx) {
        if (idx == K) return;
        if (m.find(food[idx]) == m.end()) {
            m.insert({food[idx], new Trie()});
        }
        m[food[idx]]->insert(idx+1);
    }
    void find(int depth) {
        map<string,Trie*>::iterator it;
        for (it=m.begin(); it!=m.end(); it++) {
            for (int i=0; i<depth; i++) {
                cout << "--";
            }
            cout << (*it).first << '\n';
            (*it).second->find(depth+1);
        }
    }
};

int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
    Trie *t = new Trie();
    cin >> N;
    for (int i=0; i<N; i++) {
        cin >> K;
        for (int j=0; j<K; j++) {
            cin >> food[j];
        }
        t->insert(0);
    }
    t->find(0);
}
문서 댓글 ({{ doc_comments.length }})
{{ comment.name }} {{ comment.created | snstime }}