poj3630 Phone List
spy on一下,发现是trie裸题,结果一交就T...
然后把cin改成scanf就A了...
trie的空间一定要开足,要不然RE
1 #include <cstdio> 2 #include <string> 3 #include <iostream> 4 #include <cstring> 5 using namespace std;; 6 const int N = 1000010; 7 char s[15]; 8 // poj 3630 Phone List 9 struct Trie { 10 int tr[N][10]; 11 int top; 12 bool ed[N]; 13 Trie() { 14 top = 1; 15 } 16 inline bool insert() { 17 int p = 1; 18 19 //cout << s << endl; 20 21 for(int i = 0; i < strlen(s) - 1; i++) { 22 int f = s[i] - '0'; 23 if(ed[tr[p][f]]) { 24 return 0; 25 } 26 if(!tr[p][f]) { 27 tr[p][f] = ++top; 28 } 29 p = tr[p][f]; 30 } 31 int f = s[strlen(s) - 1] - '0'; 32 if(tr[p][f]) return 0; 33 tr[p][f] = ++top; 34 p = tr[p][f]; 35 ed[p] = 1; 36 return 1; 37 } 38 void DFS(int p) { 39 for(int i = 0; i <= 9; i++) { 40 if(tr[p][i]) { 41 DFS(tr[p][i]); 42 tr[p][i] = 0; 43 } 44 } 45 if(ed[p]) { 46 ed[p] = 0; 47 } 48 return; 49 } 50 inline void clear() { 51 DFS(1); 52 return; 53 } 54 }trie; 55 56 int main() { 57 int T; 58 scanf("%d", &T); 59 while(T--) { 60 trie.clear(); 61 int n; 62 bool f = 1; 63 scanf("%d", &n); 64 for(int i = 1; i <= n; i++) { 65 scanf("%s", s); 66 if(f) { 67 f = trie.insert(); 68 } 69 } 70 if(f) { 71 printf("YES\n"); 72 } 73 else { 74 printf("NO\n"); 75 } 76 } 77 78 return 0; 79 }