UVa 213 Message Decoding (信息编码)

该题目作为放假回归正轨的第一道正式做的题目,被卡了好久,唉,怪我有颗太浪的心

解题思路:

1、把编码头用二维字符数组存储起来,a[x][y]存储对应的字符,x表示编码的长度,y表示编码的大小,

存储时注意y<2^x - 1的

2、由于编码文本可能是由多行组成,所以需要判断和跳过对换行符的处理,为方便处理,将编码文本视作字符处理

#include<iostream>
#include<CString>
#include<cmath>
#include<stdlib.h>
using namespace std;
char a[8][64];
int get(int x)
{//算出长度为x的编码的值 
    char a;
    int s = 0;
    while (a = getchar())
    {
        if (a != '
' && a != '
')
        {
            x--;
            s += (int)(pow(2, x) * (a - '0'));
            if (x == 0) break;
        }
    }
    return s;
}
int main()
{
    memset(a, 0, sizeof(a));
    for (int i = 1; i < 8; i++)
    {//将编码头存储到二维字符数组中 
        int flags = 0;
        for (int j = 0; j < pow(2, i) - 1; j++)
        {
            char b = getchar();
            if (b == '
' || b == '
')
            {
                flags = 1;
                break;
            }
            else
                a[i][j] = b;

            }
            if (flags == 1)
                break;
    }
    int L, S; 
    while (true)
    {
        L = get(3);//小节中每个编码的长度 
        if (L == 0) break;//000代表文本结束 
        while (true)
        {//对小节中的每个编码进行解码 
            S = get(L);
            if (S == pow(2, L) - 1)
                break;
            else
                cout << a[L][S];
        }
    }
    cout << endl;
    system("pause");
}
原文地址:https://www.cnblogs.com/denghui666/p/8626506.html