-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path11362.cpp
55 lines (48 loc) · 1.4 KB
/
11362.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
#include <iostream>
#include <string>
struct Digit {
bool is_end;
Digit *next[10];
};
int main(void)
{
bool valid;
int num_case, num_phone, x;
std::string buffer;
std::cin >> num_case;
while (num_case--) {
std::cin >> num_phone;
valid = true;
Digit *head = new (Digit);
head->is_end = false;
for (int i = 0; i < 10; ++i)
head->next[i] = NULL;
Digit *curr;
while (num_phone--) {
std::cin >> buffer;
if (valid) {
curr = head;
for (int i = 0; i < buffer.size(); ++i) {
x = int(buffer[i] - '0');
if (curr->next[x] == NULL) {
curr->next[x] = new (Digit);
curr = curr->next[x];
curr->is_end = i + 1 == buffer.size();
for (int i = 0; i < 10; ++i)
curr->next[i] = NULL;
}
else {
curr = curr->next[x];
if (curr->is_end || i + 1 == buffer.size())
valid = false;
}
}
}
}
if (valid)
std::cout << "YES" << std::endl;
else
std::cout << "NO" << std::endl;
}
return 0;
}