zoukankan      html  css  js  c++  java
  • Phone List(字典树)

    Phone List
    Time Limit: 1000MS   Memory Limit: 65536K
    Total Submissions: 25709   Accepted: 7785

    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
    题解:只要找到一个word【k】==1,证明这个单词是新的;
    就结束,没找到,证明这个单词是其他单词的前缀;
    代码:
     1 #include<cstdio>
     2 #include<iostream>
     3 #include<algorithm>
     4 #include<cstring>
     5 #include<cmath>
     6 using namespace std;
     7 #define mem(x,y) memset(x,y,sizeof(x))
     8 const int INF=0x3f3f3f3f;
     9 const double PI=acos(-1.0);
    10 const int MAXN=1000010;
    11 int ch[MAXN][10],word[MAXN];
    12 char dt[MAXN][12];
    13 int sz;
    14 void initial(){
    15     sz=1;
    16     mem(word,0);
    17     mem(ch[0],0);
    18 }
    19 void join(char *s){
    20     int len=strlen(s),k=0,j;
    21     for(int i=0;i<len;i++){
    22         j=s[i]-'0';
    23         if(!ch[k][j]){
    24             mem(ch[sz],0);
    25             ch[k][j]=sz++;
    26         }
    27         k=ch[k][j];
    28         word[k]++;
    29     }
    30 }
    31 bool find(char *s){
    32     int len=strlen(s),k=0,j;
    33     for(int i=0;i<len;i++){
    34         j=s[i]-'0';
    35         k=ch[k][j];
    36         if(word[k]==1)return true;
    37     }
    38     return false;
    39 }
    40 void display(int n){
    41     for(int i=0;i<n;i++){
    42         if(!find(dt[i])){
    43         //    puts(dt[i]);
    44             puts("NO");
    45             return;
    46         }
    47     }
    48     puts("YES");
    49 }
    50 int main(){
    51     int T,N;
    52     scanf("%d",&T);
    53     while(T--){
    54         initial();
    55         scanf("%d",&N);
    56         for(int i=0;i<N;i++){
    57             scanf("%s",dt[i]);
    58             join(dt[i]);
    59         }
    60         display(N);
    61     }
    62     return 0;
    63 }
    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<cmath>
    #include<algorithm>
    using namespace  std;
    typedef long long LL;
    #define mem(x,y) memset(x,y,sizeof(x))
    #define SI(x) scanf("%d",&x)
    #define PI(x) printf("%d",x)
    #define P_ printf(" ")
    const int INF=0x3f3f3f3f;
    const int MAXN=1000010;//要开的足够大 
    int ch[MAXN][10];
    int word[MAXN];
    char str[MAXN][15];
    int sz;
    int N;
    void insert(char *s){
    	int k=0,j;
    	for(int i=0;s[i];i++){
    		j=s[i]-'0';
    		if(!ch[k][j]){
    			mem(ch[sz],0);
    			ch[k][j]=sz++;
    		}
    		k=ch[k][j];
    		word[k]++;
    	}
    }
    bool find(char *s){
    	int k=0,j;
    	for(int i=0;s[i];i++){
    		j=s[i]-'0';
    		k=ch[k][j];
    		if(word[k]==1)return true;
    	}
    	return false;
    }
    bool work(){
    	for(int i=0;i<N;i++){
    	//	puts("**");
    		if(!find(str[i]))return false;
    	}
    	return true;
    }
    int main(){
    	int T;
    	SI(T);
    	while(T--){
    		SI(N);
    		sz=1;//
    		mem(ch[0],0);mem(word,0);//
    		for(int i=0;i<N;i++)scanf("%s",str[i]),insert(str[i]);
    		if(work())puts("YES");
    		else puts("NO");
    	}
    	return 0;
    }
    

      

  • 相关阅读:
    read-uncommited 下脏读的实现
    MySQL 加锁处理分析【重点】
    Next-key locking是如何解决幻读(当前读)问题的
    spring ioc
    讨论 update A set number=number+ ? where id=?的原子性 (含数据库原理)
    PESSIMISTIC_READ & PESSIMISTIC_WRITE 与 共享锁 & 排它锁
    innodb当前读 与 快照读 and rr级别是否真正避免了幻读
    java finalize及实践
    uva 539 The Settlers of Catan
    zoj 1016 Parencodings
  • 原文地址:https://www.cnblogs.com/handsomecui/p/4918326.html
Copyright © 2011-2022 走看看