zoukankan      html  css  js  c++  java
  • HDU 1251 统计难题 字典树大水题

    今天刚看的字典树, 就RE了一发, 字典树原理还是很简单的, 唯一的问题就是不知道一维够不够用, 就开的贼大, 这真的是容易MLE的东西啊, 赶紧去学优化吧。

    HDU-1251 统计难题

    这道题唯一的问题就是会不会字典树, 2333, 给一个字典树的博客传送门, 话说这个博客一搜就搜到了啊.

    代码:

     1 #include<bits/stdc++.h>
     2 using namespace std;
     3 #define LL long long
     4 #define ULL unsigned LL
     5 #define fi first
     6 #define se second
     7 #define lson l,m,rt<<1
     8 #define rson m+1,r,rt<<1|1
     9 #define max3(a,b,c) max(a,max(b,c))
    10 #define min3(a,b,c) min(a,min(b,c))
    11 const int INF = 0x3f3f3f3f;
    12 const LL mod = 1e9+7;
    13 typedef pair<int,int> pll;
    14 const int N = 1e6+10;
    15 int tree[N][26];
    16 int sum[N];
    17 char str[15];
    18 int tot = 2;
    19 void Insert(){
    20     int rt = 1;
    21     int len = strlen(str);
    22     for(int i = 0; i < len; i++){
    23         int id = str[i] - 'a';
    24         if(tree[rt][id] == 0) tree[rt][id] = tot++;
    25         sum[tree[rt][id]]++;
    26         rt = tree[rt][id];
    27     }
    28 }
    29 int Find(){
    30     int rt = 1;
    31     int len = strlen(str);
    32     for(int i = 0; i < len; i++){
    33         int id = str[i] - 'a';
    34         if(tree[rt][id] == 0) return 0;
    35         rt = tree[rt][id];
    36     }
    37     return sum[rt];
    38 }
    39 int main(){
    40     while(cin.getline(str, 15)){
    41         if(!isalpha(str[0])) break;
    42         Insert();
    43     }
    44     while(cin.getline(str, 15)){
    45         printf("%d
    ",Find());
    46     }
    47     return 0;
    48 }
    水水水
  • 相关阅读:
    to_char &&to_date
    java中Integer 与 String 类型的 相互 转换
    group by 的用法
    谈 计算时间的天数差
    领域建模
    Java Classloader详解
    阿里巴巴Java招聘
    Maven Archetype
    负载均衡
    Maven
  • 原文地址:https://www.cnblogs.com/MingSD/p/8708488.html
Copyright © 2011-2022 走看看