【UVA213】Message Decoding(读入技巧+二进制十进制转换)

题目:https://vjudge.net/problem/UVA-213

思路:


1.(len,value)二元组,len为二进制位数,value为第value+1个len位二进制,code[len][value]为其对应的字母

len最大为7,value最大为1<<8-1

如:对于

TNM AEIOU

code[1][0]=T     code[2][0]=N       code[2][1]=M       code[2][2]=空格

依次类推

2.编码头在一行,但是输入的编码文本却可能是由多行组成的,且中间可能有任意个空格换行之类的,要注意读取技巧,边输入边读

 

ac代码:


《算法竞赛入门经典》紫书代码

#include <iostream>
#include <cmath>
#include <string.h>
#include <ctype.h>
#include <algorithm>
#include <stdlib.h>
#define maxn 100
#define inf 1e+9+10
using namespace std;
typedef long long ll;
int code[8][1<<8];
int readchar()
{
    for( ; ;)
    {
        int ch=getchar();
        if(ch!='\n'&&ch!='\r')
            return ch;
    }
}
int readint(int c)
{
    int v=0;
    while(c--)
        v=v*2+readchar()-'0';
    return v;
}
int readcodes()
{
    memset(code,0,sizeof(code));
    code[1][0]=readchar();
    for(int len=2;len<=7;len++)
    {
        for(int i=0;i<(1<<len)-1;i++)
        {
            int ch=getchar();
            if(ch==EOF) return 0;//读取到文件末尾,没有输入
            if(ch=='\n'||ch=='\r') return 1;//结束输入编码头
            code[len][i]=ch;
        }
    }
    return 1;
}
void printcodes()
{
    for(int len=1;len<=7;len++)
    {
        for(int i=0;i<(1<<len)-1;i++)
        {
            if(code[len][i]==0)//说明后面都是0,未存入数据
                return ;
            printf("code[%d][%d]=%c\n",len,i,code[len][i]);
        }
    }
}
int main()
{
    //freopen("/Users/zhangkanqi/Desktop/11.txt","r",stdin);
    while(readcodes())
    {
        //printcodes();
        for(; ;)
        {
            int len=readint(3);
            if(len==0)//000时
                break;
            //printf("len=%d\n",len);
            for( ; ;)
            {
                int v=readint(len);
               //printf("v=%d\n",v);
                if(v==(1<<len)-1)//len位都为1时
                    break;
                putchar(code[len][v]);
            }
        }
        printf("\n");
    }
    return 0;
}

网上参考大神的代码:

#include <iostream>
#include <cmath>
#include <string.h>
#include <ctype.h>
#include <algorithm>
#include <stdlib.h>
#define maxn 100
#define inf 1e+9+10
using namespace std;
typedef long long ll;
char code[8][1<<8];
string s;
char ch;
int len,val,l,i,j,a,b,c;
void print()
{
    while(1)
    {
        int val = 0;
        for (i = 0; i < len; i++) {
            do {
                scanf("%c", &ch);
            } while (!isdigit(ch));
            val = val * 2 + (ch - '0');
        }
        if (val == ((1 << len) - 1))//全1
            return;
        else
            putchar(code[len][val]);
    }
}
int main()
{
    //freopen("/Users/zhangkanqi/Desktop/11.txt","r",stdin);
    while(getline(cin,s))
    {
        if(!s[0])//不是很懂这句话,不加的话会pe
            continue;
        len=1,val=0,l=s.length();
        memset(code,0,sizeof(code));
        for(i=0;i<l;i++)
        {
            code[len][val++]=s[i];
            if(val==((1<<len)-1))
            {
                len++;
                val = 0;
            }
        }
        /*for(i=1;i<=len;i++)
            for(j=0;j<((1<<i)-1);j++)
            {
                if(code[i][j]!=0)
                {
                    cout << i << ' ' << j << ' ';
                    putchar(code[i][j]);
                    cout << endl;
                }
            }输出code[][]*/
        while(scanf("%1d%1d%1d",&a,&b,&c))
        {
            len=a*4+b*2+c;
            if(len==0)
            {
                printf("\n");
                break;
            }
            print();
        }
    }
    return 0;
}

 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值