zoukankan      html  css  js  c++  java
  • 【Trie】Phone List

    【题目链接】:

    https://loj.ac/problem/10049

    【题意】

    问是否存在一组公共前缀。如果存在输出“NO”,否则输出“YES”

    【题解】

    首先建出Trie树来,然后开始记录所有的字符串,然后进行再跑一遍。看看是否在跑的过程中遇到某个位置上标记。

    裸的模板题。

    【代码】

     1 #include<bits/stdc++.h>
     2 using namespace std;
     3 typedef long long ll;
     4 const int N = 2e4 +10;
     5 int son[N*11][10];
     6 bool cnt[N*200],f;
     7 int T,n,idx;
     8 char str[N][12];
     9 void Insert(char s[]){
    10     //printf("###%s
    ",s);
    11     int p = 0 ;
    12     for(int i=0 ; s[i] ; i++ ){
    13         int t = s[i] - '0';
    14         if( !son[p][t] ) son[p][t] = ++idx ;
    15         p = son[p][t];
    16     }
    17     cnt[p] = true;
    18 }
    19 void Query(char s[]){
    20     //printf("$$$%s
    ",s);
    21     int p = 0 ;
    22     for(int i=0 ; s[i+1] ; i++ ){
    23         int t = s[i] - '0';
    24         if( !son[p][t] ) break;
    25         p = son[p][t];
    26         if( cnt[p] ) f = true;
    27     }
    28 }
    29 void Init(){
    30     f = false ;
    31     idx = 0 ;
    32     memset(cnt,false,sizeof cnt );
    33     memset(son,0,sizeof son );
    34 }
    35 int main()
    36 {
    37     scanf("%d",&T);
    38     while(T--){
    39         Init();
    40         scanf("%d",&n);
    41         for(int i=1;i<=n;i++){
    42             scanf("%s",str[i]);
    43             Insert(str[i]);
    44         }
    45         for(int i=1;i<=n;i++){
    46             Query(str[i]);
    47         }
    48         puts(f?"NO":"YES");
    49     }
    50     return 0;
    51 }
    52 /*
    53 40
    54 2
    55 9999999999
    56 999999999
    57 
    58 2
    59 9999999999
    60 999999998
    61 
    62 2
    63 012
    64 12
    65 
    66 2
    67 012
    68 1
    69 
    70 2
    71 0
    72 01
    73 
    74 2
    75 012
    76 1
    77 
    78 */
    View Code
  • 相关阅读:
    bzoj 3035 二分答案+二分图最大匹配
    bzoj 1058 bst
    bzoj 1093 缩点+DP
    bzoj 1452 二维树状数组
    bzoj 1968 数学
    bzoj 1034 贪心
    牛客小白月赛12 I (tarjan求割边)
    Loj 103、10043 (KMP统计子串个数)
    poj 2356 (抽屉原理)
    hdu 1907 (尼姆博弈)
  • 原文地址:https://www.cnblogs.com/Osea/p/11361483.html
Copyright © 2011-2022 走看看