Ultraviolet A 644 immediate decodability

Source: Internet
Author: User

I have read the problem several times, even though Google Translate does not understand it.

The meaning of the question is very simple. It is to give strings consisting of '0' and '1' that are different from each other to see if a string will become a substring starting with another one.

Simply and roughly compare it.

This is the original question:



Immediate decodability

 

An encoding of a set of symbols is said to beImmediatelyDecodable 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 thatAIs a prefixC)

 

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 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.

 


The sample input describes the examples abve.

 

Sample Input

 

0110001000009011001000009

 

Sample output
Set 1 is immediately decodableSet 2 is not immediately decodable

 

 

Miguel A. Revilla
2000-01-17

AC code:

1 // # define local 2 # include <iostream> 3 # include <cstdio> 4 # include <cstring> 5 # include <algorithm> 6 using namespace STD; 7 8 char code [9] [11]; 9 bool CMP (char S1 [], char S2 []); 10 11 int main (void) 12 {13 # ifdef local14 freopen ("644in.txt", "r", stdin); 15 # endif16 int Kase = 0, N; 17 while (gets (code [0]) 18 {19 bool flag = false; 20 int I, j; 21 n = 0; 22 while (code [N] [0]! = '9') 23 gets (code [++ N]); 24 25 for (I = 0; I <n-1; ++ I) 26 {27 if (FLAG) 28 break; 29 for (j = I + 1; j <n; ++ J) 30 {31 flag = CMP (code [I], code [J]); 32 If (FLAG) break; 33} 34} 35 36 IF (! Flag) 37 printf ("set % d is immediately decodable \ n", ++ Kase); 38 else39 printf ("set % d is not immediately decodable \ n ", + + Kase); 40} 41 return 0; 42} 43 // compare whether a string will start with another substring 44 bool CMP (char S1 [], char S2 []) 45 {46 int L1 = strlen (S1); 47 int L2 = strlen (S2); 48 int Lmin = min (L1, L2), I = 0; 49 If (L1 = l2) // If the length is equal, it cannot be a substring of 50 return false; 51 for (I = 0; I <Lmin; ++ I) 52 {53 If (S1 [I]! = S2 [I]) 54 break; 55} 56 if (I = Lmin) 57 Return true; 58 return false; 59}
Code Jun

 

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.