Immediate Decodability UVA - 644

 

//有两个问题,第一个:Trie root 和Trie *root,要选*root....    root只能有第一个输出,然后就结束了...目前还不知道为啥,第二个就是DelTrie完以后要给root动态分配内存...要不然也会和第一个问题一样.......

#include <iostream>
#include <string.h>
#include <cstdio>

using namespace std;

struct Trie{    //字典树定义
    Trie* next[2];
    int num;    //以当前字符串为前缀的单词的数量
    Trie()    //构造函数
    {
        int i;
        for(i=0; i<2; i++){
            next[i] = NULL;
        }
        num=0;
    }
}*root;

void Insert(char word[])    //将字符串s[i]插入到字典树中
{
    Trie *p = root;
    int i;
    for(i=0;word[i];i++){

        if(p->next[word[i]-'0']==NULL)
            p->next[word[i]-'0'] = new Trie();

        p = p->next[word[i]-'0'];
        p->num++;
    }
}
int Find(char word[])    //返回以字符串s[i]为前缀的单词的数量
{
    Trie *p = root;
    int i;

    for(i=0;word[i];i++){
        if(p->next[word[i]-'0']==NULL)
            return 0;

        p = p->next[word[i]-'0'];
    }
    return p->num;
}

int DelTrie(Trie* T)
{
    int i;
    if(T==NULL)
        return 0;

    for(i=0; i<2; i++)
    {
        if(T->next[i]!=NULL)
            DelTrie(T->next[i]);
    }
    free(T);
//    T = NULL;
//    T -> num = 0;
    return 0;
}

int main()
{
    char s[15][20];
    int i=0;

    root = new Trie();
    int cnt = 1;
    while(cin >> s[i]){ //输入单词
        if(s[i][0] == '9'){
            int j;
            for(j=0; j<i; j++){
                if(Find(s[j]) > 1){
                    cout << "Set "<< cnt++ <<" is not immediately decodable" << endl;
                    break;
                }
            }
            if(j == i)
                cout << "Set " << cnt++ << " is immediately decodable" << endl;


            DelTrie(root);

            root = new Trie();
            i = 0;
        }
        else
            Insert(s[i++]);
    }
    return 0;
}

 

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值