UVA 213 Message Decoding 【模拟】

64 篇文章 3 订阅
1 篇文章 0 订阅

题目链接:

  https://cn.vjudge.net/problem/UVA-213

  https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=149

题目大意:

  给一种编码方式,0,00,01,10,000,...,依次对应一个字符;

  一开始需要读入3位长的2进制数来表示接下来的解码长度L,接下来读L位,解码,直到读入L个1停止。

  接下来继续读解码长度L,重复上述步骤直到L=0,结束。

  输入可能会有换行,要求能够处理换行。

题目思路:

  【模拟】

  主要就是处理换行的问题。

  首先肯定需要把2进制转换成10进制存储,将对应位置的字符存好(可以用二维也可以一维)

  接着就是模拟这个读入字符的过程,解码即可。注意判断全1的情况用位运算比较方便。




//hdu5490
//by coolxxx
/*
#include<iostream>
#include<algorithm>
#include<string>
#include<iomanip>
#include<map>
#include<stack>
#include<queue>
#include<set>
#include<bitset>
#include<memory.h>
#include<time.h>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
//#include<stdbool.h>
#include<math.h>
#define min(a,b) ((a)<(b)?(a):(b))
#define max(a,b) ((a)>(b)?(a):(b))
#define swap(a,b) ((a)^=(b),(b)^=(a),(a)^=(b))
*/
#include<bits/stdc++.h>
#pragma comment(linker,"/STACK:1024000000,1024000000")
#define abs(a) ((a)>0?(a):(-(a)))
#define lowbit(a) (a&(-a))
#define sqr(a) ((a)*(a))
#define mem(a,b) memset(a,b,sizeof(a))
#define eps (1e-8)
#define J 10000
#define mod 100000007
#define MAX 0x7f7f7f7f
#define PI 3.14159265358979323
#define N 204

using namespace std;
typedef long long LL;

char h[250];
const int aa[]={0,1,3,7,15,31,63,127};
const int bb[]={0,0,1,4,11,26,57,120,247};
char Read()
{
	char ch;
	while((ch=getchar())=='\n' || ch=='\r');
	return ch;
}
int read()
{
	mem(h,0);
	int i,j,num=0;
	char ch;
	while(1)
	{
		ch=Read();
		if(ch==EOF)return 0;
		h[0]=ch;
		for(i=2;;i++)
		{
			for(j=0;j<aa[i];j++)
			{
				if((ch=getchar())=='\n' || ch=='\r')return 1;
				h[++num]=ch;
			}
		}
	}
	return 1;
}
int getint(int len)
{
	int tot=0;
	bool mark=0;
	char ch;
	while(len--)
	{
		ch=Read();
		tot=tot*2+ch-'0';
	}
	return tot;
}

int main()
{
	#ifndef ONLINE_JUDGE
	freopen("1.txt","r",stdin);
	//freopen("2.txt","w",stdout);
	#endif
	int i,j,k;
	int x,y,z;
	while(read())
	{
		while(1)
		{
			int len=getint(3);
			if(!len)break;
			while(1)
			{
				x=getint(len);
				if(x==(1<<len)-1)break;
				putchar(h[bb[len]+x]);
			}
		}
		puts("");
	}
	return 0;
}
/*
//

//
*/


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值