hdu 1671 Phone List
Problem Description
Given a list of phone numbers, determine if it is consistent in the sense that no number is the prefix of another. Let’s say the phone catalogue listed these numbers:
1. Emergency 911
2. Alice 97 625 999
3. Bob 91 12 54 26
In this case, it’s not possible to call Bob, because the central would direct your call to the emergency line as soon as you had dialled the first three digits of Bob’s phone number. So this list would not be consistent.
1. Emergency 911
2. Alice 97 625 999
3. Bob 91 12 54 26
In this case, it’s not possible to call Bob, because the central would direct your call to the emergency line as soon as you had dialled the first three digits of Bob’s phone number. So this list would not be consistent.
Input
The first line of input gives a single integer, 1 <= t <= 40, the number of test cases. Each test case starts with n, the number of phone numbers, on a separate line, 1 <= n <= 10000. Then follows n lines with one unique phone number on each line. A phone number is a sequence of at most ten digits.
Output
For each test case, output “YES” if the list is consistent, or “NO” otherwise.
Sample Input
2
3
911
97625999
91125426
5
113
12340
123440
12345
98346
Sample Output
NO
YES
今天一瞬间感受到算法这个东西不是理解思想就行的.....难啊!加油。。。。
无力吐槽 WA...... 题解很明确,就是寻找是否有一个电话号码是不是别的前缀.....
AC代码:
1 #include <stdio.h> 2 #include <string.h> 3 #include <math.h> 4 #include <algorithm> 5 #include <iostream> 6 #include <ctype.h> 7 #include <iomanip> 8 #include <queue> 9 #include <stdlib.h> 10 using namespace std; 11 #include <iostream> 12 #include <cstdio> 13 #include <cstring> 14 using namespace std; 15 struct node 16 { 17 int count; // 表示某个号码是否出现过 18 node *next[10]; // 每一层出现的号码可能种类 19 node(){ //构造函数 20 count=0; 21 memset(next,0,sizeof(next)); 22 } 23 }; 24 node *root; 25 node *b[10003]; 26 int k=0; 27 void insert(char *a) 28 { 29 int l=strlen(a); 30 node *p=root; 31 int i; 32 for(i=0;i<l;i++) 33 { 34 if(p->next[a[i]-'0']==0) 35 { 36 p->next[a[i]-'0']=new node; 37 } 38 // 已存在此前缀 39 p=p->next[a[i]-'0']; 40 p->count++; 41 } 42 b[k++]=p; 43 } 44 int check(int n) 45 { 46 int i; 47 for(i=0;i<k;i++) 48 { 49 if(b[i]->count!=1) 50 return 1; 51 } 52 return 0; 53 } 54 void de(node *p) 55 { 56 if(p==0) 57 return ; 58 int i; 59 for(i=0;i<10;i++) 60 { 61 de(p->next[i]); 62 } 63 delete p; 64 65 } 66 int main() 67 { 68 int t; 69 scanf("%d",&t); 70 char a[15]; 71 while(t--) 72 { 73 root = new node; 74 int n; 75 k=0; 76 scanf("%d",&n); 77 int i; 78 for(i=0;i<n;i++) 79 { 80 scanf("%s",a); 81 insert(a); 82 } 83 if(check(n)) 84 printf("NO\n"); 85 else 86 printf("YES\n"); 87 de(root); 88 } 89 return 0; 90 }
WA代码:
1 #include <stdio.h> 2 #include <string.h> 3 #include <math.h> 4 #include <algorithm> 5 #include <iostream> 6 #include <ctype.h> 7 #include <iomanip> 8 #include <queue> 9 #include <stdlib.h> 10 using namespace std; 11 12 typedef struct node 13 { 14 int count; 15 struct node *next[26]; 16 }Trienode; 17 18 Trienode *root; 19 Trienode memory[1000000]; 20 int p=0; 21 22 struct node *newset() // 23 { 24 Trienode *current = &memory[p++]; 25 for(int i=0 ;i < 26; i++){ 26 current->next[i]=NULL; 27 } 28 current->count=1; 29 return current; 30 } 31 32 void insert(char *s) // 33 { 34 struct node *current; 35 int len=strlen(s); 36 if(len==0) 37 return ; 38 current= root; 39 for(int i=0 ;i < len; i++){ 40 if(current->next[s[i]-'0']!=NULL){ 41 current = current->next[s[i]-'0']; 42 current->count = current->count+1; 43 } 44 else{ 45 current->next[s[i]-'0'] = newset(); 46 current = current->next[s[i]-'0']; 47 } 48 } 49 } 50 51 int find(char *s) 52 { 53 struct node *current; 54 int len=strlen(s); 55 if(len==0) 56 return 0; 57 current=root; 58 for(int i=0 ;i < len; i++){ 59 if(current->next[s[i]-'0']!=NULL){ 60 current = current->next[s[i]-'0']; 61 } 62 else{ 63 return 0; 64 } 65 } 66 return current->count; 67 } 68 69 void del(Trienode* p) 70 { 71 if(p==0) return ; 72 for(int i=0;i<26;i++) 73 del(p->next[i]); 74 delete p; 75 } 76 77 char str[10002][15]; 78 int main() 79 { 80 int i=0,j,k; 81 int n,m; 82 scanf("%d",&n); 83 while(n--){ 84 for(i = 0 ;i < 10002;i++){ 85 memset(str[i],'\0',sizeof(str[i])); 86 } 87 int flag=1; 88 root=newset(); 89 scanf("%d",&m); 90 for(j=0;j<m;j++){ 91 scanf("%s",str[j]); 92 insert(str[j]); 93 } 94 for(j=0;j<m;j++){ 95 if(find(str[j])>1){ 96 flag=0; 97 break; 98 } 99 } 100 if(flag == 0){ 101 printf("NO\n"); 102 } 103 else{ 104 printf("YES\n"); 105 } 106 del(root); 107 } 108 return 0; 109 }