题目链接:
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;
}
/*
//
//
*/