zoukankan      html  css  js  c++  java
  • UVA

    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 ", num); else printf("Set %d is not immediately decodable ", num); free_node(head); n = 0; } } } int main(){ input(); return 0; }
  • 相关阅读:
    Sicily 1153. 马的周游问题 解题报告
    回溯法与八皇后问题
    Sicily 1151. 魔板 解题报告
    Sicily 1176. Two Ends 解题报告
    Sicily 1046. Plane Spotting 解题报告
    Java多线程11:ReentrantLock的使用和Condition
    Java多线程10:ThreadLocal的作用及使用
    Java多线程9:ThreadLocal源码剖析
    Java多线程8:wait()和notify()/notifyAll()
    Java多线程7:死锁
  • 原文地址:https://www.cnblogs.com/Asimple/p/6890870.html
Copyright © 2011-2022 走看看