将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 ...
随机推荐
- SilkTest天龙八部系列5-类的属性
SilkTest的面向对象机制让用户可以为类定义属性,用property语句实现.除此以外用户在类中还可以定义成员变量和不可变的setting属性.也就是是说Silktest类中可以有以下三种属性/变 ...
- go - 变量和常量
1.定义变量 goLang中定义变量的方式很多 先声明再使用:如果定义的变量未使用编译时会报错 a. /*定义单个变量*/ var varName type //定义一个 type 类型的变量 var ...
- C++ 中dynamic_cast<>的用法
/*这是从网上摘下的样例,主要讲述了 dynamic_cast<> 的用法.*/ /* 作用:将一个基类对象指针(或引用)cast到继承类指针,dynamic_cast会依据基类指针是否真 ...
- java高级project师须要掌握的技术
1.你须要精通面向对象分析与设计(OOA/OOD).涉及模式(GOF,J2EEDP)以及综合模式.你应该十分了解UML,尤其是class,object,interaction以及statediagra ...
- C++中rand()函数的用法
C++中rand()函数的用法 2011-12-30 11:03:59| 分类: C / C++|举报|字号 订阅 一.C++中不能使用random()函数 random函数不是ANSI C标准 ...
- HDOJ 3047 带权并查集
解题思路转自: http://blog.csdn.net/azheng51714/article/details/8500459 http://blog.csdn.net/acresume/artic ...
- c语言,内存字节对齐
引用:内存字节对齐 写出一个struct,然后sizeof,你会不会经常对结果感到奇怪?sizeof的结果往往都比你声明的变量总长度要大,这是怎么回事呢?讲讲字节对齐吧. /************* ...
- Pip 安装 出现UnicodeEncodeError: 'ascii' codec can't encode characters in position 1-5: ordinal not in
在Python 环境下,使用PiP 命令安装时,报错提示: UnicodeEncodeError: 'ascii' codec can't encode characters in position ...
- 利用Ihttpmodel实现网站缓存,解决Server.Transfer 直接输出HTML源代码的问题
今天在用.NET利用IHttpModel实现网站静态缓存的时候,不知道最后为什么用 Server.Transfer(html)的时候结果输出的是HTML的源代码. 贴上源代码 using System ...
- 三种java 去掉字符串中的重复字符函数
三种java 去掉字符串中的重复字符函数 public static void main(string[] args) { system.out.println(removerepeatedchar( ...