码迷,mamicode.com
首页 > 其他好文 > 详细

Phone List(简单的字典树插入操作)

时间:2015-02-15 21:55:49      阅读:157      评论:0      收藏:0      [点我收藏+]

标签:字典树

Phone List

Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 11655    Accepted Submission(s): 3970



Problem 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:
1. Emergency 911
2. Alice 97 625 999
3. 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
2008 “Insigma International Cup” Zhejiang Collegiate Programming Contest - Warm Up(3)

就是简单的字典树插入操作,在插入操作是判断下会不会出现前缀现象即可.需要销毁内存,不然会MLE. 我的代码C++超时了,G++234MS,心塞.
..............................各位巨巨要是知道超时原因,不妨教教弱弱...........................

AC代码:
#include <iostream>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <set>
#include <map>
#include <queue>
#include <vector>
#include <cstdlib>
#include <algorithm>

#define ls u << 1
#define rs u << 1 | 1
#define lson l, mid, u << 1
#define rson mid + 1, r, u << 1 | 1
#define INF 0x3f3f3f3f
#define MAX 10

using namespace std;
typedef long long ll;
const int M = 1e4 + 100;
const int mod = 2147483647;

struct Trie{
    Trie *next[MAX];
    int index;
    Trie(){
        index = 1;
        memset(next,0,sizeof(next));
    }
};
char s[20];
bool flag;

void Trie_insert(Trie *tr,int len){
    if(!flag) return;
    if(s[len]){
        int u = s[len] - '0';
        if(tr->next[u] == 0){
            tr->next[u] = new Trie;
        }
        else {
            if(tr->next[u]->index == -1 || s[len + 1] == '\0'){
                flag = false;
                return ;
            }
        }
        Trie_insert(tr->next[u],len + 1);
    }
    else tr->index = -1;
}

void deal_Trie(Trie *tr){
    if(tr == NULL) return ;
    for(int i = 0; i < MAX; i++){
        if(tr->next[i]) deal_Trie(tr->next[i]);
    }
    free(tr);
}

int main(){
    int T,n;
    cin>>T;
    while(T--){
        scanf("%d",&n);
        flag = true;
        Trie *root = new Trie;
        while(n--){
            scanf("%s",s);
            if(flag) Trie_insert(root,0);
        }
        if(flag) puts("YES");
        else puts("NO");
        deal_Trie(root);
    }
    return 0;
}


hash擦边过的,需要小小的剪枝.考虑到会有前导0,所以在每一个号码前加1,不影响结果.

#include <iostream>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <set>
#include <map>
#include <queue>
#include <vector>
#include <cstdlib>
#include <algorithm>

#define ls u << 1
#define rs u << 1 | 1
#define lson l, mid, u << 1
#define rson mid + 1, r, u << 1 | 1
#define INF 0x3f3f3f3f
#define MAX 10

using namespace std;
typedef long long ll;
const int M = 1e4 + 100;
const int mod = 2147483647;
map<ll,int>mp;
ll d[M];

bool solve(int n){
    mp[d[0]] = 1;
    for(int i = 1; i < n; i++){
        ll res = d[i];
        while(res >= 100){ //剪枝,不加会超时.
            res /= 10;
            if(mp[res]) return 1;
        }
        mp[d[i]] = 1;
    }
    return 0;
}

int main(){
    int T,n;
    char s[20];
    scanf("%d",&T);
    while(T--){
        mp.clear();
        scanf("%d",&n);
        for(int i = 0; i < n; i++){
            scanf("%s",s);
            ll res = 1;
            for(int j = 0; s[j]; j++) res = res * 10 + s[j] - '0';
            d[i] = res;
        }
        sort(d,d + n);
        if(solve(n)) puts("NO");
        else puts("YES");
    }
    return 0;
}



Phone List(简单的字典树插入操作)

标签:字典树

原文地址:http://blog.csdn.net/zsgg_acm/article/details/43837083

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!