【原】 POJ 1056 IMMEDIATE DECODABILITY Trie树查找前缀 解题报告

http://poj.org/problem?id=1056

方法:Trie树

1、由于编码只由0、1组合,因此Trie的数组分支为2个,即son[2]
2、估计节点数目。已知一个集合中最多有8个序列,每个序列最多有10位。因此最坏情况下共有节点80个。

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

   1: #include <iostream>
   2: #include <stdio.h>
   3:  
   4: using namespace std ;
   5:  
   6:  
   7: struct Node
   8: {
   9:     Node():isStr(false){ memset(son, NULL, sizeof(son) ); }
  10:     bool isStr ;
  11:     Node* son[2] ; //数字0、1
  12: };
  13:  
  14: Node buffer[100] ;
  15: //int bufnum = 1 ;
  16:  
  17: class Trie
  18: {
  19: private:
  20:     Node root ;
  21:     int bufnum ;
  22: public:
  23:     Trie():root(buffer[0]),bufnum(1){}
  24:     bool InsertAndCheck( char* s ) ;
  25:     //void Clear(){ bufnum=1 ; }
  26: };
  27:  
  28: bool Trie::InsertAndCheck(char *s)
  29: {
  30:     Node *p = &root ;
  31:     int len = strlen(s) ;
  32:     int digit, i ;
  33:  
  34:     for( i=0 ; i<len ; ++i )
  35:     {
  36:         digit = s[i]-'0' ;
  37:         if( i==len-1 && p->son[digit]!=NULL )
  38:             return false ;
  39:  
  40:         if( p->son[digit] == NULL )
  41:         {
  42:             p->son[digit] = &buffer[bufnum] ;
  43:             buffer[bufnum].isStr = false ;
  44:             memset( buffer[bufnum].son, NULL, sizeof(buffer[bufnum].son) ) ;
  45:             ++bufnum ;
  46:         }
  47:         else
  48:         {
  49:             if( p->son[digit]->isStr == true )
  50:                 return false ;
  51:         }
  52:         p = p->son[digit] ;
  53:     }
  54:  
  55:     p->isStr = true ;
  56:     return true ;
  57: }
  58:  
  59: void run1056()
  60: {
  61:     int num, n, i ;
  62:     char s[11] ;
  63:     bool flag ;
  64:  
  65:     num = 0 ;
  66:     while( true )
  67:     {
  68:         ++num ;
  69:         if( scanf( "%s", s )==EOF )
  70:             return ;
  71:  
  72:         Trie trie ;
  73:         flag = trie.InsertAndCheck( s ) ;
  74:         while( scanf( "%s", s ) && strcmp(s,"9")!=0 )
  75:         {
  76:             flag = trie.InsertAndCheck( s ) ;
  77:             if( !flag )
  78:                 break ;
  79:         }
  80:  
  81:         if( strcmp(s,"9") != 0 )
  82:         {
  83:             while( scanf( "%s", s ) && strcmp(s,"9")!=0 )
  84:                 NULL ;
  85:         }
  86:  
  87:         if( flag )
  88:             printf( "Set %d is immediately decodable\n", num ) ;
  89:         else
  90:             printf( "Set %d is not immediately decodable\n", num ) ;
  91:     }
  92: }

如果您满意我的博客,请点击“订阅Allen Sun的技术博客”即可订阅,谢谢:)

原创文章属于Allen Sun
欢迎转载,但请注明文章作者Allen Sun和链接
原文地址:https://www.cnblogs.com/allensun/p/1869402.html