Skip to content

Commit

Permalink
Merge pull request #155 from AlgoLeadMe/38-Munbin-Lee
Browse files Browse the repository at this point in the history
38-Munbin-Lee
  • Loading branch information
MunbinLee authored Mar 13, 2024
2 parents dc35c20 + a2861d9 commit 44fc5a3
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 |
---
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 44fc5a3

Please sign in to comment.