UVA 644 Immediate Decodability

本文通过使用字典树(Trie)结构解决输入01串中是否存在一个串为另一个串前缀的问题,详细介绍了建树、插入、清理过程,并提供了解决方案的代码实现。

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

字典树,人老了,就胆小了,两个之间挨个比较貌似也可以过,但我怕超时,就直接搞个Trie,就当复习一下。

输入一堆01串,问是否有一个串是别的串的前缀。建立二叉树,插入时,如果经过之前的某个串的end,说明之前的那个串为当前串的前缀,如果本次插入结束后,仍有后续节点,说明当前串是之前某串的前缀。

算是复习了Trie的建树,插入,及最后的清理。

#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<vector>
#include<string>
#include<cmath>
#include<map>
///LOOP
#define REP(i, n) for(int i = 0; i < n; i++)
#define FF(i, a, b) for(int i = a; i < b; i++)
#define FFF(i, a, b) for(int i = a; i <= b; i++)
#define FD(i, a, b) for(int i = a - 1; i >= b; i--)
#define FDD(i, a, b) for(int i = a; i >= b; i--)
///INPUT
#define RI(n) scanf("%d", &n)
#define RII(n, m) scanf("%d%d", &n, &m)
#define RIII(n, m, k) scanf("%d%d%d", &n, &m, &k)
#define RIV(n, m, k, p) scanf("%d%d%d%d", &n, &m, &k, &p)
#define RV(n, m, k, p, q) scanf("%d%d%d%d%d", &n, &m, &k, &p, &q)
#define RFI(n) scanf("%lf", &n)
#define RFII(n, m) scanf("%lf%lf", &n, &m)
#define RFIII(n, m, k) scanf("%lf%lf%lf", &n, &m, &k)
#define RFIV(n, m, k, p) scanf("%lf%lf%lf%lf", &n, &m, &k, &p)
#define RS(s) scanf("%s", s)
///OUTPUT
#define PN printf("\n")
#define PI(n) printf("%d\n", n)
#define PIS(n) printf("%d ", n)
#define PS(s) printf("%s\n", s)
#define PSS(s) printf("%s ", n)
#define PC(cas) printf("Excuse Set #%d\n", cas)
///OTHER
#define PB(x) push_back(x)
#define CLR(a, b) memset(a, b, sizeof(a))
#define CPY(a, b) memcpy(a, b, sizeof(b))
#define display(A, n, m) {REP(i, n){REP(j, m)PIS(A[i][j]);PN;}}

using namespace std;
typedef long long LL;
typedef pair<int, int> P;
const int MOD = 1000000;
const int INFI = 1e9 * 2;
const LL LINFI = 1e17;
const double eps = 1e-6;
const double pi = acos(-1.0);
const int N = 88;
const int M = 33;
const int move[8][2] = {0, 1, 0, -1, 1, 0, -1, 0, 1, 1, 1, -1, -1, 1, -1, -1};

bool flag;
char s[N];
struct node
{
    bool end;
    node *next[2];
    node(){end = 0, next[0] = next[1] = NULL;};
}root;

void insert(node *t)
{
    int l = strlen(s), k;
    REP(i, l)
    {
        k = s[i] - '0';
        if(t -> next[k])
        {
            t = t -> next[k];
            if(t -> end)flag = 0;
        }
        else
        {
            node *p = new node();
            t -> next[k] = p;
            t = p;
        }
    }
    t -> end = 1;
    if(t -> next[0] || t -> next[1])flag = 0;
}

void del(node *t)
{
    if(t -> next[0])del(t -> next[0]);
    if(t -> next[1])del(t -> next[1]);
    if(t != &root)delete(t);
}

int main()
{
    //freopen("input.txt", "r", stdin);
    //freopen("output.txt", "w", stdout);

    int cas = 1;
    flag = 1;
    root.end = 0;
    root.next[0] = root.next[1] = NULL;
    while(gets(s))
    {
        if(s[0] == '9')
        {
            printf("Set %d is%simmediately decodable\n", cas++, flag ? " " : " not ");
            del(&root);
            flag = 1;
            root.end = 0;
            root.next[0] = root.next[1] = NULL;
        }
        else if(flag)insert(&root);
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值