将n进制的数组压缩成字符串(0-9 a-z)同时解压

本文介绍了一种将3进制数组压缩为字符串的方法,并提供了相应的解压算法。通过对数组进行位运算和基数转换,实现了高效的数据压缩与还原。

例如一个3进制的数组: [1 1 2 2 2 0 0] 用一个字符串表示。。。


此类题目要明确两点:

1. 打表:用数组下标索引字符,同时注意如果从字符对应回数字: 

int index = (str[i] >= '0' && str[i] <= '9') ? (str[i]-'0'-radix):(str[i]-'a'-radix + 10);

2. 注意低位在前还是高位在前,如果先来的是 低位*radix^i 即可。

3. 统计每几个radix进制数组成一位,利用bits来表示。。。


这破题主要是麻烦。。。


#include <assert.h>
#include <algorithm>
#include <vector>
using namespace std;
const int radix = 3;
string base = "";
string compress(const vector<int>& arr) {
  string res = "";
  int i, j, size = arr.size(), left, bits;
  vector<int> base;
  for (i = 1, j = 0; i*radix+radix < 36; i *= radix, j++); // 剩余部分保持不变,直接追加到结尾
  bits = j;
  left = size - size / bits * bits;
  size -= left;
  for (char ch = '0'; ch <= '9'; ++ch)
    base.push_back(ch);
  for (char ch = 'a'; ch <= 'z'; ++ch)
    base.push_back(ch);
  for (i = 0; i < size; i += bits) {
    int tmp = 0, t = 1;

    for (j = 0; j < bits; ++j) {
      tmp += arr[i+j]*t;
      t *= radix;
    }
    res += base[radix + tmp];
  }
  for (j = 0; j < left; ++j)
    res += base[arr[i+j]];
  return res;
}
vector<int> depress(const string& str) {
  vector<int> res;
  int i, j, len = str.length(), idx, bits;
  for (i = 1, j = 0; i*radix+radix < 36; i *= radix, j++); // 剩余部分保持不变,直接追加到结尾
  bits = j;
  for (i = 0; i < len; ++i) {
    idx = str[i] >= 'a' && str[i] <= 'z' ? (str[i] - 'a' + 10) : (str[i] - '0');
    if (idx < radix) {
      res.push_back(idx);
    }
    else {
      idx -= radix;
      for (j = 0; j < bits; ++j) {
        res.push_back(idx%radix);
        idx /= radix;
      }
    }
  }
  return res;
}
int main() {
  int arr[] = {0,1,2,2,2,2,1,1,1,2,0,0,0,0,0,1};
  vector<int> vec(arr, arr+sizeof(arr)/sizeof(int));
  string str = compress(vec);
  vector<int> res = depress(str);
  return 0;
}



下面的代码多了很多无用的控制字符,不知道为啥。。。


#include <assert.h>
#include <algorithm>
#include <vector>
using namespace std;
const int radix = 4;
string base = "";
string compress(const vector<int>& arr) {
  string res = "";
  
  int i, j, size = arr.size(), left, bits;
  
  for (i = 1, j = 0; i*radix + radix < 36; i *= radix, ++j);
  bits = j;
  left = size - size / bits * bits;
  size = size / bits * bits;


  for (char ch = '0'; ch <= '9'; ++ch)
    base.push_back(ch);
  for (char ch = 'a'; ch <= 'z'; ++ch)
    base.push_back(ch);
  for (i = 0; i < size; i += bits) {
    int index = 0, t = 1;
    for (j = 0; j < bits; ++j) {
      index = index + arr[i+j]*t;
      t *= radix;
    }           
    res.push_back(base[radix+index]);
  }
  for (i = 0; i < left; ++i)
    res.push_back(arr[size+i]+'0');
  return res; 
}


vector<int> depress(const string& str) {
  int len = str.length(), i = 0, j, bits;
  for (i = 1, j = 0; i*radix + radix< 36; i *= radix, ++j);
  bits = j;
  vector<int> res;
  for (i = 0; i < len; ++i) {
    if (str[i]-'0' >= 0 && str[i]-'0' < radix) 
      res.push_back(str[i]-'0');
    else {
      int index = (str[i] >= '0' && str[i] <= '9') ? (str[i]-'0'-radix):(str[i]-'a'-radix + 10);      
      for (j = 0; j < bits; ++j) {
        res.push_back(index%radix);
        index = index/radix;
      }
    }
  }
  return res;
} 
int main() {


  int arr[] = {0,1,2,2,2,2,1,1,1,2,0,0,0,0,0,1};
  vector<int> vec(arr, arr+sizeof(arr) / sizeof(int));




  string str = compress(vec);
  vector<int> res = depress(str);


  return 0;
}



题目描述 输入一串字符串,根据给定的字符串中字符出现的频率建立相应哈夫曼树,构造哈夫曼编码表,在此基础上可以对待压缩文件进行压缩(即编码),同时可以对压缩后的二进制编码文件进行解压(即译码)。 输入格式 多组数据,每组数据一行,为一个字符串(只考虑26个小写字母即可)。当输入字符串为“0”时,输入结束。 输出格式 每组数据输出2n+3行(n为输入串中字符类别的个数)。第一行为统计出来的字符出现频率(只输出存在的字符,格式为:字符:频度),每两组字符之间用一个空格分隔,字符按照ASCII码从小到大的顺序排列。第二行至第2n行为哈夫曼树的存储结构的终态(形如教材139页表5.2(b),一行当中的数据用空格分隔)。第2n+1行为每个字符的哈夫曼编码(只输出存在的字符,格式为:字符:编码),每两组字符之间用一个空格分隔,字符按照ASCII码从小到大的顺序排列。第2n+2行为编码后的字符串,第2n+3行为解码后的字符串(与输入的字符串相同)。 数据范围 无 输入用例 aaaaaaabbbbbccdddd aabccc 0 输出用例 a:7 b:5 c:2 d:4 1 7 7 0 0 2 5 6 0 0 3 2 5 0 0 4 4 5 0 0 5 6 6 3 4 6 11 7 2 5 7 18 0 1 6 a:0 b:10 c:110 d:111 00000001010101010110110111111111111 aaaaaaabbbbbccdddd a:2 b:1 c:3 1 2 4 0 0 2 1 4 0 0 3 3 5 0 0 4 3 5 2 1 5 6 0 3 4 a:11 b:10 c:0 111110000 aabccc 不用堆实现
06-14
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值