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

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

      题意:给你几个串,如果这些串中不存在一个串是另一个串的前缀,就说明这一组串是immediately decodable,反之则不是。

      思路:先把所有串全部插入,然后来一个循环判断每个单词结尾处的cnt与1的大小关系,大于1的话说明这个串是某一个或者某几个串的前缀,那么就是not immediately decodable。

      代码:

    View Code
    #include <algorithm>
    #include <iostream>
    #include <cstdlib>
    #include <cstring>
    #include <cstdio>
    #include <queue>
    #include <cmath>
    #include <stack>
    
    using namespace std;
    
    const int maxn=2;
    
    struct node
    {
        int cnt;
        node *next[maxn];
        node()
        {
            cnt=0;
            for(int i=0;i<maxn;i++)
                next[i]=NULL;
        }
        ~node()
        {
            for(int i=0;i<maxn;i++)
            {
                if(next[i]!=NULL)
                    next[i]=NULL;
            }
        }
    };
    
    class Trie
    {
        public:
        node *root;
        Trie()
        {
            root=NULL;
        }
        void insert(char *s)
        {
            if(!root)
                root=new node();
            node *loca=root;
            for(int i=0;s[i];i++)
            {
                int id=s[i]-'0';
                if(loca->next[id]==NULL)
                    loca->next[id]=new node();
                loca->next[id]->cnt++;
                loca=loca->next[id];
            }
        }
        int search(char *s)
        {
            node *loca=root;
            for(int i=0;s[i];i++)
            {
                int id=s[i]-'0';
                if(loca->next[id]==NULL)
                    return 0;
                loca=loca->next[id];
            }
            return loca->cnt;
        }
    };
    
    int main()
    {
        char str[15][15];
        int line=0,cnt=1;
        while(~scanf("%s",str[line]))
        {
            Trie t;
            t.insert(str[line++]);
            while(scanf("%s",str[line]))
            {
                if(str[line][0]=='9') break;
                t.insert(str[line++]);
            }
            bool flg=false;
            for(int i=0;i<line;i++)
            {
                if(t.search(str[i])>1)
                {
                    flg=true;
                    break;
                }
            }
            printf("Set %d is ",cnt++);
            if(flg)
                printf("not ");
            printf("immediately decodable\n");
            line=0;
        }
        return 0;
    }

     善待每一天,努力做好自己。

    欢迎转载,注明出处。

  • 相关阅读:
    简单四则运算实现--第二次作业
    人生第一篇博客
    团队任务1:第一次团队会议
    第二次作业
    自我介绍
    五号团队—团队任务4:每日立会(2018-11-27)
    软件设计与开发准备
    原型设计与UI设计
    第一次团队会议
    课后作业2
  • 原文地址:https://www.cnblogs.com/RainingDays/p/3068758.html
Copyright © 2011-2022 走看看