• 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");
        }
    }
  • 相关阅读:
    新增更改app.Config的值
    repeater DropDownList 事件
    ASP.NET 状态服务 及 session丢失问题解决方案总结
    js动态添加table的行
    各大社交网络首页黄金区输入框提示(facebook,人人网,开心网)
    Color theme installation for Emacs in Windows 7
    乐观锁和悲观锁
    google的落寞
    印象深刻的网络实验课
    未知和恐惧
  • 原文地址:https://www.cnblogs.com/8023spz/p/9597652.html
Copyright © 2020-2023  润新知