Immediate Decodability(字典树)

早过忘川 提交于 2020-03-05 00:19:13

Immediate Decodability

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 2248    Accepted Submission(s): 1168

点我

Problem 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)
 

 

Input
Write a program that accepts as input a series of groups of records from 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
题目大意:判断是否存在一串编码是另一串编码的前缀
 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 using namespace std;
 5 #define max 2
 6 typedef struct TrieNode
 7 {
 8     int ncount;
 9     struct TrieNode *next[max];
10 }TrieNode;
11 TrieNode* createTrieNode()
12 {
13     TrieNode* temp=new TrieNode;
14     temp->ncount=0;
15     for(int i=0;i<max;i++)
16         temp->next[i]=NULL;
17     return temp;
18 }
19 void insertTrie(TrieNode* proot,char* str)
20 {
21     TrieNode *temp=proot;
22     for(int i=0;str[i];i++)
23     {
24         int t=str[i]-'0';
25         if(temp->next[t]==NULL)
26             temp->next[t]=createTrieNode();
27         temp=temp->next[t];
28         temp->ncount++;
29     }
30 }
31 int searchTrie(TrieNode* p,char *str)
32 {
33     for(int i=0;str[i];i++)
34     {
35         int t=str[i]-'0';
36         p=p->next[t];
37         if(!p)
38             return 0;
39     }
40     return p->ncount;
41 }
42 int main()
43 {
44     char a[99][100];
45     int i=0,k=1;
46     TrieNode* root=createTrieNode();
47     while(gets(a[i]))
48     {
49         int count=0;
50         while(a[i][0]!='9')
51         {
52             insertTrie(root,a[i]);
53             i++;
54             gets(a[i]);
55         }
56         count=i;
57         for(i=0;i<count;i++)
58         {
59             if(searchTrie(root,a[i])!=1)
60             {
61                 cout<<"Set "<<k++<<" is not immediately decodable"<<endl;
62                 break;
63             }
64         }
65         if(i==count)
66             cout<<"Set "<<k++<<" is immediately decodable"<<endl;
67         i=0;
68     }
69 }

 

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!