UVA644_Immediate Decodability/HDU1671_Phone List(字典树)

30 篇文章 0 订阅

Description

  Immediate Decodability 

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 a data file. 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.


The Sample Input describes the examples above.

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



Miguel A. Revilla
2000-01-17

解题报告
这题以前就做过了,不过以前的解法类似暴力。。。

不过当初排序还是冒泡过的~~~

#include<stdio.h>
#include<string.h>
int main()
{
    int i,l[10],k,j,t,p,q=0,m=0,n=0;
    char str[8][11],ch[11];
    while(gets(str[0])!=NULL)
    {
        n++;
        i=1;
        l[0]=strlen(str[0]);
        while(gets(str[i])!=NULL)
        {
            if(str[i][0]=='9')
            break;
            l[i]=strlen(str[i]);
            i++;

        }
        for(j=0;j<i;j++)
            for(k=j+1;k<i;k++)
                if(l[j]>l[k])
                {
                    strcpy(ch,str[j]);
                    strcpy(str[j],str[k]);
                    strcpy(str[k],ch);
                    t=l[j];
                    l[j]=l[k];
                    l[k]=t;
                }
        for(j=0;j<i-1;j++)
        {
            q=0;
            for(k=j+1;k<i;k++)
            {
                q=0;
                for(p=0;p<l[j];p++)
                if(str[j][p]==str[k][p])
                q++;
                if(q==l[j])
                {
                    m++;
                }
            }
        }
        if(m!=0)
        {
            printf("Set %d is not immediately decodable\n",n);
            m=0;
        }
        else printf("Set %d is immediately decodable\n",n);
    }
    return 0;
}

现在学字典数了,先建字典树,不过思路有点奇葩。。。我是先排序在建树,消掉小的字符串在大字符串里出现的标记。。。

话说这判断前缀的思路自己都没明白过来。。。

#include <iostream>
#include <stdio.h>
#include <string.h>
using namespace std;
struct node
{
    int v;
    node *next[2];
};
node *newnode()
{
    node *p=new node;
    p->v=0;
    p->next[0]=NULL;
    p->next[1]=NULL;
    return p;
}
void insertnode(node *root,char *str)
{
    node *p=root;
    int f=0;
    int l=strlen(str);
    for(int i=0;i<l;i++)
    {
        int t=str[i]-'0';
        if(p->next[t]==NULL)
            p->next[t]=newnode();
        p=p->next[t];
        if(p->v==1)
            p->v=0;
    }
    p->v=1;
}
int find(node *root,char *str)
{
    node *p=root;
    int l=strlen(str);
    int i;
    for(i=0;i<l;i++)
    {
        int t=str[i]-'0';
        if(p->next[t]==NULL)
            return 0;
        p=p->next[t];
    }
    if(p->v!=1)
        return 1;
    return 0;
}
int main()
{
    char str[1000][1000],ch[1000];
    int i,n=0,j,k,l[1000],t;
    while(gets(str[0])!=NULL)
    {
        n++;
        i=1;
        l[0]=strlen(str[0]);
        node *root=newnode();
        while(gets(str[i])!=NULL)
        {
            if(str[i][0]=='9')
            break;
            l[i]=strlen(str[i]);
            i++;
        }
        for(j=0;j<i;j++)
            for(k=j+1;k<i;k++)
                if(l[j]>l[k])
                {
                    strcpy(ch,str[j]);
                    strcpy(str[j],str[k]);
                    strcpy(str[k],ch);
                    t=l[j];
                    l[j]=l[k];
                    l[k]=t;
                }
        for(j=0;j<i;j++)
        {
            insertnode(root,str[j]);
        }
        int f=0;
        for(int j=0;j<i;j++)
        {
            if(find(root,str[j]))
                f=1;
        }
        if(f)
            printf("Set %d is not immediately decodable\n",n);
        else printf("Set %d is immediately decodable\n",n);
    }
    return 0;
}

Phone List
Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u

Description

Given a list of phone numbers, determine if it is consistent in the sense that no number is the prefix of another. Let's say the phone catalogue listed these numbers:

  • Emergency 911
  • Alice 97 625 999
  • Bob 91 12 54 26

In this case, it's not possible to call Bob, because the central would direct your call to the emergency line as soon as you had dialled the first three digits of Bob's phone number. So this list would not be consistent.

Input

The first line of input gives a single integer, 1 ≤ t ≤ 40, the number of test cases. Each test case starts with n, the number of phone numbers, on a separate line, 1 ≤ n ≤ 10000. Then follows n lines with one unique phone number on each line. A phone number is a sequence of at most ten digits.

Output

For each test case, output "YES" if the list is consistent, or "NO" otherwise.

Sample Input

2
3
911
97625999
91125426
5
113
12340
123440
12345
98346

Sample Output

NO
YES


解题报告
意思跟上面那题一样,就是时间上比上一题小,需要1000ms跑完。。。
把冒泡改成快排,可以快过去。。。
由于定义的结构体数组,所以快排比较函数得自己敲。。。
struct cnode
{
    char num[200];
    int l;
};

int cmp(const void *a,const void *b)//升序
{
    return (((cnode *)a)->l-((cnode *)b)->l);
}
结果还是超时了。。。

好像静态分配省时间的。。。

好像还得释放内存的。。。静态不用。。。

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
struct cnode
{
    char num[200];
    int l;
};
int cmp(const void *a,const void *b)
{
    return (((cnode *)a)->l-((cnode *)b)->l);
}
struct node
{
    int v;
    node *next[10];
}T[1100000];//静态分配
int t = 0;
node *newnode()
{
    node *p= &T[t++];
    p->v=0;
    for(int i=0;i<10;i++)
    {
        p->next[i]=NULL;
    }
    return p;
}
void insertnode(node *root,char *str)
{
    node *p=root;
    int l=strlen(str);
    for(int i=0;i<l;i++)
    {
        int t=str[i]-'0';
        if(p->next[t]==NULL)
            p->next[t]=newnode();
        p=p->next[t];
        if(p->v==1)
            p->v=0;
    }
    p->v=1;
}
int find(node *root,char *str)
{
    node *p=root;
    int l=strlen(str);
    for(int i=0;i<l;i++)
    {
        int t=str[i]-'0';
        if(p->next[t]==NULL)
            return 0;
        p=p->next[t];
    }
    if(p->v!=1)
        return 1;
    return 0;
}
/*void freenode(node *root)//释放内存
{
    node *p=root;
    for(int i=0;i<10;i++)
        if(p->next[i])freenode(p->next[i]);
    free(p);
}*/
cnode str[20000];
int main()
{
    int t,n,m,tt;
    int i,j,k;
    scanf("%d",&tt);
    while(tt--)
    {
        t = 0 ;
        node *root=newnode();
        scanf("%d%*c",&n);
        for(i=0;i<n;i++)
        {
            scanf("%s",str[i].num);
            str[i].l=strlen(str[i].num);
        }
        qsort(str,n,sizeof(str[0]),cmp);
        for(i=0;i<n;i++)
            insertnode(root,str[i].num);
        int f=0;
        for(i=0;i<n;i++)
        {
            if(find(root,str[i].num))
                f=1;
        }
        if(f)printf("NO\n");
        else printf("YES\n");
        //freenode(root);
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值