1305 Immediate Decodability(字典树)

来源:互联网 发布:淘宝售假次数如何计算 编辑:程序博客网 时间:2024/06/01 10:04

Immediate Decodability(链接)

Sample Input
0110001000009011001000009
 

Sample Output
Set 1 is immediately decodableSet 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;}


原创粉丝点击