zoukankan      html  css  js  c++  java
  • POJ3630 Phone List Trie树 | qsort

      题目链接:http://poj.org/problem?id=3630

      建立一个Trie树查找就可以了,但是这里动态建立Trie居然会超时,静态化居然可以秒,太不厚道了= =

      当然还可以用qsort秒过,依次比较。。。

     1 //STATUS:C++_AC_110MS_2568KB
     2 #include<stdio.h>
     3 #include<stdlib.h>
     4 #include<string.h>
     5 #include<math.h>
     6 #include<iostream>
     7 #include<string>
     8 #include<algorithm>
     9 #include<vector>
    10 #include<queue>
    11 #include<stack>
    12 #include<map>
    13 using namespace std;
    14 #define LL long long
    15 #define pii pair<int,int>
    16 #define Max(a,b) ((a)>(b)?(a):(b))
    17 #define Min(a,b) ((a)<(b)?(a):(b))
    18 #define mem(a,b) memset(a,b,sizeof(a))
    19 #define lson l,mid,rt<<1
    20 #define rson mid+1,r,rt<<1|1
    21 #define PI acos(-1.0)
    22 const int N=10010,INF=0x3f3f3f3f,MOD=10000,STA=8000010;
    23 const LL LNF=0x3f3f3f3f3f3f3f3f;
    24 const double DNF=1e13;
    25 //
    26 void swap(int& a,int& b){int t=a;a=b;b=t;}
    27 void swap(LL& a,LL& b){LL t=a;a=b;b=t;}
    28 //
    29 
    30 struct Trie {
    31     int ch[1<<16][10];
    32     int val[1<<16];
    33     int sz;
    34 
    35     void init(){sz=1;mem(ch[0],0);}
    36     void insert(char *s,int v){
    37         int i,len=strlen(s),id,u=0;
    38         for(i=0;i<len;i++){
    39             id=s[i]-'0';
    40             if(!ch[u][id]){
    41                 mem(ch[sz],0);
    42                 val[sz]=0;
    43                 ch[u][id]=sz++;
    44             }
    45             u=ch[u][id];
    46         }
    47         val[u]=v;
    48     }
    49     int find(char *s){
    50         int i,len=strlen(s),id,u=0;
    51         for(i=0;i<len;i++){
    52             id=s[i]-'0';
    53             if(!ch[u][id])return 0;
    54             u=ch[u][id];
    55         }
    56         for(i=0;i<10;i++)
    57             if(ch[u][id])return 1;
    58         return 0;
    59     }
    60 }trie;
    61 
    62 int T,n;
    63 char s[12];
    64 
    65 int main()
    66 {
    67  //   freopen("in.txt","r",stdin);
    68     int i,j,ok;
    69     scanf("%d",&T);
    70     while(T--)
    71     {
    72         ok=1;
    73         trie.init();
    74         scanf("%d",&n);
    75         while(n--){
    76             scanf("%s",s);
    77             if(trie.insert(s,1)){ok=0;break;}
    78         }
    79         if(n>1)while(n--)scanf("%s",s);
    80 
    81         printf("%s\n",ok?"YES":"NO");
    82     }
    83     return 0;
    84 }
  • 相关阅读:
    URAL 2046 A
    URAL 2056 Scholarship 水题
    Codeforces Gym 100286I iSharp 水题
    Codeforces Gym H. Hell on the Markets 贪心
    Codeforces Gym 100286G Giant Screen 水题
    Codeforces Gym 100286B Blind Walk DFS
    Codeforces Gym 100286F Problem F. Fibonacci System 数位DP
    Codeforces Gym 100286A. Aerodynamics 计算几何 求二维凸包面积
    Codeforces Gym 100418K Cards 暴力打表
    Codeforces Gym 100418J Lucky tickets 数位DP
  • 原文地址:https://www.cnblogs.com/zhsl/p/3024866.html
Copyright © 2011-2022 走看看