-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #155 from AlgoLeadMe/38-Munbin-Lee
38-Munbin-Lee
- Loading branch information
Showing
2 changed files
with
81 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |