【例题 4-4 uva 213】Message Decoding

【链接】 我是链接,点我呀:)
【题意】


在这里输入题意

【题解】


输入的二进制长度最长为7
所以得开个sta[7][2^7]的样子才存的下所有的字符的。。
定义这么一个数组当字典。
然后一个字符一个字符地读。。组合成题目中的参数。
然后根据读入的每个长度为len的二进制,在字典中找到相应的字符就ok啦。

【代码】

#include <bits/stdc++.h>
using namespace std;

string s;
char dic[100][100];

int readbinary(){
    char key = cin.get();
    while (key!='0' && key!='1') key=cin.get();
    return (int)(key-'0');
}

int main()
{
    //freopen("/home/ccy/rush.txt","r",stdin);
    ios::sync_with_stdio(0),cin.tie(0);
    while (getline(cin,s)){
        int cur = 2,now = 0,curlen = 1;
        for (int i = 0;i < (int)s.size();i++){
            if (now==cur-1) {
                now = 0,cur*=2;curlen++;
            }
            dic[curlen][now] = s[i];
            now++;
        }
        int len = 1;
        while (len!=0){
            len = 0;
            for (int i = 0;i < 3;i++) {
                int ju = readbinary();
                len = len*2 + ju;
            }
            int over = 0;
            while (1){
                over = 1;
                int temp = 0;
                for (int i = 0;i < len;i++){
                    int ju = readbinary();
                    if (ju==0) over = 0;
                    temp = temp*2+ju;
                }
                if (over) break;
                cout<<dic[len][temp];
            }
        }
        cout<<endl;
        cin.get();
    }

    return 0;
}

猜你喜欢

转载自www.cnblogs.com/AWCXV/p/9834934.html