Wednesday, February 9, 2011

644 - Immediate Decodability


#include<iostream>
#include<algorithm>
#include<cstring>
using namespace std;

struct decode
{
    char code[12];
};
int isPrefix(char[],char[]);

bool compare(decode,decode);

int main()
{
    int i,j,n=0,t=1,d;
    struct decode dec[10];
    //freopen("in.txt","r",stdin);
    while(scanf("%s",dec[n].code)&&strcmp(dec[n].code,"9"))n++;

    while(n)
    {
        sort(dec,dec+n,compare);

        //Deciding decodability
        d=1;
        for(i=0;i<n;i++)
            for(j=i+1;j<n;j++)
                if(isPrefix(dec[i].code,dec[j].code))
                    d=0,j=i=n;
       
        //Print output
        if(d)
            printf("Set %d is immediately decodable\n",t);
        else
            printf("Set %d is not immediately decodable\n",t);

        //Read next set
        n=0;
        while(scanf("%s",dec[n].code)==1&&strcmp(dec[n].code,"9"))
            n++;
        t++;
    }
    return 0;
}

bool compare(decode s1,decode s2)
{
    if(strlen(s1.code)>strlen(s2.code))
        return false;
    return true;
}

int isPrefix(char pre[],char str[])
{
    int i;
    for(i=0;pre[i];i++)
        if(pre[i]!=str[i])
            return 0;
    return 1;
}

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.