天天看點

[ACM] hdu 1671 Phone List (字典樹)

Phone List

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.

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

2008

“Insigma International Cup” Zhejiang Collegiate Programming Contest - Warm Up(3)

解題思路:

推斷輸入的串中是否存在某個串是另外串的字首。

建立字典樹,關鍵是設立某個串結尾的标志,即在哪個字母結尾。

要推斷是否存在字首要考慮兩種情況。一是目前輸入的串是否是曾經輸入的串的字首,而是曾經輸入的串是否是目前輸入的串的字首。前一種情況在查找該串時,會從第一個字母查找到最後一個字母,中間不傳回不論什麼值,說明目前的串是曾經的字首,後一種情況。當在查找目前串的時候遇到曾經串結束的标志。則說明曾經串是目前串的字首。

代碼中結束的标志為儲存串中最後一個字母的那個節點的cnt值為-1.

如圖:

[ACM] hdu 1671 Phone List (字典樹)
#include <iostream>
#include <malloc.h>
#include <algorithm>
#include <string.h>
#include <stdio.h>
using namespace std;
const int maxn=10;
bool ok;
char str[12];
int t,n;

struct Trie
{
    int cnt;
    Trie *next[maxn];
};

Trie *root;

void CreateTrie(char *str)
{
    int len=strlen(str);
    Trie*p=root,*q;
    for(int i=0;i<len;i++)
    {
        int id = str[i]-'0';
        if(p->next[id] == NULL)
        {
            q = (Trie *)malloc(sizeof(Trie));
            q->cnt = 1;
            for(int j=0; j<maxn; ++j)
                q->next[j] = NULL;
            p->next[id] = q;
            p = p->next[id];
        }
        else
        {
            p = p->next[id];
        }
    }
    p->cnt=-1;//串末尾的标志
}

int findTrie(char *str)
{
    int len=strlen(str);
    Trie *p=root;
    for(int i=0;i<len;i++)
    {
        int id=str[i]-'0';
        if(p->next[id]==NULL)
            return 0;//沒有建過樹
        if(p->next[id]->cnt==-1)
            return -1;//曾經串是目前串的字首
        p=p->next[id];
    }
    return -1;//目前串是曾經串的字首
}

void release(Trie *root)//釋放空間
{
    for(int i=0;i<maxn;i++)
    {
        if(root->next[i])
            release(root->next[i]);
    }
    free(root);
}
int main()
{
    scanf("%d",&t);
    while(t--)
    {
        ok=1;
        root=(Trie*)malloc(sizeof(Trie));//root為指針類型,須要申請空間
        for(int i=0; i<10; ++i)
            root->next[i] = NULL;
        scanf("%d",&n);
        while(n--)
        {
            scanf("%s",str);
            if(findTrie(str)==-1)
                ok=0;//有字首
            if(!ok)
                continue;//有字首,後面的就不用建樹了
            CreateTrie(str);
        }
        if(ok)
            printf("YES\n");
        else
            printf("NO\n");
        release(root);
    }
    return 0;
}