Trie树模板 POJ1056
IMMEDIATE DECODABILITY
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 12907 | Accepted: 6188 |
Description
An encoding of a set of symbols is said to be immediately decodable 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 that A is a prefix of C)
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 that A is a prefix of C)
Input
Write a program that 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 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 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.
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
思路:很裸的Trie吧,check给定的字符串有无某一子串是另一子串的前缀
1.sort 短的字符串在前 ps.刚开始慌着套模板,忘了排序了
2.套模板 ps.'\0'表示的val[u] = 1,u为下一个节点
#include <iostream> #include <cstdio> #include <cstdlib> #include <cstring> #include <cmath> #include <stack> #include <string> #include <queue> #include <vector> #include <algorithm> #include <ctime> using namespace std; #define EdsonLin #ifdef EdsonLin #define debug(...) fprintf(stderr,__VA_ARGS__) #else #define debug(...) #endif //EdsonLin typedef long long ll; typedef double db; const int inf = 0x3f3f3f; const int MAXN = 1e3; const int MAXNN = 2e6+100; //const int MAXM = 1e6; //const int MAXM = 3e4+100; const int MOD = 1000000007; const db eps = 1e-3; #define PB push_back int readint(){int x;scanf("%d",&x);return x;} inline int code(char x){return x-'0';}; struct Tire{ int ch[MAXN][26]; int val[MAXN]; int sz; bool sg; /* Tire(){ sz = 1; sg = true; sizeof(ch[0],0,sizeof(ch[0][0])); }*/ void Insert(string &s){ int n = s.length(); int u = 0; for(int i=0;i<n;i++){ int c = 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; } void init(){ sz = 1; sg = true; memset(ch[0],0,sizeof(ch[0])); val[0] = 0; } }solver; using namespace std; int main() { string s[MAXN],ts; int mc = 0,x; while(cin>>ts){ solver.init(); x = 0; while(ts.compare("9")!=0){ s[x++] = ts; cin>>ts; } sort(s,s+x); for(int i=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; }
在一个谎言的国度,沉默就是英雄