题解UVA11362 Phone List

Posted bj2002

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了题解UVA11362 Phone List相关的知识,希望对你有一定的参考价值。

Tags :

? 排序,字典树

? 从短到长排序,逐个插入字典树。若与已有的重复,返回错误信息。

#include <iostream>
#include <stdio.h>
#include <string>
#include <cstring>
#include <algorithm>
#define re register
#define GC getchar()
#define Clean(X,K) memset(X,K,sizeof(X))
using namespace std ;
const int Maxn = 10005 , MaxD = 10 , Base = 10 ;
int Times , N , T[Maxn * MaxD][Base] , End[Maxn * MaxD] , Tot = 0;
string S[Maxn] ;
bool Add (string &S) {
    int L = S.length() , P = 0 ;
    for (re int i = 0 ; i < L; ++ i) {
        if (!T[P][S[i] - '0']) T[P][S[i] - '0'] = ++ Tot ;
        P = T[P][S[i] - '0'] ;
        if (End[P]) return true ;
    }
    ++ End[P] ;
    return false ;
}
bool C (const string &X , const string &Y) {
    return X.length() < Y.length() ;
}
int main () {
//  freopen ("POJ3630.in" , "r" , stdin) ;
    scanf ("%d" , &Times) ;
    while (Times -- ) {
        scanf ("%d\n" , &N) ;
        Clean (T , 0) , Clean (End , 0);
        Tot = 0 ;
        bool Fl = true ;
        for (re int i = 0 ; i < N; ++ i) getline (cin , S[i]) ;
        sort (S , S + N , C) ;
        for (re int i = 0 ; i < N; ++ i) {
            if (Add (S[i])) {
                printf("NO\n") ;
                Fl = false;
                break ;
            }
        }
        if (Fl ) printf ("YES\n") ;
    }
    fclose (stdin) , fclose (stdout) ;
    return 0 ;
}

以上是关于题解UVA11362 Phone List的主要内容,如果未能解决你的问题,请参考以下文章

UVA Phone List (字典树)(查询是否有前缀或自身是其他的前缀)

poj3630:Phone List——题解

题解Street Numbers [UVA138]

UVa 307 Sticks 题解

题解 UVa11727

题解 UVa13244