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 != ‘\n‘ && a != ‘\r‘)
        {
            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 == ‘\n‘ || b == ‘\r‘)
            {
                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

时间: 2024-08-11 04:04:10

UVa 213 Message Decoding (信息编码)的相关文章

UVa 213 Message Decoding(World Finals1991,字符串)

 Message Decoding  Some message encoding schemes require that an encoded message be sent in two parts. The first part, called the header, contains the characters of the message. The second part contains a pattern that represents the message. You must

[UVa 213]Message Decoding,ACM/ICPC World Finals 1991 信息解码

A children's board game consists of a square array of dots that contains lines connecting some of the pairs of adjacent dots. One part of the game requires that the players count the number of squares of certain sizes that are formed by these lines.

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(c

uva 213 - Message Decoding (我觉得我的方法要比书上少很多代码,不保证好……)

#include<stdio.h> #include<math.h> #include<string.h> char s[250]; char a[10][250]; int a1[4]; int a2[250]; char ch; int init(int len) { int tt=0; for(int i=1;i<=7;i++) { for(int j=0;j<(int)pow(2,i)-1;j++) { a[i][j]=s[tt++]; if(tt&

UVA - 213 Message Decoding (输入字符串并对单个字符进行操作的输入输出)

POINT: 关于表示一个编码:利用code字符数组表示一个编码字符,其中code[len][val]表示长度为len,二进制值为val的字符: 主程序如下: 1 #include <iostream> 2 #include <sstream> 3 #include <cstdio> 4 #include <cstring> 5 #include <cmath> 6 #include <string> 7 #include <v

UVA213 UVALive5152 Message Decoding

World Finals >> 1991 - San Antonio 问题链接:UVA213 UVALive5152 Message Decoding. 问题简述:参见问题链接. 问题分析:(略). 程序中,若干功能封装到函数中,使得程序逻辑变得简洁. AC的C语言程序如下: /* UVA213 UVALive5152 Message Decoding */ #include <stdio.h> #include <memory.h> #define CODE_LEN

UVa 213 信息编码!模拟!

背景:一次ac!!而且调试时间也短!!!!看来这个自定义函数,确实是一个好的方法!!构思又清晰,调试又明朗! 思路:一些单一的函数堆砌而成,每个函数有自己的功能. 学习:1.我是采用模拟手算二进制为十进制的方法,而小紫书上给出的方法似乎更简单:(这似乎透露除了字符串数转化普通数的方法)(普通二进制数,转化为十进制数就一位一位的拆分) //assumpt that temp[] have n charnumbers int decimal=0; for(int i = 0;i < n;i++){

紫书第一章训练1 D -Message Decoding(UVA213) by 16黄睿博

来源:http://m.blog.csdn.net/article/details?id=70766751 Some message encoding schemes require that an encoded message be sent in two parts. The ?rst part, called the header, contains the characters of the message. The second part contains a pattern tha

Message Decoding

Some message encoding schemes require that an encoded message be sent in two parts. The first part, called the header, contains the characters of the message. The second part contains a pattern that represents the message. You must write a program th