zoukankan      html  css  js  c++  java
  • poj 3630 Phone List

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

     
    字典树,插入的时候记得标记,如果是以某个数字结尾,就标记为2,其他经过的都标记为1,分为两种情况,a是b的前缀,先插入a,那么再插入b的时候发现到了a的结尾时,标记为2,如果先插入b,那么再插入a时,发现到了a的结尾时,标记为1,都是NO的情况。
    代码:
    #include <iostream>
    #include <cstdio>
    #include <sstream>
    #include <cstring>
    using namespace std;
    int n,t,pos;
    int trie[1000001][10];
    int tail[1000001],flag;
    void Insert(string s) {
        int i = 0,c = 0,len = s.size();
        while(i < len) {
            int d = s[i] - '0';
            if(!trie[c][d]) {
                trie[c][d] = ++ pos;
            }
            c = trie[c][d];
            if(tail[c] == 2 || i == len - 1 && tail[c])flag = 0;
            tail[c] = 1;
            i ++;
        }
        tail[c] = 2;
    }
    int main() {
        scanf("%d",&t);
        char s[51];
        for(int i = 1;i <= t;i ++) {
            memset(tail,0,sizeof(tail));
            flag = 1;
            scanf("%d",&n);
            for(int j = 0;j < n;j ++) {
                scanf("%s",s);
                Insert(s);
            }
            puts(flag ? "YES" : "NO");
        }
    }

     每次初始化字典树

    代码:

    #include <iostream>
    #include <cstdio>
    #include <sstream>
    #include <cstring>
    using namespace std;
    int n,t,pos;
    int trie[100010][10];
    int tail[100010],flag;
    void Insert(char *s) {
        int i = 0,c = 0;
        while(s[i]) {
            int d = s[i] - '0';
            if(!trie[c][d]) {
                trie[c][d] = ++ pos;
            }
            c = trie[c][d];
            if(tail[c] == 2 || !s[i + 1] && tail[c]) flag = 0;
            tail[c] = 1;
            i ++;
        }
        tail[c] = 2;
    }
    int main() {
        scanf("%d",&t);
        char s[51];
        for(int i = 1;i <= t;i ++) {
            pos = 0;
            memset(tail,0,sizeof(tail));
            memset(trie,0,sizeof(trie));
            flag = 1;
            scanf("%d",&n);
            for(int j = 0;j < n;j ++) {
                scanf("%s",s);
                if(flag) Insert(s);
            }
            puts(flag ? "YES" : "NO");
        }
    }
  • 相关阅读:
    GitHub之初体验
    梦过去了
    转载:获取ISAPI_Rewrite重写后的URL
    看《分手合约》
    我们的家
    一盏灯的思考
    网站爱好者:开篇文
    致我们终将逝去的青春
    一起看看百度转码有多无耻!
    RPM
  • 原文地址:https://www.cnblogs.com/8023spz/p/9597652.html
Copyright © 2011-2022 走看看