将n进制的数组压缩成字符串(0-9 a-z)同时解压
来源:互联网 发布:如何配置c语言环境 编辑:程序博客网 时间:2024/05/20 23:35
例如一个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;}
1 0
- 将n进制的数组压缩成字符串(0-9 a-z)同时解压
- 使用chilkat解压.Z(unix压缩格式)的压缩文件
- 如何实现用更少的空间表示英文字母(a ~ z)构成char A[n]字符串
- 如何实现用更少的空间表示英文字母(a ~ z)构成char A[n]字符串
- android 解压.Z后缀的压缩包
- 华为上机笔试之通过键盘输入一串小写字母(a~z)组成的字符串。请编写一个字符串压缩程序,将字符串中连续出席的重复字母进行压缩,并输出压缩后的字符串。
- 通过键盘输入一串小写字母(a~z)组成的字符串。请编写一个字符串压缩程序,将字符串中连续出席的重复字母进行压缩,并输出压缩后的字符串。
- 通过键盘输入一串小写字母(a~z)组成的字符串。请编写一个字符串压缩程序,将字符串中连续出席的重复字母进行压缩,并输出压缩后的字符串。
- 通过键盘输入一串小写字母(a~z)组成的字符串。请编写一个字符串压缩程序,将字符串中连续出席的重复字母进行压缩,并输出压缩后的字符串。
- php获取a-z,A-Z,0-9的随机字符串
- php获取a-z,A-Z,0-9的随机字符串
- 实现简易字符串压缩算法:一个长度最大为128的字符串, 由字母a-z或者A-Z组成,将其中连续出现2次以上(含2次)的字母转换为字母和出现次数,以达到压缩目的
- 字符串的压缩和解压
- 字符串的压缩与解压
- 压缩与解压1---字符串的压缩与解压
- 将输入的字符串(字符串仅包含小写字母‘a’到‘z’),按照如下规则,循环转换后输出:a->b,b->c,…,y->z,z->a;
- 通过键盘输入一串小写字母(a~z)组成的字符串。请编写一个字符串压缩程序,将字符串中连续出席的重复字母进行压缩,并输出压缩后的字符串。 压缩规则: 1、仅压缩连续重复出现的字符。比如字符串"abcbc
- 给定一个字符串A,同时给定它的长度n及参数字符数组arg,请返回替换后的字符串
- 第6部分:AOP
- 1261 大数与常数的乘除
- 为何说我们需要山寨币
- HDU 1196 Lowest Bit
- 好久没有更新blog了
- 将n进制的数组压缩成字符串(0-9 a-z)同时解压
- tomcat使用教程
- java GUI设计和布局
- 悟道—位IT高管20年的职场心经(读书笔记二)
- 为什么必须保护Robots协议?
- CocoaPods安装和使用教程
- oracle 常用语法
- Vim 配置
- 正则表达式