-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathConsistencyChecker.cpp
72 lines (60 loc) · 1.25 KB
/
ConsistencyChecker.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
/* http://lightoj.com/volume_showproblem.php?problem=1129
#trie
*/
#include<iostream>
#include<string>
using namespace std;
const int MAX = 10;
struct Node {
Node* childNode[MAX];
bool isLeaf;
Node() {
for (int i=0; i < MAX; i++) {
childNode[i] = nullptr;
}
isLeaf = false;
}
};
typedef struct Node TrieNode;
bool addNumber(TrieNode* root, string number) {
TrieNode* pTmp = root;
bool ans = true;
int level;
int idx;
bool addNewNodeFlg = false;
int length = number.length();
for (level=0; level < length; level++) {
idx = number[level] - '0';
if (pTmp->childNode[idx] == nullptr) {
addNewNodeFlg = true;
pTmp->childNode[idx] = new TrieNode;
}
pTmp = pTmp->childNode[idx];
if(pTmp->isLeaf && level < length - 1) {
ans = false;
}
}
pTmp->isLeaf = true;
if(!addNewNodeFlg) {
ans = false;
}
return ans;
}
int main() {
int testcases, numbers;
string number;
cin >> testcases;
for (int t=1; t < testcases+1; t++) {
TrieNode* root = new TrieNode;
bool ans = true;
cin >> numbers;
for (int i=0; i < numbers; i++) {
cin >> number;
if (!addNumber(root, number)) {
ans = false;
}
}
cout << "Case " << t << ": " << (ans ? "YES" : "NO") << endl;
}
return 0;
}