pku 1056(字典树)

本文探讨了字典树的应用及其在解决立即可解集编码问题中的作用,通过实例展示了如何使用字典树进行编码并判断集是否立即可解。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

字典树,做的,据说暴力能过。。。

 

代码:

#include <iostream>
#include <string>
using namespace std;

struct tree {
    int nxt[2];
    bool isWord;
    void init() {
        nxt[0]=nxt[1] = -1;
        isWord = false;
    }
}num[1000010];

int len;

int main() {

    string str;
    len = 0;
    bool pan = true;
    num[0].init();
    int ca(1);
    while (cin>>str) {
        if (str=="9") {

          if(pan) cout<<"Set "<<ca++<<" is immediately decodable"<<endl;
          else cout<<"Set "<<ca++<<" is not immediately decodable"<<endl;

          len = 0;
          num[0].init();
          pan = true;
        } else {
            int s(0);
            int e = str.size();
            int i(0);
            while (s<e) {

                if (num[i].nxt[str[s]-'0']!=-1) {
                  i = num[i].nxt[str[s]-'0'];
                  if(s==e-1) num[i].isWord = true;
                  if(num[i].isWord) pan = false;

                } else {
                    ++len;
                     num[len].init();
                     num[i].nxt[str[s]-'0'] =len;
                     i = len;
                     if(s==e-1) num[i].isWord = true;

                }
                ++s;
            }

        }

    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值