• Phone List HDU


    题意:给出一堆一组一组的数字  判断有没有哪一个是另外一个的前缀

    思路:字典树 插入的同时进行判断  不过 当处理一组数字的时候 需要考虑的有两点1.是否包含了其他的序列2.是否被其他序列包含

      刚开始开的1e4死活不过  1e5直接过了。。

     1 #include<bits/stdc++.h>
     2 #include<string>
     3 using namespace std;
     4 const int maxn=1e5+5;
     5 struct Trie{
     6     int size;
     7     int ch[maxn][10];
     8     int isEnd[maxn];
     9     void init(){
    10         memset(ch,0,sizeof(ch));
    11         memset(isEnd,0,sizeof(isEnd));    
    12         size=1;
    13     }
    14     bool insert(char*s){
    15         int rc=0,i=0;
    16         int flag=1;
    17         for(;s[i]!='';i++){
    18             int id=s[i]-'0';
    19             if(ch[rc][id]==0){
    20                 ch[rc][id]=size++;
    21                 flag=0;
    22             }    
    23                 if(isEnd[rc]==1)return 1;
    24             rc=ch[rc][id];
    25             //if(isEnd[rc]==1)return 1;
    26             
    27         }
    28         isEnd[rc]=1;
    29         return flag;
    30     }
    31 }trie;
    32 char s[10000];
    33 int main(){
    34         int t;
    35         scanf("%d",&t);
    36         while(t--){
    37             int ok=0;
    38             int n;
    39             scanf("%d",&n);
    40             trie.init();
    41             for(int i=0;i<n;i++){
    42             scanf("%s",s);
    43             if(ok)continue;
    44             if(trie.insert(s))ok=1;
    45             }
    46             if(ok)printf("NO
    ");
    47             else printf("YES
    ");
    48         }
    49     return 0;
    50 }
  • 相关阅读:
    倒水问题(BFS)
    小程序整理
    微信小程序--录音
    mpvue
    hbuilder 打包 vueAPP
    react rem
    react 关闭eslint 配置
    react axios 配置
    react 路由之react-router-dom
    react mobx 装饰器语法配置
  • 原文地址:https://www.cnblogs.com/ttttttttrx/p/10252617.html
Copyright © 2020-2023  润新知