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");
        }
    }
  • 相关阅读:
    牛客小白月赛12 D 月月给华华出题 (欧拉函数,数论,线筛)
    牛客小白月赛12 F 华华开始学信息学 (分块+树状数组)
    牛客小白月赛12 C 华华给月月出题 (积性函数,线性筛)
    牛客小白月赛12 I 华华和月月逛公园 (tarjian 求桥)
    Tourist's Notes CodeForces
    Educational Codeforces Round 71 (Rated for Div. 2) E XOR Guessing (二进制分组,交互)
    Tunnel Warfare HDU
    蓝桥杯第三届总决赛
    HDU 1695(数论,筛选+素因子分解+容斥)
    lightoj 1248 Dice (III)(几何分布+期望)
  • 原文地址:https://www.cnblogs.com/8023spz/p/9597652.html
Copyright © 2011-2022 走看看