poj 3630 静态字典树入门

来源:互联网 发布:java常见注解 编辑:程序博客网 时间:2024/06/06 20:29

点击打开链接

Phone List
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 31209 Accepted: 9166

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

2391197625999911254265113123401234401234598346

Sample Output

NOYES
题意:输入一系列电话号码数字,若有某个数字是另外一个数字的前缀就输出NO,否则YES。

思路:这个题思路清晰,很容易,但是有个坑就是动态建树会超时,因此这个题要用静态建树,空间换时间。

#include<stdio.h>#include<stdlib.h>#include<string.h>struct node{int cnt;int flag;struct node *next[10];void init(){cnt=flag=0;for(int i=0;i<10;i++)next[i]=NULL;}}a[100000];char s[12];int mark;int k=0;void insert(){int i,t,len=strlen(s);node *p=&a[0];for(i=0;i<len;i++){t=s[i]-'0';if(p->next[t]==NULL)p->next[t]=&a[++k];p=p->next[t];p->cnt++;if(p->flag>0)//如果这个节点是某个数字的尾数,标记 mark=1; }p->flag++;if(p->cnt>1)//如果这个节点之前已经是别的数字的节点,标记 mark=1;}void del(){for(int i=0;i<100000;i++)a[i].init();}int main(){int T;scanf("%d",&T);while(T--){int n,i;scanf("%d",&n);mark=k=0;for(i=0;i<n;i++){scanf("%s",&s);insert();}if(mark==1)printf("NO\n");elseprintf("YES\n");del();}}


阅读全文
0 0