• 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 }
    水水水
  • 相关阅读:
    推荐有关微信开发的十个开源项目
    curl 常见错误码
    mysql修改root密码
    BAT批处理之文件与文件夹操作代码(附xcopy命令详解)
    UE4 Pak包加载
    libcurl 文件上传
    win7右键在目录当前打开命令cmd窗口
    SQLiteDeveloper破解
    Excel替换应用
    解决局域网2级路由互相连接共享的问题
  • 原文地址:https://www.cnblogs.com/MingSD/p/8708488.html
Copyright © 2020-2023  润新知