UVA - 644 Immediate Decodability

An encoding of a set of symbols is said to be immediately decodable if no code for one symbol is the prefix of a code for another symbol. We will assume for this problem that all codes are in binary, that no two codes within a set of codes are the same, that each code has at least one bit and no more than ten bits, and that each set has at least two codes and no more than eight.

 


Examples: Assume an alphabet that has symbols {A, B, C, D}

 


The following code is immediately decodable:

 


A:01 B:10 C:0010 D:0000

 


but this one is not:

 


A:01 B:10 C:010 D:0000 (Note that A is a prefix of C)

 

Input 

Write a program that accepts as input a series of groups of records from a data file. Each record in a group contains a collection of zeroes and ones representing a binary code for a different symbol. Each group is followed by a single separator record containing a single 9; the separator records are not part of the group. Each group is independent of other groups; the codes in one group are not related to codes in any other group (that is, each group is to be processed independently).

 

Output 

For each group, your program should determine whether the codes in that group are immediately decodable, and should print a single output line giving the group number and stating whether the group is, or is not, immediately decodable.

 


The Sample Input describes the examples above.

 

Sample Input 

 

01
10
0010
0000
9
01
10
010
0000
9

 

Sample Output 

Set 1 is immediately decodable
Set 2 is not immediately decodable

 判断是否有字符串是其他字符串的前缀。

简单字典树。(人生第一个字典树的题目)。

复制代码
 //Asimple
#include <bits/stdc++.h>
//#define INF 0x3fffffff
#define mod 10007
#define CLS(a, v) memset(a, v, sizeof(a))
#define debug(a)  cout << #a << " = "  << a <<endl
using namespace std;
typedef long long ll;
const int maxn = 20000+5;
const double PI=acos(-1.0);
const int INF = ( 1 << 31 ) ;
ll n, m, res, ans, len, T, k, num, sum, x, y;

struct node{
    int f;
    node *nx[2];
};

node *newnode(){
    node *p = new node;
    p->f = 0;
    p->nx[0] = NULL;
    p->nx[1] = NULL;
    return p;
}

int get_tree(node* root, char* s){
    int i, l = strlen(s);
    node *p = root;
    for(i=0; i<l; i++) {
        int k = s[i]-'0';
        if( p->f ) return 0;
        if( p->nx[k]==NULL ) p->nx[k] = newnode();
        p = p->nx[k];
    }
    if( p->nx[0]==NULL && p->nx[1]==NULL && p->f==0) {
        p->f = 1;
        return 1;
    } else return 0;
}

void free_node(node* p){
    if( p->nx[0] ) free_node(p->nx[0]);
    if( p->nx[1] ) free_node(p->nx[1]);
    free(p);
}

void input() { ios_base::sync_with_stdio(false); char s[20][20]; int n = 0, num = 0; node* head; while( scanf("%s", s[n++])!=EOF ) { if( s[n-1][0]!='9'); else { n --; num++; head = newnode(); int i; for(i=0; i<n; i++) { int k = get_tree(head, s[i]); if( k == 0 ) break; } if(i==n) printf("Set %d is immediately decodable\n", num); else printf("Set %d is not immediately decodable\n", num); free_node(head); n = 0; } } } int main(){ input(); return 0; }
复制代码

 

posted @   Asimple  阅读(165)  评论(0编辑  收藏  举报
编辑推荐:
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
阅读排行:
· 终于写完轮子一部分:tcp代理 了,记录一下
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
历史上的今天:
2016-05-22 ACM题目————The Blocks Problem
2016-05-22 ACM学习之路————一个大整数与一个小整数不得不说得的秘密
2016-05-22 ACM题目————Anagram
2016-05-22 ACM题目————马拦过河卒
点击右上角即可分享
微信分享提示