IMMEDIATE decodability
| Time Limit: 1000MS |
|
Memory Limit: 10000K |
| Total Submissions: 12907 |
|
Accepted: 6188 |
Description
An encoding of a set of symbols are said to being immediately decodable if no code for one symbol are the prefix of a code for Another symbol. We'll assume for this problem-all codes is in binary, that's no codes within a set of codes is the same, that E The ACH code have at least one bit and no more than ten bits, and so each set have at least, codes and no more than eight.
Examples:assume an alphabet as has symbols {A, B, C, D}
The following code is immediately decodable:
a:01 b:10 c:0010 d:0000
It's not:
a:01 b:10 c:010 d:0000 (Note that a was a prefix of C)
Input
Write A program this accepts as input a series of groups of records from standard input. Each record in a group contains a collection of zeroes and ones representing a binary code for a different symbol. Each group was followed by a single separator The record containing a single 9; The separator records is not part of the group. Each group was independent of other groups; The codes in one group was not related to codes with any other group (so is, each group was to be processed independently).
Output
For each group, your program should determine whether the codes of that group is immediately decodable, and should print A single output line giving the group number and stating whether the group are, or is not, immediately decodable.
Sample Input
0110001000009011001000009
Sample Output
Set 1 is immediately decodableset 2 are not immediately decodable
Idea: Very naked trie bar, check the given string has no one substring is the prefix of another substring
1.sort short string in front Ps. Just started to panic set of templates, forget the sort of
2. Set of template ps. ' Val[u ' = 1,u is the next node
#include <iostream>#include<cstdio>#include<cstdlib>#include<cstring>#include<cmath>#include<stack>#include<string>#include<queue>#include<vector>#include<algorithm>#include<ctime>using namespacestd;#defineEdsonlin#ifdef Edsonlin#defineDebug (...) fprintf (stderr,__va_args__)#else#defineDebug (...)#endif //EdsonlintypedefLong LongLl;typedefDoubledb;Const intINF =0x3f3f3f;Const intMAXN =1e3;Const intMaxnn = 2e6+ -;//const int MAXM = 1E6;//const int MAXM = 3e4+100;Const intMOD =1000000007;ConstDB EPS = 1e-3;#definePB push_backintReadint () {intX;SCANF ("%d", &x);returnx;} InlineintCodeCharx) {returnX-'0';};structtire{intch[maxn][ -]; intVAL[MAXN]; intsz; BOOLSG; /*Tire () {sz = 1; SG = TRUE; sizeof (Ch[0],0,sizeof (ch[0][0)); }*/ voidInsert (string&s) { intn =s.length (); intU =0; for(intI=0; i<n;i++){ intc =code (s[i]); if(!Ch[u][c]) {memset (Ch[sz],0,sizeof(Ch[sz])); Val[u]=0; CH[U][C]= sz++; }Else if(val[ch[u][c]]==1) {SG=false; } u=Ch[u][c]; } Val[u]=1; } voidinit () {sz=1; SG=true; memset (ch[0],0,sizeof(ch[0])); val[0] =0; }}solver;using namespacestd;intMain () {stringS[maxn],ts; intMC =0, X; while(cin>>ts) {Solver.init (); X=0; while(Ts.compare ("9")!=0) {s[x++] =ts; CIN>>ts; } sort (S,s+x); for(intI=0; i<x;i++) Solver. Insert (S[i]); if(!solver.sg) {printf ("Set%d is not immediately decodable\n",++MC); Continue; }Else{printf ("Set%d is immediately decodable\n",++MC); } } //cout << "Hello world!" << Endl; return 0;}View Code
Trie Tree Template POJ1056