1305 Immediate Decodability(字典树)

Immediate Decodability(链接)

Sample Input
   
   
01 10 0010 0000 9 01 10 010 0000 9
 

Sample Output
   
   
Set 1 is immediately decodable Set 2 is not immediately decodable
//模板题
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
using namespace std;

const int maxn = 2;
struct node
{
    node *next[maxn];
    int num;
}root;

node *newnode()
{
    node *p = new node;
    p->num = 0;
    p->next[0] = NULL;
    p->next[1] = NULL;
    return p;
}

int buildTree(node *root,char *t)
{
    int len = strlen(t);
    node *p = root;
    for(int i=0;i<len;i++)
    {
        int a=t[i]-'0';
        if(p->num!=0)
            return 0;
        if(p->next[a]==NULL)
            p->next[a]=newnode();
        p=p->next[a];
    }
    if(p->next[0]==NULL&&p->next[1]==NULL&&p->num==0)
    {
        p->num = 1;
        return 1;
    }
    else
        return 0;
}

void freeTree(node *p)
{
    for(int i=0;i<2;i++)
    {
        if(p->next[i])
            freeTree(p->next[i]);
    }
    free(p);
}

char str[1010][1010];
int main()
{
    int Case = 1,n=0,i,m;
    node *head;
    while(~scanf("%s",&str[n++]))
    {
        if(str[n-1][0]=='9')
        {
            head=newnode();
            for(i=0;i<n-1;i++)
            {
                m = buildTree(head,str[i]);
                if(m==0)
                    break;
            }
            if(i==n-1)
                printf("Set %d is immediately decodable\n",Case++);
            else
                printf("Set %d is not immediately decodable\n",Case++);
            n=0;
            freeTree(head);
        }
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值