644-immediate decodability
Time limit:3.000 seconds
Http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=96&page=show_problem &problem=585
An encoding of a set of "symbols is said" to "immediately decodable if no code for one symbol" the prefix of a code for Another symbol. We'll assume for this problem so all codes are in binary, which is no two codes within a set of codes are the same, that E Ach code has at least one bit and no more than ten bits, and which each set has at least two codes and no more than eight.
Examples:assume A 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 isn't:
a:01 b:10 c:010 d:0000 (note which is a prefix of C)
Input
Write a program so accepts as input a series of groups of records from a data file. Each record in a group contains a collection the zeroes and ones representing a binary code for a different symbol. Each group are followed by a single separator record containing a single 9; The separator records are is not part of the group. Each group is independent of the other groups; The codes in one group are does not related to codes into any other group (which is, each group are 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
Complete code:
/*0.035s*/#include <cstdio> #include <cstring> char s[1000][100];
int n;
inline bool Check () {for (int i = 0; i < n-1. i++) for (int j = i + 1; j < N; j +) {
BOOL flag = FALSE;
int L1 = strlen (S[i]);
int L2 = strlen (S[j]);
for (int k = 0; k < L1 && K < L2 k++)///faster than STRSTR () if (s[i][k)!= s[j][k])
{flag = true;
Break
} if (!flag) return false;
return true;
int main (void) {int cas = 0;
while (gets (S[0])) {for (n = 1;; n++) {gets (s[n));
if (s[n][0] = = ' 9 ') break;
} if (check ()) printf ("Set%d is immediately decodable\n", ++cas); else printf ("Set%d is not immediately decodable\n", + +CAS); }
}
See more highlights of this column: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/sjjg/