• Hdu 1305 【字典树】.cpp


    题意:

      给出多个串,如果没有一个串是别的串的前缀,则这些串是符合要求的,输出“Set * is immediately decodable”;

      否则输出 "Set * is not immediately decodable";

      输入中多个串以9表示输入结束

    思路:

      裸的字典树,先根据多个串建树,然后遍历每一个串看是否在字典树中..

      

    Tips:

      要注意找的时候应该判断是否为叶子结点,而不是仅仅判断是否在字典树中,因为建树的时候肯定存在该串..

      

    Code:

      

     1 #include <stdio.h>
     2 #include <cstring>
     3 #include <iostream>
     4 #include <algorithm>
     5 using namespace std;
     6 
     7 const int MAXN = 100010;
     8 
     9 char arr[10][20], str[20];
    10 int next[MAXN][30];
    11 int tot;
    12 void add(char *s)
    13 {
    14     int rt = 0;
    15     for (int i = 0; s[i]; ++i) {
    16         int a = s[i]-'0';
    17         if (next[rt][a] == 0) {
    18             next[rt][a] = ++tot;
    19         }
    20         rt = next[rt][a];
    21     }
    22 }
    23 
    24 bool find(char *s)
    25 {
    26     int rt = 0;
    27     for (int i = 0; s[i]; ++i) {
    28         int a = s[i]-'0';
    29         if (next[rt][a] == 0)
    30             return true;
    31         rt = next[rt][a];
    32     }
    33     for (int i = 0; i < 26; ++i)
    34         if (next[rt][i] != 0)
    35             return false;
    36     return true;
    37 }
    38 
    39 int main()
    40 {
    41   //  freopen("in.txt", "r", stdin);
    42     bool flag = true;
    43     int iCase = 1, cnt;
    44     while (~scanf("%s", str)) {
    45         flag = true;
    46         memset(next, 0, sizeof(next));
    47         tot = cnt = 0;
    48         while (strcmp(str, "9") != 0) {
    49             strcpy(arr[cnt++], str);
    50             add(str);
    51             scanf("%s", str);
    52         }
    53 
    54         for (int i = 0; i < cnt; ++i) {
    55             if (find(arr[i]) == false) {
    56                 flag = false;
    57                 break;
    58             }
    59         }
    60         if (flag) printf("Set %d is immediately decodable\n", iCase++);
    61         else printf("Set %d is not immediately decodable\n", iCase++);
    62     }
    63     return 0;
    64 }
    View Code

    链接:http://acm.hdu.edu.cn/showproblem.php?pid=1305

  • 相关阅读:
    复合文档(Compound Document)读写栗子
    JavaScript修改IE注册表
    mysql_real_connect 端口号说明
    _beginthreadex创建线程,立即执行?
    Access 是/否 字段
    JavaScript格式化日期输出
    STM32-串行SPI nor
    全球唯一标识符:GUID在线生成
    如何交叉编译Python到ARM-Linux平台(转)
    CMOS Sensor的调试经验分享(转)
  • 原文地址:https://www.cnblogs.com/Griselda/p/3122646.html
Copyright © 2020-2023  润新知