POJ 1056 IMMEDIATE DECODABILITY(字典树)

字典树的每个节点都标记是不是某个字符串的结尾字符。

依次插入字符串,有两种错误情况:

1.插入时经过了某个是结尾字符的节点

2.到结尾字符时,对应的节点已经存在了


代码:

//
//  main.cpp
//  1056 IMMEDIATE DECODABILITY
//
//  Created by Baoli1100 on 15/4/5.
//  Copyright (c) 2015年 Baoli1100. All rights reserved.
//

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;

struct node{
    int n;
    int next[2];
    bool end;
}G[3000];

int Num=1;

bool Insert(char s[]){
    int len=strlen(s);
    int cur=0;
    for(int i=0;i<len;i++){
        int k=s[i]-'0';
        if(i==len-1&&G[cur].next[k]) return 0;
        if(!G[cur].next[k]){
            G[cur].next[k]=Num++;
        }
        cur=G[cur].next[k];
        if(G[cur].end) return 0;
    }
    G[cur].end=1;
    return 1;
}

int main(){
    char s[20];
    int kase=1;
    bool res=0;
    while(~scanf("%s",s)){
        if(s[0]=='9'){
            printf("Set %d is ",kase);
            if(res) printf("not ");
            printf("immediately decodable\n");
            memset(G,0,sizeof(G));
            Num=1;
            kase++;
            res=0;
            continue;
        }
        if(!Insert(s)) res=1;
        
    }
}


评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值