将n进制的数组压缩成字符串(0-9 a-z)同一时候解压
2016-01-18 11:07
543 查看
比如一个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来表示。
。。
这破题主要是麻烦。。
。
以下的代码多了非常多没用的控制字符,不知道为啥。
。。
此类题目要明白两点:
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; }
相关文章推荐
- Uncaught TypeError: Cannot set property 'f7View' of undefined 错误原因
- POJ2481(树状数组:统计数字 出现个数)
- TabLayout的使用
- 项目BUG总结
- ListView局部刷新
- 移动产品的首次邂逅
- 防止div因内容过多而被撑大或变形
- 也谈自执行函数
- VC++ 获取文件属性创建时间、修改时间和访问时间
- generating project in interactive mode
- 库
- ArcGIS教程:克里金法的工作原理(二)
- ARGB 值详解
- UITableView刷新数据reLoadData
- 添加全局引用文件pch
- Java学习笔记(65)---------线程协作
- 别老扯什么Hadoop了,你的数据根本不够大
- eclipse 启动不了
- 使用HttpURLConnection向服务器发送post和get请求
- IOS开发UI-------button