【trie树】hdu 1305

本文介绍了一种使用位运算而非传统结构体来构建树形数据结构的方法,并通过一个具体实例展示了如何仅利用数组下标关系来记录节点关系。这种方法不仅节省内存,还简化了代码实现。

看来一个大牛的code,好厉害,完全不用结构体啥的去建树,就只利用数组的下标关系去记录,赞~

#include <map>
#include <set>
#include <list>
#include <queue>
#include <deque>
#include <stack>
#include <string>
#include <time.h>
#include <cstdio>
#include <math.h>
#include <iomanip>
#include <cstdlib>
#include <limits.h>
#include <string.h>
#include <iostream>
#include <fstream>
#include <algorithm>
using namespace std;

#define LL long long
#define MIN INT_MIN
#define MAX INT_MAX
#define PI acos(-1.0)
#define FRE freopen("input.txt","r",stdin)
#define FF freopen("output.txt","w",stdout)
#define N 1005
char str[15];
int tree[(1<<10)+10];//位运算加括号啊!!!否则会不明不白的WA!!!
bool tag;
void gao(char *s){
    int i,j;
    for(j = 0, i = 2; s[j]; i<<=1,j++){
        if(tree[i] == 2){
            tag = 1;cout<<str[j]<<endl;
            return ;
        }
        if(s[j] == '0')tree[i] = 1;
        else{
            i++;
            tree[i] = 1;
        }
    }
    if(tree[i] == 1){
        tag = 1;
        return ;
    }
    tree[i] = 2;
}
int main(){
    int t = 1;
    tag = 0;
    memset(tree,0,sizeof(tree));
    while(scanf("%s",str) != EOF){
        if(str[0] == '9'){
            printf("Set %d is ", t++);
            printf(tag ? "not immediately decodable\n" : "immediately decodable\n");
            tag = 0;
            memset(tree,0,sizeof(tree));
        }else
        if(!tag){
            gao(str);
        }
    }
    return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值