標籤:hdu poj 字典樹 zoj
題目連結
poj 1056 :http://poj.org/problem?id=1056
hdu 1305 :http://acm.hdu.edu.cn/showproblem.php?pid=1305
zoj 1808 :http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=808
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
0110001000009011001000009
Sample Output
Set 1 is immediately decodableSet 2 is not immediately decodable
Source
Pacific Northwest 1998
題意:找一堆單詞中是否存在一個單詞是另一個單詞的首碼。
代碼如下:
#include <cstdio>#include <cstring>#include <malloc.h>#include <iostream>using namespace std;#define MAXN 2typedef struct Trie{ int v;//根據需要變化 Trie *next[MAXN]; //next是表示每層有多少種類的數,如果只是小寫字母,則26即可, //若改為大小寫字母,則是52,若再加上數字,則是62了} Trie;Trie* root;void createTrie(char *str){ int len = strlen(str); Trie *p = root, *q; for(int i = 0; i < len; i++) { int id = str[i]-'0'; if(p->next[id] == NULL) { q = (Trie *)malloc(sizeof(Trie)); q->v = 1;//初始v==1 for(int j = 0; j < MAXN; j++) q->next[j] = NULL; p->next[id] = q; p = p->next[id]; } else { // p->next[id]->v++; p = p->next[id]; } } p->v = -1;//若為結尾,則將v改成-1表示}int findTrie(char *str){ int len = strlen(str); Trie *p = root; for(int i = 0; i < len; i++) { int id = str[i]-'0'; p = p->next[id]; if(i < len-1)//注意 { if(p->v==-1) { return -1; } } /* if(p == NULL) //若為空白集,表示不存以此為首碼的串 return 0; if(p->v == -1) //字元集中已有串是此串的首碼 return -1;*/ } return 1;//自身}int main(){ char str[15][20]; int c = 1; while(scanf("%s",str[0])!=EOF) { root = (Trie *)malloc(sizeof(Trie)); for(int i = 0; i < MAXN; i++) root->next[i] = NULL; createTrie(str[0]); int k = 1; while(scanf("%s", str[k])&&str[k][0]!='9') { createTrie(str[k++]); } int i; for(i = 0; i < k; i++) { int flag = findTrie(str[i]); if(flag == -1)//有相同首碼 { printf("Set %d is not immediately decodable\n",c++); break; } } if(i>=k)//沒有相同首碼 { printf("Set %d is immediately decodable\n",c++); } } return 0;}
poj1056 & hdu1305 & zoj1808 Immediate Decodability(字典樹變形)