Phone number viewing statistics submitted question total time limit: 1000 ms memory limit: 65536kb description
For some phone numbers, check whether they are consistent, that is, whether a phone number is the prefix of another phone number. For example:
Emergency 911
Alice 97, 625, 999
Bob 91 12 54 26
In this example, we cannot call Bob because the emergency phone is its prefix.
Emergency will be connected first, so these phone numbers are not consistent.
Input
The first line is an integer T, 1 ≤ T ≤ 40, indicating the number of test data.
The first line of each test example is an integer N, 1 ≤ n ≤ 10000, And the next n rows are a phone number of no more than 10 digits.
Output
For each test data, if the output is consistent with "yes", if the output is not "no ".
Sample Input
2
3
911
97625999
91125426
5
113
12340
123440
12345
98346
Sample output
No
Yes
Simple trie tree. Note two cases: Insert a short string before inserting a long string, and insert a long string before inserting a short string.
#include <stdio.h>#include <string.h>#include <stdlib.h>struct Node{ struct Node *next[10]; int isCover, isEnd;};int ok;void clean(Node *p){ memset(p->next, 0, sizeof(p->next)); p->isCover = p->isEnd = 0;}void insert(char *str, Node *root){ Node *p = root; int id; while(*str){ id = *str - '0'; if(p->next[id] == NULL){ p->next[id] = (Node *)malloc(sizeof(Node)); clean(p->next[id]); } p = p->next[id]; if(p->isEnd) ok = 0; ++p->isCover; ++str; } if(p->isCover > 1) ok = 0; p->isEnd = 1;}void DELETE(Node *p){ for(int i = 0; i < 10; ++i) if(p->next[i]) DELETE(p->next[i]); free(p);}int main(){ int t, n; char str[12]; scanf("%d", &t); while(t--){ Node *root = (Node *)malloc(sizeof(Node)); scanf("%d", &n); clean(root); ok = 1; while(n--){ scanf("%s", str); if(ok) insert(str, root); } printf(ok ? "YES\n" : "NO\n"); DELETE(root); } return 0;}