POJ 3630 Phone List 【Trie树入门】

76 篇文章 0 订阅
16 篇文章 1 订阅

Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 18122 Accepted: 5769

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:

  • Emergency 911
  • Alice 97 625 999
  • 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 nlines 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


算法:Trie 前缀树入门

思路1.Trie树版本【lrj《算法竞赛入门经典 训练指南 P208 - 209》】

        每次输入一个字符串,则插入 Trie 树中。

    边插入边判断 ,那么会出现下面的三种情况:

    第一、当前插入的字符串从来没有被插入过,返回 false表示插入成功,继续插入下一条字符串

    第二、当前插入的字符串是已经插入过的字符串的前缀,停止插入,返回 true 输出 NO。

    第三、当前插入的字符的前缀已经作为单独的字符串插入过,停止插入,返回 true 输出 NO。

   2.ORC的 C++ STL版本,思路简单,代码清晰。

    先用 vector<string> 存储输入的每条字符串

    再用 sort 从大到小排序

    最后依次遍历看后面的字符串中是否能 find() 前一个

【Trie树版本】

3630Accepted2576K125MSC++1421B2013-04-18 19:21:01
AAccepted2576 KB110 msC++1282 B2013-04-18 18:54:47

#include<stdio.h>
#include<string.h>

const int maxnode = 10000*10+10;

struct Trie{
    int ch[maxnode][10];
    int val[maxnode];
    int sz;

    void init() { sz = 1; memset(ch[0], 0, sizeof(ch[0])); }

    bool insert(char *s, int v){
        int len = strlen(s);
        int u = 0;
        int id, i;
        for(i = 0; i < len; i++){
            id = s[i]-'0';

            if(!ch[u][id]){
                val[sz] = 0;
                memset(ch[sz], 0, sizeof(ch[sz]));
                ch[u][id] = sz++;
            }
            u = ch[u][id];
            if(val[u]) return true; //当前插入的 s的前缀已经单独插入过
            else if(i == (len-1)){ //如果当前插入的 s 是已经插入过的字符串的前缀
                for(int j = 0; j <= 9; j++)
                if(ch[u][j])
                return true;
            }//else if(i == (len-1) && !val[u]) return true;

        }
        val[u] = v;
        return false; //成功插入
    }
}trie;

int main()
{
    int T;
    scanf("%d", &T);
    while(T--)
    {
        int n;
        char str[11];
        bool flag = true;
        scanf("%d", &n);
        trie.init();
        while(n--)
        {
            scanf("%s", str); //插入时判断,一旦冲突则退出
            if(trie.insert(str, 1)) { flag = false; break; }
        }
        if(n > 1) while(n--) scanf("%s", str);

        if(flag) printf("YES\n");
        else printf("NO\n");
    }
    return 0;
}

【ORC的 C++ STL版本】

Accepted848K438MSC++714B2013-04-18 00:56:21

#include<string>
#include<vector>
#include<algorithm>
#include<iostream>
using namespace std;

int main()
{
    int T;
    cin>>T;
    while(T--)
    {
        int n;
        vector<string> v;
        string s;

        cin>>n;
        v.clear();
        for(int i = 0; i < n; i++)
        {
            cin>>s;
            v.push_back(s);
        }
        sort(v.begin(), v.end());

        bool flag = true;
        for(int i = 0; i < n-1; i++)
        {
            if(v[i+1].find(v[i]) == 0)
            {
                flag = false;
                break;
            }
        }

        if(flag) cout<<"YES\n";
        else cout<<"NO"<<endl;
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值