数据结构编程题:Phone List

发布时间:2024年01月21日

题目描述

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:

段落大意:给定一组电话号码,判断它们是否一致,即没有一个号码是另一个号码的前缀。假设电话目录列出了以下号码:

Emergency 911

Alice 97 625 999

Bob 91 12 54 26

In the case, it’s not possible to call Bob, because the central would direct your call to the emergency line as soon as you had dialed the first three digits of bob’s phone number. So this list would not be sonsistent.

段落大意:在这种情况下,不可能拨打Bob的电话,因为只要你拨打Bob电话号码的前三位,中心就会立即将你的呼叫转接到紧急线。因此,这个列表就不一致。

输入

The first line of input gives a single integer, 1<=t<=40, the number of test cases. Each test starts with n, the number of phone numbers, on a separate line, 1<=n<=10000. Then follows n linss with one unique phone numbers on each line. A phone number is a sequence of at most ten digits.

段落大意:第一行输入一个整数,1<=t<=40,表示测试用例的数量。每个测试用例以一个单独的行n开始,表示电话号码的数量,1<=n<=10000。然后是n行,每行包含一个唯一的电话号码。电话号码是最多包含十位数字的序列。

输出

For each test case, output “YES” if the list is consistent, or “NO” otherwise.

段落大意:对于每个测试用例,如果列表一致,则输出“YES”,否则输出“NO”

输入样例 1?

2
3
911
97625999
91125426
5
113
12340
123440
12345
98346

输出样例 1

NO
YES

实现

本题采用Trie树思路

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_PHONE_LENGTH 11
typedef struct TrieNode {
    struct TrieNode* children[10];
    int isEndOfWord;
} TrieNode;
TrieNode* createTrieNode() {
    TrieNode* node = (TrieNode*)malloc(sizeof(TrieNode));
    for (int i = 0; i < 10; ++i) {
        node->children[i] = NULL;
    }
    node->isEndOfWord = 0;
    return node;
}
int insertPhoneNumber(TrieNode* root, const char* phoneNumber) {
    TrieNode* current = root;
    for (int i = 0; i < strlen(phoneNumber); ++i) {
        int index = phoneNumber[i] - '0';
        if (current->children[index] == NULL) {
            current->children[index] = createTrieNode();
        }
        current = current->children[index];
        if (current->isEndOfWord) {
            return 0;
        }
    }
    current->isEndOfWord = 1;
    for (int i = 0; i < 10; ++i) {
        if (current->children[i] != NULL) {
            return 0;
        }
    }
    return 1;
}
void freeTrie(TrieNode* root) {
    if (root == NULL) return;
    for (int i = 0; i < 10; ++i) {
        freeTrie(root->children[i]);
    }
    free(root);
}
int main() {
    int t;
    scanf("%d", &t);
    while (t--) {
        int n;
        scanf("%d", &n);
        TrieNode* root = createTrieNode();
        int consistent = 1;
        for (int i = 0; i < n; ++i) {
            char phoneNumber[MAX_PHONE_LENGTH];
            scanf("%s", phoneNumber);
            if (!consistent) {
                continue;
            }
            consistent = insertPhoneNumber(root, phoneNumber);
        }
        printf("%s\n",consistent?"YES":"NO");
        freeTrie(root);
    }
    return 0;
}

文章来源:https://blog.csdn.net/Yerosius1/article/details/135727981
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。