POJ 1056 IMMEDIATE DECODABILITY 字典树

来源:互联网 发布:淘宝p图神器软件哪个好 编辑:程序博客网 时间:2024/06/15 19:08
IMMEDIATE DECODABILITY
Time Limit: 1000MS Memory Limit: 10000KTotal Submissions: 12331 Accepted: 5862

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

[Submit]   [Go Back]   [Status]   [Discuss]

字典树是多叉树用于储存字典又称单词查找树Trie,是一种树形结构,是一种哈希树的变种。典型应用是用于统计,排序和保存大量的字符串(但不仅限于字符串),所以经常被搜索引擎系统用于文本词频统计。它的优点是:利用字符串的公共前缀来节约存储空间,最大限度地减少无谓的字符串比较,查询效率比哈希表高。字典树与字典很相似,当你要查一个单词是不是在字典树中,首先看单词的第一个字母是不是在字典的第一层,如果不在,说明字典树里没有该单词,如果在就在该字母的孩子节点里找是不是有单词的第二个字母,没有说明没有该单词,有的话用同样的方法继续查找.字典树不仅可以用来储存字母,也可以储存数字等其它数据。

定义中的next是表示每层有多少种类的数,如果只是小写字母,则26即可,若改为大小写字母,则是52,若再加上数字,则是62了,这里根据题意来确定。

v可以表示一个字典树到此有多少相同前缀的数目,这里根据需要应当学会自由变化。

本题求的是某串是否是其他串的前缀,输入比较麻烦。

ACcode:

#pragma warning(disable:4786)//使命名长度不受限制#pragma comment(linker, "/STACK:102400000,102400000")//手工开栈#include <map>#include <set>#include <queue>#include <cmath>#include <stack>#include <cctype>#include <cstdio>#include <cstring>#include <stdlib.h>#include <iostream>#include <algorithm>#define rd(x) scanf("%d",&x)#define rd2(x,y) scanf("%d%d",&x,&y)#define rds(x) scanf("%s",&x)#define rdc(x) scanf("%c",&x)#define ll long long int#define maxn 1005#define mod 1000000007#define INF 0x3f3f3f3f //int 最大值#define FOR(i,f_start,f_end) for(int i=f_start;i<=f_end;++i)#define MT(x,i) memset(x,i,sizeof(x))#define PI  acos(-1.0)#define E  exp(1)#define MAX 2using namespace std;/*next 表示每层有多少种类的数v表示一个字典树到此有多少相同的前缀数目*/struct Trie{    Trie *next[MAX];    int v;///根据需求变化};Trie  *root;char str[maxn][maxn];void createTrie(char *str){    int len=strlen(str);    Trie *p=root,*q;    FOR(i,0,len-1){        int id=str[i]-'0';        if(p->next[id]==NULL){            q=(Trie*)malloc(sizeof(Trie));            q->v=1;///初始 v==1            FOR(j,0,MAX-1)                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(i,0,len-1){        int id=str[i]-'0';        p=p->next[id];        if(p==NULL)///若为空集,表示不存在以此为前缀的串            return 1;        if(p->v==-1)///字符集中已有串是此串的前缀            return 2;    }    return 0;///此串是字符集中某串的前缀}void dealTrie(Trie *T){    if(T==NULL)        return ;    FOR(i,0,MAX-1)        if(T->next[i]!=NULL)            dealTrie(T->next[i]);    free(T);}int main(){    int cnt=1;    root=(Trie *) malloc (sizeof(Trie));    FOR(i,0,MAX-1){        root->next[i]=NULL;        root->v=1;    }   int flag=true;;    int num=0;    while(scanf("%s",&str[++num])!=EOF){            if(str[num][0]=='9'){                FOR(i,1,num-1){                  flag=findTrie(str[i]);                  if(!flag)break;                }                if(flag)printf("Set %d is immediately decodable\n",cnt++);                else printf("Set %d is not immediately decodable\n",cnt++);                dealTrie(root);                root=(Trie *) malloc (sizeof(Trie));                FOR(i,0,MAX-1){                    root->next[i]=NULL;                    root->v=1;}                flag=true;                num=0;                continue;            }            else                createTrie(str[num]);    }    return 0;}/*0110001000009011001000009*/


0 0