将n进制的数组压缩成字符串(0-9 a-z)同一时候解压
比如一个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;
}
将n进制的数组压缩成字符串(0-9 a-z)同一时候解压的更多相关文章
- C#字节数组转换成字符串
C#字节数组转换成字符串 如果还想从 System.String 类中找到方法进行字符串和字节数组之间的转换,恐怕你会失望了.为了进行这样的转换,我们不得不借助另一个类:System.Text.Enc ...
- 100怎么变成100.00 || undefined在数字环境下是:NaN || null在数字环境下是0 || 数组的toString()方法把每个元素变成字符串,拼在一起以逗号隔开 || 空数组转换成字符串后是什么?
100怎么变成100.00?
- php部分--例子:租房子(复选框的全选、数组拼接成字符串、设置复选框的name值、)
1.链接数据库 <?php include("DBDA.class.php"); $db=new DBDA(); $sql="select * from fangz ...
- 怎样把php数组转换成字符串,php implode()
实例代码 一维数组转换成字符串代码! <?php $arr1=array("shu","zhu","1"); $c=implode(& ...
- 在Ajax中将数组转换成字符串(0517-am)
一.如何在Ajax中将数组转换成字符串 1. 主页面; <head> <meta http-equiv="Content-Type" content=" ...
- PHP 数组拼接成字符串
PHP[知识分享] 数组拼接成字符串 <?php // 格式: [二维数组] Array ( [0] => Array ( [topicid] => 1 ) [1] => Ar ...
- Java中如何将字符串数组转换成字符串
如果将“字符串数组”转换成“字符串”,只能通过循环,没有其他方法: public static String getExecSqlString(String str){ StringBuffer sb ...
- js冒泡法和数组转换成字符串示例代码
将数组转换成字符串的方法有很多,讲解下js冒泡法的使用.js代码: //js冒泡法与数据转换为字符串的例子 //整理:www.jbxue.com window.onload = function(){ ...
- 【JS】jQuery中将数组转换成字符串join()和push()使用
1.push()将元素依次添加至数组:2.join()将数组转换成字符串,里面可以带参数分隔符,默认[,] <script type = text/javascript> $(docume ...
随机推荐
- Genymotion Unable to create Virtual Device:Connection timeout
1.进入C:\Users\[UserName]\AppData\Local\Genymobile,打开genymotion.log,找到最后几句话: 九月 2 14:29:45 [Genymo ...
- 言未及之而言,谓之躁;言及之而不言,谓之隐;未见颜色而言,谓之瞽(gǔ)
前言 一个高效的团队离不开leader和组员之前,组员和组员之前的通力合作.而合作的基础便是彼此之间的商讨与协调,意见的统一,进而在达成共识的前提下行动.那么如何才能和组员达成共识呢? 和组员之间的沟 ...
- 转:什么是 HTTP Headers?
什么是HTTP Headers HTTP是“Hypertext Transfer Protocol”的所写,整个万维网都在使用这种协议,几乎你在浏览器里看到的大部分内容都是通过http协议来传输的,比 ...
- 不老的新丁 Python何以让人着迷
Python是一门美丽的语言.它简单易学,跨平台,而且运转良好.达成了许多Java一直求索的技术目标.一言以蔽之就是:其他的语言是与时代同 步,而Python则是未雨绸缪,而且计划得颇为出色.当然,这 ...
- 基于visual Studio2013解决算法导论之016查找最大值最小值
题目 查找最大.最小值 解决代码及点评 #include <stdio.h> #include <stdlib.h> #include <malloc.h> ...
- eclipse上 安装php插件
首先在安装之前需要有eclipse 以及SDK环境已经搭建好 eclipse开发工具下载路径: http://dl.oschina.net/soft/eclipse java sdk下载路径: h ...
- ListView属性解释
1.android:scrollbarStyle 定义滚动条的样式和位置 参考:http://www.trinea.cn/android/android-scrollbarstyle/ 2.andro ...
- source insight 中文注释为乱码解决
1. source insight 中文注释为乱码解决 http://blog.csdn.net/bingfeng1210/article/details/7527059 2. Source Insi ...
- iOS设置textfield为密码框
self.passWordTextField.secureTextEntry = YES;
- How to calculate the undo_retention time
UNDO_RETENTION The undo_retention is a initialization parameter of the undo tablespace. The initiali ...