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 {<tt>A, B, C, D</tt>}

 


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 <tt>A</tt> is a prefix of <tt>C</tt>)

 

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; }

 

全部评论

相关推荐

头像
11-26 15:46
已编辑
中南大学 后端
字节国际 电商后端 24k-35k
点赞 评论 收藏
分享
无情咸鱼王的秋招日记之薛定谔的Offer:好拒信,偷了,希望有机会用到
点赞 评论 收藏
分享
喜欢吃蛋糕仰泳鲈鱼是我的神:字节可以找个hr 给你挂了,再放池子捞
点赞 评论 收藏
分享
评论
点赞
收藏
分享
牛客网
牛客企业服务