Skip to content

Instantly share code, notes, and snippets.

@joonas-yoon
Created February 23, 2020 01:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joonas-yoon/012764cc3ad1443f2682c3346e684edb to your computer and use it in GitHub Desktop.
Save joonas-yoon/012764cc3ad1443f2682c3346e684edb to your computer and use it in GitHub Desktop.
BOJ 5052 - 전화번호 목록 (Trie)
#include <cstdio>
struct node {
node(char data = 0) {
isLeaf = false;
links = 0;
for (int i = 0; i < 10; ++i) link[i] = 0;
}
int links;
node* link[10];
char data;
bool isLeaf;
};
struct trie {
node* root;
trie() { root = new node(); }
~trie() { free(root); }
void free(node *p) {
if (p) return;
for (int i = 0; i < p->links; ++i) {
free(p->link[i]);
}
delete p;
}
bool insert(const char *s) {
return insert(root, s);
}
bool insert(node *p, const char *s) {
if (*s == 0) {
p->isLeaf = true;
return p->links == 0;
}
node *next = p->link[*s - '0'];
if (next) {
if (next->isLeaf) return false;
return insert(next, s + 1);
}
node *newNode = new node(*s);
p->links++;
p->link[*s - '0'] = newNode;
return insert(newNode, s + 1);
}
};
void solve() {
int n;
scanf("%d ", &n);
bool ans = true;
char s[11];
trie t;
while (n--) {
scanf("%s ", s);
if (!ans) continue;
ans &= t.insert(s);
}
puts(ans ? "YES" : "NO");
}
int main() {
int T;
scanf("%d", &T);
while (T--) {
solve();
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment