hdu 1671 Phone List 字典树

来源:互联网 发布:数据库中序列自增 编辑:程序博客网 时间:2024/05/17 09:16

Phone List

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


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
2391197625999911254265113123401234401234598346
 

Sample Output
NOYES
 

Source
2008 “Insigma International Cup” Zhejiang Collegiate Programming Contest - Warm Up(3)
 

Recommend
lcy   |   We have carefully selected several similar problems for you:  1075 1247 1800 1677 1298 


题目要输入一连串电话号码,要你判断时候存在一个电话号码是其它电话号码的前缀。
如果有输出NO,否则YES。
插入的时候进行判断即可。
在每个结点设置一个bool型,来表示他是否是一个电话号码的结尾。


并不能单纯的通过:在插入一个字符串时,是否存在是是它前缀的字符串或者 把它作为前缀的字符串来判断。
因为加入有a,ab,(注意顺序)那么第二种方法失效;假如有ab,a,那么第一种方法失效。
所以在插入一个字符串时 ,两者都要判断。
#include<cstdio>#include<string>#include<cstring>#include<iostream>#include<cmath>#include<algorithm>#include<climits>#include<queue>#include<vector>#include<map>#include<sstream>#include<set>#include<stack>#include<cctype>#include<utility>#pragma comment(linker, "/STACK:102400000,102400000")#define PI (4.0*atan(1.0))#define eps 1e-10#define sqr(x) ((x)*(x))#define FOR0(i,n)  for(int i=0 ;i<(n) ;i++)#define FOR1(i,n)  for(int i=1 ;i<=(n) ;i++)#define FORD(i,n)  for(int i=(n) ;i>=0 ;i--)#define  lson   ind<<1,le,mid#define rson    ind<<1|1,mid+1,ri#define MID   int mid=(le+ri)>>1#define zero(x)((x>0? x:-x)<1e-15)#define mk    make_pair#define _f     first#define _s     secondusing namespace std;//const int INF=    ;typedef long long ll;//const ll inf =1000000000000000;//1e15;//ifstream fin("input.txt");//ofstream fout("output.txt");//fin.close();//fout.close();//freopen("a.in","r",stdin);//freopen("a.out","w",stdout);const int INF =0x3f3f3f3f;const int maxn=  10000+20   ;const int N=10;//const int maxm=    ;int n;bool ok;struct Node{    int nex[N];    bool has;    void init()    {        for(int i=0;i<N;i++)  nex[i]=0;        has=0;    }}node[ maxn*12];int root,cnt;char tmp[15];int newnode(){    node[++cnt].init();    return cnt;}void insert(){    int s=root;    for(int i=0;tmp[i];i++)    {        int x=tmp[i]-'0';        if(node[s].has)  ok=0;        if(!node[s].nex[x])  node[s].nex[x]=newnode();        s=node[s].nex[x];    }    if(node[s].has)  ok=0;    node[s].has=1;    for(int i=0;ok&&i<N;i++)    {        if(node[s].nex[i])   {ok=0;break;}    }}int main(){    int T;scanf("%d",&T);    while(T--)    {        root=1,cnt=1;        node[root].init();        scanf("%d",&n);         ok=1;        for(int i=1;i<=n;i++)        {            scanf("%s",tmp);            if(ok) insert();        }        puts(ok?"YES":"NO");    }    return 0;}



 
0 0