Hdu1305immediate decodability (Dictionary tree)

Source: Internet
Author: User

Check whether

This question is lucky. The solution is to save the input strings and sort them.

Problem descriptionan 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) inputwrite a program that accepts as input a series of groups of records from input. 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 ). outputfor each group, your program shocould determine whether the codes in that group are immediately decodable, and shocould print a single output line giving the Group number and stating whether the group is, or is not, immediately decodable. sample input0110001000009011001000009 sample outputset 1 is immediately decodableset 2 is not immediately decodable

# Include <iostream>

# Include <stdio. h>

# Include <string. h>

# Include <stdlib. h>

Using namespace STD;

Typedef struct Node

{

Struct node * Next [2];

Int flag;

} Node, * tree;

Int flag1;

Void creat (Tree & T)

{

T = (node *) malloc (sizeof (node ));

T-> flag = 0;

For (INT I = 0; I <2; I ++)

T-> next [I] = NULL;

}

Void insert (Tree & T, char * s)

{

Tree P = T;

Int T;

Int L = strlen (s );

For (INT I = 0; I <L; I ++)

{

T = s [I]-'0 ';

If (p-> next [T] = NULL)

Creat (p-> next [T]);

P = p-> next [T];

If (p-> flag> 0)

Flag1 = 0;

}

P-> flag ++;

}

Void D (tree P)

{

For (INT I = 0; I <2; I ++)

{

If (p-> next [I]! = NULL)

D (P-> next [I]);

}

Free (P );

}

Int main ()

{

Char A [30];

Int KK = 0;

Tree T;

While (scanf ("% S % * C", )! = EOF)

{

Creat (t );

Kk ++;

Flag1 = 1;

Insert (t, );

While (scanf ("% s", )! = EOF)

{

If (strcmp (a, "9") = 0)

Break;

Insert (t, );

}

If (flag1)

Printf ("set % d is immediately decodable \ n", KK );

Else printf ("set % d is not immediately decodable \ n", KK );

D (t );

}

Return 0;

}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.