zoukankan      html  css  js  c++  java
  • uva 12526

    字典树,可惜比赛的时候有两句话写倒了;

    害得我调了一个小时;

    今天不宜做题 = =

    代码:

     1 #include<cstdio>
     2 #include<cstring>
     3 #define maxn 2600009
     4 using namespace std;
     5 
     6 struct node
     7 {
     8     bool flag;
     9     int cnt;
    10     node *a[26];
    11 } no[maxn];
    12 
    13 char s[100];
    14 int ans,nonocount;
    15 node *newnode()
    16 {
    17     node *p=no+nonocount++;
    18     p->flag=0;
    19     p->cnt=0;
    20     for(int i=0; i<26; i++)
    21         p->a[i]=NULL;
    22     return p;
    23 }
    24 
    25 void insert(node *rt,char *s)
    26 {
    27     int l=strlen(s);
    28     int i=0;
    29     while(1)
    30     {
    31         if(rt->a[s[i]-'a']==NULL) rt->a[s[i]-'a']=newnode();
    32         rt=rt->a[s[i]-'a'];
    33         rt->cnt++;
    34         i++;
    35         if(i==l)
    36         {
    37             rt->flag=1;
    38             break;
    39         }
    40     }
    41 }
    42 
    43 void query(node *rt)
    44 {
    45     int cot=0;
    46     for(int i=0; i<26; i++)
    47     {
    48         if(rt->a[i]!=NULL)
    49         {
    50             cot++;
    51             query(rt->a[i]);
    52         }
    53     }
    54     if(cot>1)
    55     {
    56         ans+=rt->cnt;
    57         if(rt->flag==1)ans--;
    58     }
    59     else if(rt->flag==1)
    60     {
    61         ans+=(rt->cnt-1);
    62     }
    63 }
    64 
    65 int main()
    66 {
    67     int n;
    68     while(scanf("%d",&n)!=EOF)
    69     {
    70         nonocount=0;
    71         node *p=newnode();
    72         for(int i=0; i<n; i++)
    73         {
    74             scanf("%s",s);
    75             insert(p,s);
    76         }
    77         ans=0;
    78         query(p);
    79         printf("%.2lf
    ",(double)(ans+n)/n);
    80     }
    81     return 0;
    82 }
    View Code
  • 相关阅读:
    扩展中国剩余定理
    bzoj 3816&&uoj #41. [清华集训2014]矩阵变换
    THUSC2017
    bzoj 4521: [Cqoi2016]手机号码
    bzoj 4871: [Shoi2017]摧毁“树状图”
    bzoj 2300 : [HAOI2011]防线修建
    bzoj 3853 : GCD Array
    HEOI 2017 游记
    bzoj3926: [Zjoi2015]诸神眷顾的幻想乡 广义后缀自动机模板
    bzoj 4310 跳蚤
  • 原文地址:https://www.cnblogs.com/yours1103/p/3389833.html
Copyright © 2011-2022 走看看