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