UVa213 - Message Decoding 题解

题目

题目链接

UVa213 - Message Decoding

题目大意

太长了 不想翻译。

大概就是告诉你一个编码规则的规则,然后输入编码规则,再输入编码后的数据,再输入读取编码的规则,要你输出解码后的数据。

样例输入

TNM AEIOU
0010101100011
1010001001110110011
11000
$#**\
0100000101101100011100101000

样例输出

TAN ME
##*\$

题解

这编码规则的规则明显就是符合2进制的,所以能用二元组(len, value)来保存一个编码,其中len是编码长度,value是编码对应的10进制的值。

着急赶回宿舍,所以火速写完题解。

Then show the code.

#include <stdio.h>
#include <string.h>

char codes[8][1<<8];

//读取一个非回车的字符
char readchar(){
    char ch;
    while(1){
        ch = getchar();
        if(ch == '\n' || ch == '\r') continue;
        return ch;
    }
}
//读取v位的2进制 并转化为10进制
int readint(int v){
    int a=0;
    for(int i =0; i<v; i++){
        a = a*2 + (readchar() -'0');
    }
    return a;
}
//读取编码头
int readcodes(){
    //第2组以及之后的数据如果直接getchar()会读到一个回车 所以要readchar()
    codes[1][0] = readchar();
    char ch;
    for(int len=2; len<=7; len++){
        for(int i=0; i<(1<<len)-1; i++){
            ch = getchar();
            if(ch == '\n' || ch == '\r') return 1;
            if(ch == EOF) return 0;
            codes[len][i] = ch;
        }
    }
    return 1;
}

int main(){
    while(readcodes()){
        while(1){
            int len = readint(3);
            if(len == 0) break;
            while(1){
                int v = readint(len);
                if(v == (1<<len)-1) break;
                putchar(codes[len][v]);
            }
        }
        putchar('\n');
    }
    return 0;
}
posted @ 2020-11-14 20:42  1v7w  阅读(97)  评论(0编辑  收藏  举报