zoj 1808 || poj 1056 IMMEDIATE DECODABILITY

 

给你几个二进制代码,如果有其中一个代码是另一个的前缀,输出is not immediately decodable,反之,输出可编码。

 

Trie水过。开的空间应该最小是8*10。

 

#include <cstdio> #include <cstdlib> #include <iostream> #include <string.h> #define MAX 100 using namespace std; typedef struct Trie{ int flag; Trie *one,*zero; }Trie; Trie *head,node[MAX]; int cou; void init() { cou = 0; memset(node,'/0',sizeof(node)); head = &node[cou++]; } int Add(char *str) { Trie *p = head; int len = strlen(str),i; for(i=0; i<len; i++) { if( p != NULL && p->flag == 1 ) return 0; p->flag = -1; if( str[i] == '0' ) { if( p->zero == NULL ) p->zero = &node[cou++]; p = p->zero; } else { if( p->one == NULL ) p->one = &node[cou++]; p = p->one; } } if( p->flag != 0 ) return 0; p->flag = 1; return 1; } int main() { char str[20]; int len,ans,flag,ind = 1; while( scanf("%s",str) && str[0] != '9' ) { init(); flag = 0; ans = Add(str); if( ans == 0 ) flag = 1; while( scanf("%s",str) && str[0] != '9' ) { if( !flag ) { ans = Add(str); if( ans == 0 ) flag = 1; } } if( flag ) printf("Set %d is not immediately decodable/n",ind++); else printf("Set %d is immediately decodable/n",ind++); } return 0; }

你可能感兴趣的:(zoj 1808 || poj 1056 IMMEDIATE DECODABILITY)