Phone List
Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 19234 Accepted Submission(s): 6504
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/* 3 97625999 91125426 911*/这种情况也要考虑#define _CRT_SECURE_NO_WARNINGS #include <stdio.h> #include <stdlib.h> #define MAXPHONELEN 10 //#define sizeof_v(var) ((size_t) ((char *)(&(var) + 1) - (char *)&(var))) typedef struct node { int count; //是结尾的个数 node* next[10]; //10个数字 }; int flag = 0;//没有是前缀的 struct node* head; struct node* trie_creat(char* str, int len) { int i = 0; int j = 0; int index = 0; struct node* p1; struct node* p2; p1 = head; for (i = 0; i<len; i++) { index = str[i] - '0'; if (NULL == p1->next[index]) //说明这个数字没出现过 { p2 = (node*)malloc(sizeof(struct node)); if (i == (len - 1)) { p2->count = 1; } else { p2->count = 0; } for (j = 0; j<MAXPHONELEN; j++) { p2->next[j] = NULL; } p1->next[index] = p2; p1 = p1->next[index]; } else { //这个数字在这个位置曾经出现过尾数 if (1 == p1->next[index]->count) { flag = 1; } /* 3 97625999 91125426 911*/ //这种情况一开始没考虑到 if (i == (len-1)) //这个之前有写的,但是删掉了可惜 { flag = 1; } p1 = p1->next[index]; } } return head; } int length(char *str) { int len = 0; if (NULL == str) { return 0; } while ('\0' != *str) { len += 1; str++; } return len; } void trie_free(struct node* p) { struct node* p1; int i = 0; if (NULL == p) return; //这个是借鉴别人的 for (i = 0; i < MAXPHONELEN;i++) { trie_free(p->next[i]); } free(p); return; } int main() { //字典树问题,就是任意一个号码是不是别的号码的前缀 int i = 0; int j = 0; int k = 0; int m = 0; int tc = 0; int num = 0; char str[MAXPHONELEN]; int len = 0; freopen("input.txt", "r", stdin); scanf("%d", &tc); for (i = 0; i<tc; i++) { flag = 0; scanf("%d", &num); //头结点初始化 head = (node*)malloc(sizeof(struct node)); head->count = -1; for (k = 0; k<MAXPHONELEN; k++) { head->next[k] = NULL; } for (j = 0; j<num; j++) { scanf("%s", str); //开始创建字典树 len = length(str); head = trie_creat(str, len); } if (1 == flag) { printf("NO\n"); } else { printf("YES\n"); } //释放trie trie_free(head); } return 0; }