UVA 213 Message Decoding 【模拟】

 

题目链接:

  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的情况用位运算比较方便。

 

  1 //hdu5490
  2 //by coolxxx
  3 /*
  4 #include<iostream>
  5 #include<algorithm>
  6 #include<string>
  7 #include<iomanip>
  8 #include<map>
  9 #include<stack>
 10 #include<queue>
 11 #include<set>
 12 #include<bitset>
 13 #include<memory.h>
 14 #include<time.h>
 15 #include<stdio.h>
 16 #include<stdlib.h>
 17 #include<string.h>
 18 //#include<stdbool.h>
 19 #include<math.h>
 20 #define min(a,b) ((a)<(b)?(a):(b))
 21 #define max(a,b) ((a)>(b)?(a):(b))
 22 #define swap(a,b) ((a)^=(b),(b)^=(a),(a)^=(b))
 23 */
 24 #include<bits/stdc++.h>
 25 #pragma comment(linker,"/STACK:1024000000,1024000000")
 26 #define abs(a) ((a)>0?(a):(-(a)))
 27 #define lowbit(a) (a&(-a))
 28 #define sqr(a) ((a)*(a))
 29 #define mem(a,b) memset(a,b,sizeof(a))
 30 #define eps (1e-8)
 31 #define J 10000
 32 #define mod 100000007
 33 #define MAX 0x7f7f7f7f
 34 #define PI 3.14159265358979323
 35 #define N 204
 36 
 37 using namespace std;
 38 typedef long long LL;
 39 
 40 char h[250];
 41 const int aa[]={0,1,3,7,15,31,63,127};
 42 const int bb[]={0,0,1,4,11,26,57,120,247};
 43 char Read()
 44 {
 45     char ch;
 46     while((ch=getchar())=='\n' || ch=='\r');
 47     return ch;
 48 }
 49 int read()
 50 {
 51     mem(h,0);
 52     int i,j,num=0;
 53     char ch;
 54     while(1)
 55     {
 56         ch=Read();
 57         if(ch==EOF)return 0;
 58         h[0]=ch;
 59         for(i=2;;i++)
 60         {
 61             for(j=0;j<aa[i];j++)
 62             {
 63                 if((ch=getchar())=='\n' || ch=='\r')return 1;
 64                 h[++num]=ch;
 65             }
 66         }
 67     }
 68     return 1;
 69 }
 70 int getint(int len)
 71 {
 72     int tot=0;
 73     bool mark=0;
 74     char ch;
 75     while(len--)
 76     {
 77         ch=Read();
 78         tot=tot*2+ch-'0';
 79     }
 80     return tot;
 81 }
 82 
 83 int main()
 84 {
 85     #ifndef ONLINE_JUDGE
 86     freopen("1.txt","r",stdin);
 87     //freopen("2.txt","w",stdout);
 88     #endif
 89     int i,j,k;
 90     int x,y,z;
 91     while(read())
 92     {
 93         while(1)
 94         {
 95             int len=getint(3);
 96             if(!len)break;
 97             while(1)
 98             {
 99                 x=getint(len);
100                 if(x==(1<<len)-1)break;
101                 putchar(h[bb[len]+x]);
102             }
103         }
104         puts("");
105     }
106     return 0;
107 }
108 /*
109 //
110 
111 //
112 */
View Code

 

posted @ 2017-02-21 14:27  Cool639zhu  阅读(516)  评论(0编辑  收藏  举报