HDU-1671-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


思路:首先将输入的字符串存放到一个string数组里面,然后对这个数组排序。
从第一个开始遍历,判断前一个字符串是不是下一个字符串的前缀。如果第一个字符相同,而且前一个字符串的长度小于下一个字符串的长度的,就开始对比下面的字符是不是全都相同。如果找到一个字符串是另一个字符串的前缀,则不满足题意,跳出循环,之间输出NO。

/*
    Name: hdu-1671-Phone List
    Author: long_long_ago
    Date: 18/12/15 09:42
    Description: http://acm.hdu.edu.cn/showproblem.php?pid=1671

*/

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#define N 10
using namespace std;
string str[10009];
int main()
{
#ifndef ONLINE_JUDGE
    freopen("1.txt", "r", stdin);
#endif
    int i, j, T, n;
    bool flag;
    cin >> T;
    while(T--)
    {
        flag = true;
        cin >> n;
        for (i = 0; i < n; i++)
        {
            cin >> str[i];
        }
        sort(str, str+n);
        for (i = 0; i < n-1; i++)
        {
            if(str[i][0] == str[i+1][0])
            {
                int l = str[i].length();
                if (l > str[i+1].length())
                {
                    continue;
                }
                for (j = 0; j < l; j++)
                {
                    if (str[i][j] != str[i+1][j])
                    {
                        break;
                    }
                }
                if (j == l)
                {
                    flag = false;
                    break;
                }
            }
        }
        if (flag)
            cout << "YES" << endl;
        else
            cout << "NO" << endl;
    }
    return 0;
}

这里写图片描述


字典树写法

/*
    Name: hdu-1671-Phone List
    Author: long_long_ago
    Date: 18/12/15 10:01
    Description: http://acm.hdu.edu.cn/showproblem.php?pid=1671

*/

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#define N 10
using namespace std;
struct Trie
{
    Trie * next[N];
    int cnt;
};
Trie *root;
void CreateTrie(string str)
{
    int i, j, len = str.length(), t;
    Trie *p = root, *tmp;
    for (i = 0; i < len; i++)
    {
        t = str[i] - '0';
        if (p->next[t])
        {
            p = p->next[t];
            p->cnt++;
        }
        else
        {
            tmp = (Trie*)malloc(sizeof(Trie));
            for (j = 0; j < N; j++)
            {
                tmp->next[j] = NULL;
            }
            p->next[t] = tmp;
            p = p->next[t];
            tmp->cnt = 1;
        }
    }
    p->cnt = -1;
}
int Find(string str)
{
    int i, len = str.length(), t;
    Trie * tmp = root;
    for(i = 0; i < len; i++)
    {
        t = str[i] - '0';
        tmp = tmp->next[t];
        if (!tmp)   return 0;
        if (tmp->cnt == -1)     return -1;
    }
    return -1;
}
void Del(Trie *T)
{
    if (!T) return ;
    for (int i = 0; i < N; i++)
    {
        if (T->next[i])
            Del(T->next[i]);
    }
    free(T);
}
int main()
{
#ifndef ONLINE_JUDGE
    freopen("1.txt", "r", stdin);
#endif
    int i, j, T, n;
    string str;
    int flag;
    cin >> T;
    while(T--)
    {
        flag = 0;
        root = (Trie*)malloc(sizeof(Trie));
        for (i = 0; i < N; i++)
        {
            root->next[i] = NULL;
        }
        root->cnt = true;
        cin >> n;
        for (i = 0; i < n; i++)
        {
            cin >> str;
            if (flag)   continue;
            if (Find(str) == -1)    flag = 1;
            CreateTrie(str);
        }

        if (flag)
            cout << "NO" << endl;
        else
            cout << "YES" << endl;
        Del(root);
    }
    return 0;
}

这里写图片描述

你可能感兴趣的:(杭电)