算法四

霍夫曼解码

算法描述

  • 当一串文本进行霍夫曼编码后,每一个字符变成了0和1字符串(bit字符串)
  • 任意一个bit字符串不是另一个bit字符串的前缀
  • 这条规则可以使我们轻易解码霍夫曼编码的文本

参数定义

  • 类名 HuffmanDecoding
  • 方法 decode
  • 输入参数 string, vector <string>
  • 输出 string
  • 方法声明 string decode(string archive, vector <string> dictionary)

限制条件

  • archive包含[1,50]个字符
  • archive只包含字符’0’或‘1’
  • dictionary包含[1,26]个元素
  • dictionary中的每个元素包含[1,50]个字符
  • dictionary中的每个元素都不能成为其他元素的前缀
  • archive使用dictionary来解码

例子

  • 输入
    • archive: “101101”
    • dictionary: {“00”,”10”,”01”,”11”}
  • 输出
    • ”BDC”

测试实例

  • 实例一

    • 输入
      • archive: “10111010”
      • dictionary: {“0”,”111”,”10”}
    • 输出
      • ”CBAC”
  • 实例二

    • 输入
      • archive: “0001001100100111001”
      • dictionary: {“1”,”0”}
    • 输出
      • ”BBBABBAABBABBAAABBA”
  • 实例三

    • 输入
      • archive: “111011011000100110”
      • dictionary: {“010”,”00”,”0110”,”0111”,”11”,”100”,”101”}
    • 输出
      • ”EGGFAC”
  • 实例四

    • 输入
      • archive: “001101100101100110111101011001011001010”
      • dictionary: {“110”,”011”,”10”,”0011”,”00011”,”111”,”00010”,”0010”,”010”,”0000”}
    • 输出
      • ”DBHABBACAIAIC”

代码

#include <iostream>
#include <string>
#include <vector>
#include <stdlib.h>

using namespace std;

// -------------- start of solution ------------
class HuffmanDecoding
{
public:
    string decode(string archive, vector<string> dictionary)
    {
        string ret ="";
        string strTmp="";
        for(int i=0; i<archive.length(); i++)
        {
            strTmp += archive[i];
            if(!compareDic(strTmp,dictionary))
            {
                i--;
                ret += computeCharacter(strTmp.substr(0,strTmp.length()-1),dictionary);
                strTmp.clear();
            }
        }
        ret += computeCharacter(strTmp,dictionary);
        return ret;
    }

private:
    //
    bool compareDic(string strTmp, vector<string> dictionary)
    {
        for(vector<string>::const_iterator iter=dictionary.begin(); iter!=dictionary.end(); iter++)
        {
            string strDic = (string)*iter;
            if(strDic.length()>=strTmp.length())
            {
                if(strDic.substr(0,strTmp.length()) == strTmp)
                    return true;
            }
        }
        return false;
    }

    // 返回该字符串对应的字母
    string computeCharacter(string strTmp, vector<string> dictionary)
    {
        string ret="A";
        int sumCount = dictionary.size();
        for(int i = 0; i<sumCount; i++)
        {
            if(dictionary[i] == strTmp)
            {
                ret[0]= ret[0]+i;
                return ret;
            }
        }
    }
};
// -------------- end of solution --------------

int main()
{
    HuffmanDecoding hd;
    string archive = "0001001";
    vector<string> dictionary;
    dictionary.push_back("1");
    dictionary.push_back("0");
    string ret = hd.decode(archive, dictionary);
    cout << ret << endl;
    system("pause");

    cout.flush();
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值