将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 ...
随机推荐
- 基于visual Studio2013解决C语言竞赛题之0520相邻元素
题目
- 用php 把数组中偶数,选择出来
我有这种一个小算法,把数组中的全部的偶数或技术分别选择出来.非常多人可能,会循环这个数组,而我恰恰不循环数组就能做到这一点.代码例如以下. function odd($var) { // return ...
- Android灭亡论之Firefox OS操作系统出现
今天是2014年7月1日,过几天就要到深圳实训去了,实训核心内容是Android开发.尽管Android现在很火,但作为程序猿的我们必须时刻保持清醒的头脑.我虽不是什么预言家,但近期接触的Androi ...
- phantomjs环境搭建已经运行
1.下载phantomjs http://phantomjs.org/ 2.运行 新建phantomjs.bat,记得改目录路径 里面内容为: D:\java\phantomjs\phantomjs. ...
- 安卓开发-Activity中finish() onDestroy() 和System.exit()的区别
Activity.finish()Call this when your activity is done and should be closed. 在你的activity动作完成的时候,或者Act ...
- 工作随记 warning: 'includeantruntime' was not set, defaulting to build.sysclasspath=last; set to false for repeatable builds
错误信息:F:\BUILD\IDS7020\trunk\manage_src\dev\java_src\tds7030-web\Ant\build.xml:344: warning: 'include ...
- 输入输出函数 I/O函数之perror()
perror()函数的函数原型 void perror(char const *message); 它会将message信息输出出来,后面再加上错误原因字符串. 下面是来自百度百科的实例: #incl ...
- Qt 状态机框架学习(没学会)
Qt状态机框架是基于状态图XML(SCXML) 实现的.从Qt4.6开始,它已经是QtCore模块的一部分.尽管它本身是蛮复杂的一套东西,但经过和Qt的事件系统(event system).信号槽(s ...
- 动态网页爬取例子(WebCollector+selenium+phantomjs)
目标:动态网页爬取 说明:这里的动态网页指几种可能:1)需要用户交互,如常见的登录操作:2)网页通过JS / AJAX动态生成,如一个html里有<div id="test" ...
- jQuery操作checkbox的问题
问题: 使用 jquery 的 attr('checked',false) 和 attr('checked',true) 方法给 checkbox 设置选中和未选中状态时,失效. 原因: jquery ...