UVa 213 (implement)

本文介绍了一种利用哈希映射进行编码的方法,并通过具体的C++实现代码展示了如何简化编码过程。该方法通过读取特定长度的输入,并将其映射到预定义的字符上,实现了高效的数据编码。

一种哈希映射的方法,简化代码量。

/*
* 20171028
*/

//#define LOCAL

#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;

#define maxn 1000

char code[8][1<<8];
char head[maxn];

int readchar()
{
    char t = getchar();
    if(t=='0'||t=='1') return t-'0';
    else return readchar();
}

bool readlen(int& len)
{
    int a = readchar();
    int b = readchar();
    int c = readchar();
    len = a*4+b*2+c;
    if(len==0)
    {
        //printf("the all code is over!\n");
        return false;
    }
    return true;
}

bool readcode(int len)
{
    int v = 0;
    for(int i=0;i<len;i++)
    {
        v*=2;
        v+=readchar();
    }
    if(v>=((1<<len)-1))
    {
        //printf("the segment is over!\n");
        return false;
    }
    //printf("len:%d v:%d\n code:%c\n",len,v,code[len][v]);
    printf("%c",code[len][v]);
    return true;
}

void solvecode()
{
    int len = 0;
    while(readlen(len))
    {
        while(readcode(len));
    }
}

int main()
{
    #ifdef LOCAL
        freopen("input.txt","r",stdin);
        freopen("output.txt","w",stdout);
    #endif
    while(fgets(head,maxn,stdin)!=NULL)
    {
        memset(code,0,sizeof(code));
        int len = strlen(head)-1;
        int loc = 0;
        int i = 0;
        while(i<len)
        {
            int j = 0;
            loc++;
            while(j<((1<<loc)-1))
            {
                code[loc][j] = head[i];
                //printf("loc:%d j:%d code:%c head:%c\n",loc,j,code[loc][j],head[i]);
                i++;
                j++;
                if(i>=len) break;
            }
        }
        solvecode();
        printf("\n");
        getchar();
    }
    return 0;
}
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值