hdu1671 Phone List [字典树 hash]
Phone List
Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 11633 Accepted Submission(s): 3965
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
Source
Recommend
前面一个是字典树
12942795 | 2015-02-13 09:56:42 | Accepted | 1671 | 249MS | 4896K | 2261 B | G++ | czy |
12940774 | 2015-02-12 19:16:57 | Accepted | 1671 | 780MS | 3940K | 1566 B | G++ | czy |
自己写的很丑的字典树:
1 #include<iostream> 2 #include<cstring> 3 #include<cstdlib> 4 #include<cstdio> 5 #include<algorithm> 6 #include<cmath> 7 #include<queue> 8 #include<map> 9 #include<set> 10 #include<stack> 11 #include<string> 12 13 #define N 100005 14 #define M 205 15 #define mod 10000007 16 //#define p 10000007 17 #define mod2 1000000000 18 #define ll long long 19 #define LL long long 20 #define eps 1e-6 21 #define inf 100000000 22 #define maxi(a,b) (a)>(b)? (a) : (b) 23 #define mini(a,b) (a)<(b)? (a) : (b) 24 25 using namespace std; 26 27 int n; 28 int T; 29 int flag; 30 31 typedef struct 32 { 33 int v; 34 vector<int>nt; 35 }PP; 36 37 PP p[N]; 38 int cnt[N]; 39 40 int tot; 41 vector<int>::iterator it; 42 43 void insert(char s[]) 44 { 45 int l=strlen(s); 46 int i; 47 int ff; 48 int st=0; 49 int y; 50 for(i=0;i<l;i++){ 51 ff=0; 52 for(it=p[st].nt.begin();it!=p[st].nt.end();it++){ 53 y=*it; 54 if(p[y].v==s[i]-'0'){ 55 ff=1; 56 st=y; 57 break; 58 } 59 } 60 if(ff==0){ 61 p[st].nt.push_back(tot); 62 p[tot].v=s[i]-'0'; 63 p[tot].nt.clear(); 64 st=tot; 65 tot++; 66 } 67 } 68 cnt[st]++; 69 } 70 71 void ini() 72 { 73 memset(cnt,0,sizeof(cnt)); 74 flag=1; 75 p[0].nt.clear(); 76 p[0].v=-1; 77 tot=1; 78 scanf("%d",&n); 79 char s[105]; 80 while(n--){ 81 scanf("%s",s); 82 insert(s); 83 } 84 } 85 86 int check() 87 { 88 int st=0; 89 queue<int>q; 90 q.push(st); 91 int te; 92 int y; 93 while(q.size()>=1) 94 { 95 te=q.front(); 96 q.pop(); 97 if(p[te].nt.size()>=1 && cnt[te]>=1){ 98 return 0; 99 } 100 for(it=p[te].nt.begin();it!=p[te].nt.end();it++){ 101 y=*it; 102 q.push(y); 103 } 104 } 105 return 1; 106 } 107 108 void solve() 109 { 110 flag=check(); 111 } 112 113 void out() 114 { 115 if(flag==0){ 116 printf("NO\n"); 117 } 118 else{ 119 printf("YES\n"); 120 } 121 } 122 123 int main() 124 { 125 //freopen("data.in","r",stdin); 126 //freopen("data.out","w",stdout); 127 scanf("%d",&T); 128 //for(int ccnt=1;ccnt<=T;ccnt++) 129 while(T--) 130 //scanf("%d%d",&n,&m); 131 //while(scanf("%d",&n)!=EOF) 132 { 133 ini(); 134 solve(); 135 out(); 136 } 137 return 0; 138 }
hash:
另一种解法:转自:http://fszxwfy.blog.163.com/blog/static/44019308201282484625551/
大致题意:多组电话号码,如果存在某组是其他组的前缀则输出NO
看了解题报告都说用字典树做,个人看了下题目数据突然想到一种巧法。
首先将所有的号码当做 long long 型存入数组,读完后进行排序,再从小到大读出每个数,并用map进行标记以示这个数出现过,然后在对这个数不断尽心除10操作,判断除10后的数有没有被标记,bingo..
不过WA了,后来想想看应该是有些号码前面是0,这样就不行了,不过后来一想可以将所有的数前面都加个1不就解决了。。。。
然后然后就这样ac了。。。。。 同学也在做 他都受不了 哈哈哈哈
1 #include<iostream> 2 #include<cstring> 3 #include<cstdlib> 4 #include<cstdio> 5 #include<algorithm> 6 #include<cmath> 7 #include<queue> 8 #include<map> 9 #include<set> 10 #include<stack> 11 #include<string> 12 13 #define N 20005 14 #define M 205 15 #define mod 10000007 16 //#define p 10000007 17 #define mod2 1000000000 18 #define ll long long 19 #define LL long long 20 #define eps 1e-6 21 #define inf 100000000 22 #define maxi(a,b) (a)>(b)? (a) : (b) 23 #define mini(a,b) (a)<(b)? (a) : (b) 24 25 using namespace std; 26 27 int n; 28 int T; 29 map<ll,bool>mp; 30 int flag; 31 32 void ini() 33 { 34 flag=1; 35 scanf("%d",&n); 36 mp.clear(); 37 char s[105]; 38 int l; 39 int i; 40 queue<ll>q; 41 ll tt=1; 42 while(n--){ 43 scanf("%s",s); 44 l=strlen(s); 45 tt=1; 46 for(i=0;i<l;i++){ 47 tt=tt*10+s[i]-'0'; 48 } 49 q.push(tt); 50 mp[tt]=true; 51 } 52 ll te; 53 while(q.size()>=1){ 54 te=q.front(); 55 q.pop(); 56 while(te>0){ 57 te=te/10; 58 if(mp[te]==true){ 59 flag=0;return; 60 } 61 } 62 //q.push(nt); 63 } 64 65 } 66 67 void solve() 68 { 69 70 } 71 72 void out() 73 { 74 if(flag==0){ 75 printf("NO\n"); 76 } 77 else{ 78 printf("YES\n"); 79 } 80 } 81 82 int main() 83 { 84 //freopen("data.in","r",stdin); 85 //freopen("data.out","w",stdout); 86 scanf("%d",&T); 87 //for(int ccnt=1;ccnt<=T;ccnt++) 88 while(T--) 89 //scanf("%d%d",&n,&m); 90 //while(scanf("%d",&n)!=EOF) 91 { 92 ini(); 93 solve(); 94 out(); 95 } 96 return 0; 97 }