UVA1052 Bit Compressor(DFS)

16 篇文章 0 订阅

题面

The aim of data compression is to reduce redundancy in stored or communicated data. This increases effective data density and speeds up data transfer rates. One possible method to compress any binary message is the following:
       Replace any maximal sequence of n 1’s with the binary version of n whenever it shortens the length of the message.
For example, the compressed form of the data 11111111001001111111111111110011 becomes 10000010011110011. The original data is 32 bits long while the compressed data is only 17 bits long. The drawback of this method is that sometimes the decompression process yields more than one result for the original message, making it impossible to obtain the original message. Write a program that determines if the original message can be obtained from the compressed data when the length of the original message (L), the number of 1’s in the original message (N) and the compressed data are given. The original message will be no longer than 16 Kbytes and the compressed data will be no longer than 40 bits.
Input
The input file contains several test cases. Each test case has two lines. The first line contains L and N and the second line contains the compressed data. The last case is followed by a line containing two zeroes.
Output
For each test case, output a line containing the case number (starting with 1) and a message ‘YES’, ‘NO’ or ‘NOT UNIQUE’. ‘YES’ means that the original message can be obtained. ‘NO’ means that the compressed data has been corrupted and the original message cannot be obtained. ‘NOT UNIQUE’ means that more than one message could have been the original message. Follow the format shown in the sample output.
Sample Input
32 26

10000010011110011

9 7

1010101

14 14

111111

00
Sample Output
Case #1: YES

Case #2: NOT UNIQUE

Case #3: NO

题目链接

UVA_1052

题目简述

the binary version of n 一定以1开头,开头一定不会在最后一个字符上,开头的前一个字符一定不可以是‘1’。

the binary version of n的结尾的下一个字符不可以是‘1’,如果是‘1’,则说明原来假定的maximal sequence of n 1’s 可以更长。

两个情况需要特殊考虑:

①如10100这样以10开头的位置,可以至少可能由1111100(不跳过开头10)或者101111(10位置被跳过)两种情况得到

②110……至少可能由1110……或110……两种情况得到。

any maximal sequence of n 1’s 所有111……的串都要被替换

1bytes=8bits  原串最长16*1024*8=131072位,不会超过int。

新串最多40位。

程序分析

l为新串的长度

length为i位置之前,原串积累的长度

num_1为i位置之前,原串1积累的个数

i为the binary version of n开始的位置,j为the binary version of n结束的位置。

程序

#include<stdio.h>
#include<string.h>
#define maxn 50
int L,l,N;
char data[maxn];
int answer;

void dfs(int i,int length,int num_1)
{
    if(i==l)
    {
        dfs(i+1,length+1,num_1+data[i]-'0');
        return;
    }
    if(i>l)
    {
        if(length==L&&num_1==N)
            answer++;
        return;
    }
    if(length>L||num_1>N||answer>=2)
        return;
    if(data[i]=='0')
    {
        dfs(i+1,length+1,num_1);
        return;
    }
    /**data[i]=='1'**/
    if(data[i-1]=='1')
        return;
    long long temp=0;
    if(data[i+1]=='0')
        dfs(i+1,length+1,num_1+1);
    if(data[i+1]=='1'&&data[i+2]!='1')
        dfs(i+2,length+2,num_1+2);
    for(int j=i;j<=l;j++)
    {
        temp*=2;
        temp+=data[j]-'0';
        if(temp+num_1>N||temp+length>L)
            break;
        if(temp>j-i+1&&data[j+1]!='1')
            dfs(j+1,temp+length,num_1+temp);
    }
}


int main()
{
    int e=0;
    while(scanf("%d",&L)&&L)
    {
        scanf("%d",&N);
        scanf("%s",data+1);
        l=strlen(data+1);
        answer=0;
        dfs(1,0,0);
        printf("Case #%d: ",++e);
        if(answer==0)
            printf("NO\n");
        else if(answer==1)
            printf("YES\n");
        else
            printf("NOT UNIQUE\n");
    }
    return 0;
}

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值