• POJ 3630


    题目链接:http://poj.org/problem?id=3630

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

    题意:

    给你 $n$ 个字符串,让你确定其中是否有存在某个字符串是另一个字符串的前缀。

    AC代码:

    (通过OpenJ_Bailian - 3791OpenJ_Bailian - 4089

    #include<bits/stdc++.h>
    using namespace std;
    const int maxn=1e4+5;
    
    int n;
    string s[maxn];
    
    namespace Trie
    {
        const int SIZE=maxn*10;
        int sz;
        struct TrieNode{
            int ed;
            int nxt[10];
        }trie[SIZE];
        void init()
        {
            sz=1;
            memset(trie,0,sizeof(trie));
        }
        void insert(const string& s)
        {
            int p=1;
            for(int i=0;i<s.size();i++)
            {
                int ch=s[i]-'0';
                if(!trie[p].nxt[ch]) trie[p].nxt[ch]=++sz;
                p=trie[p].nxt[ch];
            }
            trie[p].ed++;
        }
        int prefix(const string& s)
        {
            int res=0,p=1;
            for(int i=0;i<s.size();i++)
            {
                p=trie[p].nxt[s[i]-'0'];
                res+=trie[p].ed;
                if(!p) return res;
            }
            return res;
        }
    };
    
    int main()
    {
        ios::sync_with_stdio(0);
        cin.tie(0); cout.tie(0);
    
        int T;
        cin>>T;
        while(T--)
        {
            cin>>n;
            Trie::init();
            for(int i=1;i<=n;i++) {
                cin>>s[i];
                Trie::insert(s[i]);
            }
    
            bool ok=1;
            for(int i=1;i<=n;i++) {
                if(Trie::prefix(s[i])>1) {
                    ok=0;
                    break;
                }
            }
            if(ok) cout<<"YES
    ";
            else cout<<"NO
    ";
        }
    }
  • 相关阅读:
    supervisor 3.0a81 安装失败
    nginx使用HttpImageFilterModule
    docky模式下背景不透明
    一堆DLL中找一个类
    Python中序列化处理之——marshal和cPickle篇
    Mercurial host fingerprint warning
    Python中时间的处理之——tzinfo篇
    ServiceStack.Redis的问题与修正
    Python中时间的处理之——datetime篇
    Rdesktop 1.6.0 and Windows Server 2008 SP2
  • 原文地址:https://www.cnblogs.com/dilthey/p/9962932.html
Copyright © 2020-2023  润新知