1052 - Bit Compressor

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 Nand 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 
0 0

Sample Output 

Case #1: YES 
Case #2: NOT UNIQUE 
Case #3: NO

Claimer: The data used in this problem is unofficial data prepared by Derek Kisman. So any mistake here does not imply mistake in the offcial judge data. Only Derek Kisman is responsible for the mistakes. Report mistakes to dkisman@acm.org









#include<stdio.h>
char a[44];
int cases,inf,n,m,len,total,ones,solutions;

void go(int i)
{
	int j,k,tmp_len,all_ones;
	if(total>n||ones>m||solutions>1)
		return;
	if(i>=len)
	{
		if(total+ones==n+m)
			solutions++;
		return;
	}
	if(a[i]=='1')
		for(j=i,k=0,all_ones=1;j<len;j++)
		{
			if(k<inf)
				k=k*2+a[j]-'0';
			all_ones&=a[j]-'0';
			if(a[j+1]!='1')
			{
				if(k>2)
				{
					total+=k;
					ones+=k;
					go(j+1);
					total-=k;
					ones-=k;
				}
				if(all_ones&&j-i<2)
				{
					tmp_len=j-i+1;
					total+=tmp_len;
					ones+=tmp_len;
					go(j+1);
					total-=tmp_len;
					ones-=tmp_len;
				}
			}
		}
		else
		{
			total++;
			go(i+1);
			total--;
		}
}

int main()
{
	inf=99999;
	while(1)
	{
		scanf("%d%d",&n,&m);
		if(n==0&&m==0)
			break;
		scanf("%s",a);
		for(len=0;a[len]>' ';len++);
		total=0;
		ones=0;
		solutions=0;
		go(0);
		printf("Case #%d: %s\n",++cases,solutions?solutions-1?"NOT UNIQUE":"YES":"NO");
	}
	return 0;
}


  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值