1. 程式人生 > >POJ 1056 IMMEDIATE DECODABILITY

POJ 1056 IMMEDIATE DECODABILITY

IMMEDIATE DECODABILITY
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 11585 Accepted: 5517

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
 

題意:

        判斷二進位制碼是否能唯一識別,也就是一個二進位制碼不是另一個二進位制碼的字首

題解:

      最近在學字典樹,搜的字典樹的簡單題做的,其實還可以排序做,先說字典樹吧,在樹的節點上做個標記,標記作用為是否是為一個二進位制碼的結尾,於是就有兩種情況,一種為在此碼是前面出現的二進位制碼的字首,這時只需要判斷此碼的結尾字元在樹上存在,還有就是前面出現的二進位制碼是此碼的字首,這時只要判斷標記就行了,注意初始化。

      排序的方法,對二進位制碼按字典序排序,一個二進位制碼的字首肯定在其前一個位置出現,具體原因,自己想下就很容易理解了O(∩_∩)O~~

字典樹程式碼:

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
using namespace std;
const int maxn=110000;
struct node
{
    int sign;
    int next[2];
} a[maxn];
int n=0;
bool insert(char *s)
{
    int len,temp;
    len=strlen(s);
    int p=0;
    for(int i=0; i<len; i++)
    {
        temp=s[i]-'0';
        if(a[p].next[temp]!=-1)
        {
            p=a[p].next[temp];
            if(i==len-1)
                return 1;
            if(a[p].sign==1)
                return 1;
        }
        else
        {
            a[p].next[temp]=++n;
            p=n;
        }
        if(i==len-1)
            a[p].sign=1;
    }
    return 0;
}
int main()
{
    char s[15];
    int count=1;
    while(~scanf("%s",s))
    {
        n=0;
        if(s[0]=='9')
            break;
        memset(a,-1,sizeof(a));
        int ans=0;
        if(insert(s))
        {
            ans=1;
        }
        while(scanf("%s",s)&&s[0]!='9')
        {
            if(insert(s))
            {
                ans=1;
            }
        }
        if(ans)
            printf("Set %d is not immediately decodable\n",count++);
        else
            printf("Set %d is immediately decodable\n",count++);
    }
}


排序程式碼:

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;
struct node
{
    char s[15];
} a[11000];
bool operator < (node const& x,node const& y)
{
    return strcmp(x.s,y.s)<0;
}
int main()
{
    int n=0;
    int count=1;
    while(~scanf("%s",a[n].s)&&a[n].s[0]!='9')
    {
        n++;
        while(scanf("%s",a[n].s)&&a[n].s[0]!='9')
        {
            n++;
        }
        sort(a,a+n);
        int ans=0;
        for(int i=1; i<n; i++)
        {
            int len1=strlen(a[i-1].s);
            int len2=strlen(a[i].s);
            if(len1<=len2)
            {
                int j;
                for(j=0; j<len1; j++)
                    if(a[i-1].s[j]!=a[i].s[j])
                        break;
                if(j==len1)
                    ans=1;
            }
        }
        if(ans)
            printf("Set %d is not immediately decodable\n",count++);
        else
            printf("Set %d is immediately decodable\n",count++);
        n=0;
    }
    return 0;
}