Skip to content

Commit

Permalink
2024-03-08 전화번호 목록
Browse files Browse the repository at this point in the history
  • Loading branch information
MunbinLee committed Mar 7, 2024
1 parent 512b3bf commit a2861d9
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 0 deletions.
1 change: 1 addition & 0 deletions Munbin-Lee/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,5 @@
| 35차시 | 2024.02.18 | 백트래킹 | <a href="https://www.acmicpc.net/problem/24891">단어 마방진</a> | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/140 |
| 36차시 | 2024.02.21 | 문자열 | <a href="https://www.acmicpc.net/problem/15927">회문은 회문아니야!!</a> | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/143 |
| 37차시 | 2024.03.05 | 백트래킹 | <a href="https://school.programmers.co.kr/learn/courses/30/lessons/250136">석유 시추</a> | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/150 |
| 37차시 | 2024.03.08 | 트라이 | <a href="https://www.acmicpc.net/problem/5052">전화번호 목록</a> | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/155 |
---
80 changes: 80 additions & 0 deletions Munbin-Lee/트리/38-전화번호 목록.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
#include <iostream>
#include <vector>

using namespace std;

struct Trie {
struct Node {
Node *children[10]{};
bool isTerminal = false;
};

Node *root = new Node;

bool insert(string &s) const {
auto cur = root;

for (char ch: s) {
int digit = ch - '0';

if (!cur->children[digit]) {
cur->children[digit] = new Node();
cur = cur->children[digit];
continue;
}

if (cur->children[digit]->isTerminal) {
return false;
}

cur = cur->children[digit];
}

for (auto child: cur->children) {
if (child) {
return false;
}
}

cur->isTerminal = true;

return true;
}
};

int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);

auto solve = []() {
auto trie = new Trie;

int n;
cin >> n;

vector<string> numbers(n);

for (auto &number: numbers) {
cin >> number;
}

for (auto number: numbers) {
if (!trie->insert(number)) {
cout << "NO\n";
delete trie;
return;
}
}

cout << "YES\n";
};

int t;
cin >> t;

while (t--) {
solve();
}

return 0;
}

0 comments on commit a2861d9

Please sign in to comment.