POJ 3630 Phone List 静态字典树

Phone List
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 25670   Accepted: 7772

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

Nordic 2007

[Submit]   [Go Back]   [Status]   [Discuss]

Home Page   Go Back  To top

要静态树动态树会超时建树前要sort不然这组数据会错

2
2
1
12
2
12
1
NO
NO

ACcode:

#pragma warning(disable:4786)//使命名长度不受限制
#pragma comment(linker, "/STACK:102400000,102400000")//手工开栈
#include <map>
#include <set>
#include <queue>
#include <cmath>
#include <stack>
#include <cctype>
#include <cstdio>
#include <cstring>
#include <stdlib.h>
#include <iostream>
#include <algorithm>
#define rd(x) scanf("%d",&x)
#define rd2(x,y) scanf("%d%d",&x,&y)
#define rds(x) scanf("%s",x)
#define rdc(x) scanf("%c",&x)
#define ll long long int
#define maxn  100005
#define mod 1000000007
#define INF 0x3f3f3f3f //int 最大值
#define FOR(i,f_start,f_end) for(int i=f_start;i<=f_end;++i)
#define MT(x,i) memset(x,i,sizeof(x))
#define PI  acos(-1.0)
#define E  exp(1)
#define MAX 10
using namespace std;
struct Trie{
    Trie *next[MAX];
    int v;
    Trie(){
        FOR(i,0,MAX-1)
            next[i]=NULL;
            v=1;
    }
}node[maxn];
struct N{
    char str[50];
    int len;
    friend bool operator<(N a,N b){
        return a.len<b.len;
    }
}my[maxn];
Trie *root;
int cnt;
void createTrie(char *str){
    int len=strlen(str);
    Trie *p=root,*q;
    FOR(i,0,len-1){
        int id=str[i]-'0';
        if(p->next[id]==NULL){
            q=&node[cnt++];
            p->next[id]=q;
            p=p->next[id];
        }
        else{
            p->next[id]->v++;
            p=p->next[id];
        }
    }
    p->v=111;
}
int find_Trie(char *str){
    int len=strlen(str);
    Trie *p=root;
    FOR(i,0,len-1){
        int id=str[i]-'0';
        if(p->next[id]==NULL)
            break;
        p=p->next[id];
    }
    return (p->v==111?1:0);
}
int n;
int main(){
    int loop;
    rd(loop);
    while(loop--){
        rd(n);
        cnt=0;
        MT(node,0);
        root=&node[cnt++];
        FOR(i,0,n-1){
            rds(my[i].str);
            my[i].len=strlen(my[i].str);
        }
        bool flag=true;
        sort(my,my+n);
        FOR(i,0,n-1)
            createTrie(my[i].str);
        FOR(i,0,n-1){
            flag=find_Trie(my[i].str);
            if(!flag)break;
        }
        printf(flag?"YES\n":"NO\n");
       //deal(root);
    }
    return 0;
}
/*
2
3
911
976259993
9112546
5
113
12340
123440
12345
98346
*/


你可能感兴趣的:(C++,poj,字典树)