ACM non-Prefix encoding C ++ implementation

Source: Internet
Author: User

 

Non-Prefix Encoding

Description

There are many ways to encode characters using a binary sequence, such as typical Huffman encoding, if the binary encoding of a character does not contain the prefix of another character, this encoding method is called non-Prefix encoding, huffman encoding is a non-Prefix encoding. For example, if A: 00 B: 10 C: 0100 D: 0101, this encoding is not prefix encoding. If A: 01 B: 10 C: 010 D: 0000, this encoding is prefix encoding.

Write a program to determine whether the encoding is prefix encoding or non-Prefix encoding.

 

Input:

The first line is an integer K, indicating the number of test cases, and the next line is a test case. Each test case contains several strings separated by spaces (the maximum length is not more than 1000 ).

 

Output:

Each row outputs the results of a test case. If Yes is output for non-Prefix encoding, No is output.

 

 

Sample Input

2

01 10 0010 0000

01 10 10 010 0000

 

Sample Output

Yes

No

 

# Include <iostream>

# Include <stdlib. h>

# Define N 1000

Using namespace std;

Char str [N] [N];

Int len [N];

Int cmp (const void * a, const void * B)

{

Return strcmp (char *) a, (char *) B );

}

Bool check (int I)

{

Int k = len [I], j = 0;

While (j <k)

If (str [I] [j ++]! = Str [I + 1] [j ++])

Return false;

Return true;

}

Int main ()

{

Int k;

Cin> k;

While (k --)

{

Scanf ("% s", str [0]);

Int n = 1;

Int flag = false;

While (getchar ()! = '\ N ')

{

Scanf ("% s", str [n ++]);

}

Qsort (str, n, sizeof (str [0]), cmp );

Len [0] = strlen (str [0]);

Int I = 0;

For (I = 1; I <n; I ++)

{

Len [I] = strlen (str [I]);

If (len [I-1]> len [I])

Continue;

Else

{

If (check (I-1 ))

{

Flag = true;

Break;

}

}

}

If (flag)

Cout <"No" <endl;

Else

Cout <"Yes" <endl;

}

Return 0;

}

 


From my dream and my dream ~~~

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.