#include <bits/stdc++.h>
using namespace std;
const int childMax = 26;
const char baseChar = 'A';
struct Trie {
Trie* child[childMax]; // 'A' ~ 'Z'
bool isRet, isParent;
Trie() {
fill(child, child + childMax, nullptr);
isRet = isParent = false;
}
~Trie() {
for (int i = 0; i < childMax; ++i) {
if (child[i]) delete child[i];
}
}
void insert(const char* key) {
if (*key == '\0') isRet = true;
else {
int next = *key - baseChar;
if (!child[next]) child[next] = new Trie;
isParent = true;
child[next]->insert(key + 1);
}
}
bool isConsistent() {
if (isParent && isRet) return false;
for (int i = 0; i < childMax; ++i)
if (child[i] && !child[i]->isConsistent()) return false;
return true;
}
};
int main() {
ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
int n; cin >> n;
Trie* root = new Trie;
for (int i = 0; i < n; ++i) {
string tel; cin >> tel;
root->insert(tel.c_str());
}
cout << (root->isConsistent() ? "YES" : "NO") << "\n";
delete root;
return 0;
}
BOJ https://www.acmicpc.net/problem/5052
t번의 테스트는 제거하였다.
'알고리즘 > 라이브러리' 카테고리의 다른 글
Suffix Array와 LCP Array (0) | 2020.09.23 |
---|---|
Topological Sort (0) | 2020.09.16 |
Floyd-Warshall (0) | 2020.08.15 |
KMP (0) | 2020.08.11 |
Dijkstra (0) | 2020.08.06 |
댓글