UVa 213 - Message Decoding

将字符用01串进行编码,并把下面的01串转换成对应字符串

打了一遍书上的样例程序..

读取单个字符的函数来忽略换行符还是很神奇的

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 using namespace std;
 5 char code[8][1<<8];
 6 char readchar(){
 7     while(1){
 8         char ch=getchar();
 9         if(ch!='\n'&&ch!='\r') return ch;
10     }
11 }
12 int readint(int c){
13     int v=0;
14     while(c--) v=v*2+readchar()-'0';
15     return v; 
16 }
17 int readcode(){
18     memset(code,0,sizeof(code));
19     code[1][0]=readchar();
20     for(int len=2;len<=7;len++){
21         for(int i=0;i<(1<<len)-1;i++){
22             char ch=getchar();
23             if(ch==EOF) return 0;
24             if(ch=='\n'||ch=='\r') return 1;
25             code[len][i]=ch;
26         }
27     }
28     return 1;
29 }
30 int main()
31 {
32     while(readcode()){
33         while(1){
34             int len=readint(3);
35             if(!len) break;
36             while(1){
37                 int v=readint(len);
38                 if(v==(1<<len)-1) break;
39                 putchar(code[len][v]);
40             }
41         }
42         puts("");
43     }
44 }
45 /*
46 TNM AEIOU
47 0010101100011
48 1010001001110110011
49 11000
50 $#**\
51 010000010110110001110010100
52 */

 

posted @ 2016-05-21 17:10  nicetomeetu  阅读(187)  评论(0编辑  收藏  举报