hdu 1305 Immediate Decodability

题意:给你一些0 1串,判断这些0 1串之间没有一个串是另一个串的前缀。
做法:Trie树,用count函数判断以它为头字符串的字符串的个数是否大于1就行了

#include<iostream>
#include<string>
#include<vector>
#include<cstring>
#include<cstdio>
using namespace std;
#define maxn 500
struct node
{    char* s;
     int count1;
     bool isword;
     node* next[2];
     node()
     {    s=NULL;
          count1=0;
          isword=false;
          memset(next,0,sizeof(next));
     }
}*root;

void insert(node* root,char* s)
{    node* p=root;
     for(int i=0;s[i];i++)
     {    int x=s[i]-'0';
          p->s=s+i;
          if(p->next[x]==NULL)
              p->next[x]=new node;
          p=p->next[x];
          p->count1++;
     }
     p->isword=true;
}

bool Search(node* root,const char* s)
{    node* p=root;
     for(int i=0;s[i];i++)
     {    int x=s[i]-'0';
          if(p->next[x]==NULL)
               return false;
          p=p->next[x];
     }
return p->isword;
}

int count(node* root,const char* s)
{
     node* p=root;
     for(int i=0;s[i];i++)
     {
          int x=s[i]-'0';
          if(p->next[x]==NULL)
    return 0;
          p=p->next[x];
     }
return p->count1 ;
}
int main()
{
     char s[maxn];
     bool flag;
     int cas = 1;
     while(scanf("%s",s) != EOF)
     {     flag = true;
           root = new node;
           vector<string> v;
           insert(root,s);
           string str(s);
           v.push_back(str);
           while(scanf("%s",s))
           {
                if(s[0]=='9') break;
                insert(root,s);
                string str1(s);
                v.push_back(str1);
           }
           for(int i = 0;i < (int)v.size(); i++)
           {
               if(count(root,v[i].c_str()) > 1)
                {
                    flag = false;
                    break;
                }
           }
           if(flag)
                printf("Set %d is immediately decodable\n",cas++);
           else
                printf("Set %d is not immediately decodable\n",cas++);
     }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值