zoukankan      html  css  js  c++  java
  • hdu 1305 Immediate Decodability

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

    字典树裸题,如下:

     1 #include<algorithm>
     2 #include<iostream>
     3 #include<cstdlib>
     4 #include<cstring>
     5 #include<cstdio>
     6 const int Max_N = 100000;
     7 struct Node{
     8     int cnt;
     9     Node *next[2];
    10     inline void set(){
    11         cnt = 0;
    12         for (int i = 0; i < 2; i++) next[i] = NULL;
    13     }
    14 };
    15 struct Trie{
    16     Node stack[Max_N], *root, *tail;
    17     void init(){
    18         tail = &stack[0];
    19         root = tail++;
    20         root->set();
    21     }
    22     inline Node *newNode(){
    23         Node *p = tail++;
    24         p->set();
    25         return p;
    26     }
    27     inline void insert(Node *x, char *src){
    28         char *p = src;
    29         while (*p != ''){
    30             if (!x->next[*p - '0']) x->next[*p - '0'] = newNode();
    31             x = x->next[*p - '0'];
    32             x->cnt++;
    33             p++;
    34         }
    35     }
    36     inline int query(Node *x, char *src){
    37         char *p = src;
    38         while (*p != ''){
    39             if (!x || !x->next[*p - '0']) return 0;
    40             x = x->next[*p - '0'];
    41             p++;
    42         }
    43         return x->cnt;
    44     }
    45     inline void insert(char *src){
    46         insert(root, src);
    47     }
    48     inline int query(char *src){
    49         return query(root, src);
    50     }
    51 }trie;
    52 char ret[20][200], buf[200];
    53 int main(){
    54 #ifdef LOCAL
    55     freopen("in.txt", "r", stdin);
    56     freopen("out.txt", "w+", stdout);
    57 #endif
    58     trie.init();
    59     int i, n = 0, k = 1, flag = 1;
    60     while (~scanf("%s", buf)){
    61         if (0 != strcmp(buf, "9")){
    62             trie.insert(buf);
    63             strcpy(ret[n++], buf);
    64         } else if (0 == strcmp(buf, "9")){
    65             flag = 0;
    66             for (i = 0; i < n; i++){
    67                 if (trie.query(ret[i]) > 1){
    68                     printf("Set %d is not immediately decodable
    ", k++);
    69                     flag = 1;
    70                     break;
    71                 }
    72             }
    73             if (!flag) printf("Set %d is immediately decodable
    ", k++);
    74             n =  0, flag = 1;
    75             trie.init();
    76         }
    77     }
    78     return 0;
    79 }
    View Code
    By: GadyPu 博客地址:http://www.cnblogs.com/GadyPu/ 转载请说明
  • 相关阅读:
    arcgis server10.2自带打印模板路径
    【js笔记】数组那些事[0]
    微信打开网页不能下载的解决
    CSS里一个奇怪的属性
    存一些有用的CSS
    【JS笔记】闭包
    关于百度空间的关闭
    数据校验插件开发
    JavaScript 内存机制
    手写JQuery 的框架的实现
  • 原文地址:https://www.cnblogs.com/GadyPu/p/4485291.html
Copyright © 2011-2022 走看看