Problem 48: Phone List
Time Limit:1 Ms| Memory Limit:128 MB
Difficulty:2
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
3
911
97625999
91125426
5
113
12340
123440
12345
98346
Sample Output
NO
YES
YES
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#define MAX 10
typedef struct Node
{
char ph[3];
struct Node *next[MAX];
}Node;
typedef struct tree
{
Node *head;
}Tr;
int flag;
int len;
char na[MAX];
void insert_Node(Tr *T);
void init_tree(Tr *T);
Node * create_Node(char i, int k);
void destroy(Node *T);
int main()
{
int t, n;
Tr T,i;
scanf("%d", &t);
while(t--)
{
flag = 0;
init_tree(&T);
scanf("%d", &n);
getchar();
while(n--)
{
len = 0;
memset(na, 0, sizeof(na));
scanf("%s",na);
len = strlen(na);
insert_Node(&T);
}
if(flag == 1)
{
printf("NO\n");
}
else
printf("YES\n");
destroy(T.head);
}
return 0;
}
void init_tree(Tr *T)
{
int i;
(T)->head = (Node *)malloc(sizeof(Node));
for(i = 0; i < MAX; i++)
(T)->head->next[i] = NULL;
}
void destroy(Node *T)
{
int i;
if(T == NULL)
return ;
for(i = 0; i < MAX; i++)
{
destroy(T->next[i]);
}
//free(T->ph);
free(T);
}
Node * create_Node(char i, int k)
{
Node *p;
int j;
p = (Node *)malloc(sizeof(Node));
p->ph[1] = i;
p->ph[2] = '\0';
if(k == len - 1)
{
p->ph[0] = '1';
}
else
{
p->ph[0] = '0';
}
for(j = 0; j < MAX; j++)
{
p->next[j] = NULL;
}
return p;
}
void insert_Node(Tr *T)
{
Node *temp = (T)->head;
int i;
for(i = 0; i < len; i++)
{
if(temp->next[na[i] - 48] == NULL)//当头为空时;
{
temp->next[na[i] - 48] = create_Node(na[i],i);
temp = temp->next[na[i] - 48];
}
else
{
temp = temp->next[na[i] - 48];//temp指向该号码第一个数字
if(temp->ph[0] == '1')//如果当前结点已标记为号码
{
flag = 1;
return ;
}
if(i == len -1 && temp->ph[0] != '1')//如果已经是存入的完整的号码;
//如果p->ph中存的号码和输入得相同且在树中找到了存放相同号码的结点;
{
flag = 1;
return;
}
}
}
}
注意事项:
#include<string.h>
#include<stdlib.h>
#define MAX 10
typedef struct Node
{
char ph[3];
struct Node *next[MAX];
}Node;
typedef struct tree
{
Node *head;
}Tr;
int flag;
int len;
char na[MAX];
void insert_Node(Tr *T);
void init_tree(Tr *T);
Node * create_Node(char i, int k);
void destroy(Node *T);
int main()
{
int t, n;
Tr T,i;
scanf("%d", &t);
while(t--)
{
flag = 0;
init_tree(&T);
scanf("%d", &n);
getchar();
while(n--)
{
len = 0;
memset(na, 0, sizeof(na));
scanf("%s",na);
len = strlen(na);
insert_Node(&T);
}
if(flag == 1)
{
printf("NO\n");
}
else
printf("YES\n");
destroy(T.head);
}
return 0;
}
void init_tree(Tr *T)
{
int i;
(T)->head = (Node *)malloc(sizeof(Node));
for(i = 0; i < MAX; i++)
(T)->head->next[i] = NULL;
}
void destroy(Node *T)
{
int i;
if(T == NULL)
return ;
for(i = 0; i < MAX; i++)
{
destroy(T->next[i]);
}
//free(T->ph);
free(T);
}
Node * create_Node(char i, int k)
{
Node *p;
int j;
p = (Node *)malloc(sizeof(Node));
p->ph[1] = i;
p->ph[2] = '\0';
if(k == len - 1)
{
p->ph[0] = '1';
}
else
{
p->ph[0] = '0';
}
for(j = 0; j < MAX; j++)
{
p->next[j] = NULL;
}
return p;
}
void insert_Node(Tr *T)
{
Node *temp = (T)->head;
int i;
for(i = 0; i < len; i++)
{
if(temp->next[na[i] - 48] == NULL)//当头为空时;
{
temp->next[na[i] - 48] = create_Node(na[i],i);
temp = temp->next[na[i] - 48];
}
else
{
temp = temp->next[na[i] - 48];//temp指向该号码第一个数字
if(temp->ph[0] == '1')//如果当前结点已标记为号码
{
flag = 1;
return ;
}
if(i == len -1 && temp->ph[0] != '1')//如果已经是存入的完整的号码;
//如果p->ph中存的号码和输入得相同且在树中找到了存放相同号码的结点;
{
flag = 1;
return;
}
}
}
}
注意事项:
一定要在每组数据测完后把树的空间释放点否则会造成栈溢出;
下面是大神写的代码!:简洁啊!
下面是大神写的代码!:简洁啊!
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct Node{
int bz;
struct Node *next[10];
Node(){
bz = 0;
for(int i = 0; i < 10; i ++){
next[i] = NULL;
}
}
};
int buildtree(Node &head, char a[]);
int main()
{
int i, k, n, ans;
scanf("%d", &k);
while(k --){
Node head;
char a[20];
scanf("%d", &n);
ans = 1;
for(i = 0; i < n; i ++){
scanf("%s", a);
if(ans) ans = buildtree(head, a);
}
if(ans) printf("YES\n");
else printf("NO\n");
}
return 0;
}
int buildtree(Node &head, char a[])
{
int ok = 0;
int len = strlen(a);
Node *cur = &head;
for(int i = 0; i < len; i ++){
cur ->next[a[i] - '0'];
if(cur ->next[a[i] - '0'] == NULL){
cur ->next[a[i] - '0'] = (Node *) calloc (1, sizeof(Node));
ok = 1;
}
else
if(cur ->next[a[i] - '0'] ->bz == 1) break;
cur = cur ->next[a[i] - '0'];
if(i == len - 1) cur ->bz = 1;
}
return ok;
}
#include <stdlib.h>
#include <string.h>
struct Node{
int bz;
struct Node *next[10];
Node(){
bz = 0;
for(int i = 0; i < 10; i ++){
next[i] = NULL;
}
}
};
int buildtree(Node &head, char a[]);
int main()
{
int i, k, n, ans;
scanf("%d", &k);
while(k --){
Node head;
char a[20];
scanf("%d", &n);
ans = 1;
for(i = 0; i < n; i ++){
scanf("%s", a);
if(ans) ans = buildtree(head, a);
}
if(ans) printf("YES\n");
else printf("NO\n");
}
return 0;
}
int buildtree(Node &head, char a[])
{
int ok = 0;
int len = strlen(a);
Node *cur = &head;
for(int i = 0; i < len; i ++){
cur ->next[a[i] - '0'];
if(cur ->next[a[i] - '0'] == NULL){
cur ->next[a[i] - '0'] = (Node *) calloc (1, sizeof(Node));
ok = 1;
}
else
if(cur ->next[a[i] - '0'] ->bz == 1) break;
cur = cur ->next[a[i] - '0'];
if(i == len - 1) cur ->bz = 1;
}
return ok;
}