首页 > 代码库 > HDU - 1671 Phone List

HDU - 1671 Phone List

技术分享
 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 typedef long long ll;
 4 struct Node {
 5     int next[10];
 6     int num;
 7 } node[100005];
 8 int len = 0;
 9 bool add(string& s) {
10     int p = 0;
11     bool f = false;
12     for(int i = 0; i < s.size(); i++) {
13         if(node[p].next[s[i] - 0] == -1) {
14             node[p].next[s[i] - 0] = ++len;
15             memset(node[len].next, -1, sizeof node[len].next);
16             node[len].num = 0;
17             p = len;
18             f = true;
19         }
20         else {
21             p = node[p].next[s[i] - 0];
22             if(node[p].num > 0) return false;
23         }
24     }
25     node[p].num++;
26     return f;
27 }
28 int main() {
29     int T;
30     cin >> T;
31     while(T--) {
32         len = 0;
33         memset(node[0].next, -1, sizeof node[0].next);
34         node[0].num = 0;
35         int n;
36         cin >> n;
37         string s;
38         bool f = true;
39         for(int i = 1; i <= n; i++) {
40             cin >> s;
41             if(f) f = add(s);
42         }
43         if(f) cout << "YES" << endl;
44         else cout << "NO" << endl;
45     }
46     return 0;
47 }
View Code

 

HDU - 1671 Phone List