比如一个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. #include <assert.h>
  2. #include <algorithm>
  3. #include <vector>
  4. using namespace std;
  5. const int radix = 3;
  6. string base = "";
  7. string compress(const vector<int>& arr) {
  8. string res = "";
  9. int i, j, size = arr.size(), left, bits;
  10. vector<int> base;
  11. for (i = 1, j = 0; i*radix+radix < 36; i *= radix, j++); // 剩余部分保持不变,直接追加到结尾
  12. bits = j;
  13. left = size - size / bits * bits;
  14. size -= left;
  15. for (char ch = '0'; ch <= '9'; ++ch)
  16. base.push_back(ch);
  17. for (char ch = 'a'; ch <= 'z'; ++ch)
  18. base.push_back(ch);
  19. for (i = 0; i < size; i += bits) {
  20. int tmp = 0, t = 1;
  21.  
  22. for (j = 0; j < bits; ++j) {
  23. tmp += arr[i+j]*t;
  24. t *= radix;
  25. }
  26. res += base[radix + tmp];
  27. }
  28. for (j = 0; j < left; ++j)
  29. res += base[arr[i+j]];
  30. return res;
  31. }
  32. vector<int> depress(const string& str) {
  33. vector<int> res;
  34. int i, j, len = str.length(), idx, bits;
  35. for (i = 1, j = 0; i*radix+radix < 36; i *= radix, j++); // 剩余部分保持不变。直接追加到结尾
  36. bits = j;
  37. for (i = 0; i < len; ++i) {
  38. idx = str[i] >= 'a' && str[i] <= 'z' ?
  39.  
  40. (str[i] - 'a' + 10) : (str[i] - '0');
  41. if (idx < radix) {
  42. res.push_back(idx);
  43. }
  44. else {
  45. idx -= radix;
  46. for (j = 0; j < bits; ++j) {
  47. res.push_back(idx%radix);
  48. idx /= radix;
  49. }
  50. }
  51. }
  52. return res;
  53. }
  54. int main() {
  55. int arr[] = {0,1,2,2,2,2,1,1,1,2,0,0,0,0,0,1};
  56. vector<int> vec(arr, arr+sizeof(arr)/sizeof(int));
  57. string str = compress(vec);
  58. vector<int> res = depress(str);
  59. return 0;
  60. }

以下的代码多了非常多没用的控制字符,不知道为啥。

。。

  1. #include <assert.h>
  2. #include <algorithm>
  3. #include <vector>
  4. using namespace std;
  5. const int radix = 4;
  6. string base = "";
  7. string compress(const vector<int>& arr) {
  8.   string res = "";
  9.   
  10.   int i, j, size = arr.size(), left, bits;
  11.   
  12.   for (i = 1, j = 0; i*radix + radix < 36; i *= radix, ++j);
  13.   bits = j;
  14.   left = size - size / bits * bits;
  15.   size = size / bits * bits;
  16.  
  17.   for (char ch = '0'; ch <= '9'; ++ch)
  18.     base.push_back(ch);
  19.   for (char ch = 'a'; ch <= 'z'; ++ch)
  20.     base.push_back(ch);
  21.   for (i = 0; i < size; i += bits) {
  22.     int index = 0, t = 1;
  23.     for (j = 0; j < bits; ++j) {
  24.       index = index + arr[i+j]*t;
  25.       t *= radix;
  26.     }           
  27.     res.push_back(base[radix+index]);
  28.   }
  29.   for (i = 0; i < left; ++i)
  30.     res.push_back(arr[size+i]+'0');
  31.   return res; 
  32. }
  33.  
  34. vector<int> depress(const string& str) {
  35.   int len = str.length(), i = 0, j, bits;
  36.   for (i = 1, j = 0; i*radix + radix< 36; i *= radix, ++j);
  37.   bits = j;
  38.   vector<int> res;
  39.   for (i = 0; i < len; ++i) {
  40.     if (str[i]-'0' >= 0 && str[i]-'0' < radix) 
  41.       res.push_back(str[i]-'0');
  42.     else {
  43.       int index = (str[i] >= '0' && str[i] <= '9') ? (str[i]-'0'-radix):(str[i]-'a'-radix + 10);      
  44.       for (j = 0; j < bits; ++j) {
  45.         res.push_back(index%radix);
  46.         index = index/radix;
  47.       }
  48.     }
  49.   }
  50.   return res;
  51. } 
  52. int main() {
  53.  
  54.   int arr[] = {0,1,2,2,2,2,1,1,1,2,0,0,0,0,0,1};
  55.   vector<int> vec(arr, arr+sizeof(arr) / sizeof(int));
  56.  
  57.   string str = compress(vec);
  58.   vector<int> res = depress(str);
  59.  
  60.   return 0;
  61. }

将n进制的数组压缩成字符串(0-9 a-z)同一时候解压的更多相关文章

  1. C#字节数组转换成字符串

    C#字节数组转换成字符串 如果还想从 System.String 类中找到方法进行字符串和字节数组之间的转换,恐怕你会失望了.为了进行这样的转换,我们不得不借助另一个类:System.Text.Enc ...

  2. 100怎么变成100.00 || undefined在数字环境下是:NaN || null在数字环境下是0 || 数组的toString()方法把每个元素变成字符串,拼在一起以逗号隔开 || 空数组转换成字符串后是什么?

    100怎么变成100.00?

  3. php部分--例子:租房子(复选框的全选、数组拼接成字符串、设置复选框的name值、)

    1.链接数据库 <?php include("DBDA.class.php"); $db=new DBDA(); $sql="select * from fangz ...

  4. 怎样把php数组转换成字符串,php implode()

    实例代码 一维数组转换成字符串代码! <?php $arr1=array("shu","zhu","1"); $c=implode(& ...

  5. 在Ajax中将数组转换成字符串(0517-am)

    一.如何在Ajax中将数组转换成字符串 1. 主页面; <head> <meta http-equiv="Content-Type" content=" ...

  6. PHP 数组拼接成字符串

    PHP[知识分享] 数组拼接成字符串 <?php // 格式: [二维数组] Array ( [0] => Array ( [topicid] => 1 ) [1] => Ar ...

  7. Java中如何将字符串数组转换成字符串

    如果将“字符串数组”转换成“字符串”,只能通过循环,没有其他方法: public static String getExecSqlString(String str){ StringBuffer sb ...

  8. js冒泡法和数组转换成字符串示例代码

    将数组转换成字符串的方法有很多,讲解下js冒泡法的使用.js代码: //js冒泡法与数据转换为字符串的例子 //整理:www.jbxue.com window.onload = function(){ ...

  9. 【JS】jQuery中将数组转换成字符串join()和push()使用

    1.push()将元素依次添加至数组:2.join()将数组转换成字符串,里面可以带参数分隔符,默认[,] <script type = text/javascript> $(docume ...

随机推荐

  1. 基于visual Studio2013解决C语言竞赛题之0520相邻元素

          题目

  2. 用php 把数组中偶数,选择出来

    我有这种一个小算法,把数组中的全部的偶数或技术分别选择出来.非常多人可能,会循环这个数组,而我恰恰不循环数组就能做到这一点.代码例如以下. function odd($var) { // return ...

  3. Android灭亡论之Firefox OS操作系统出现

    今天是2014年7月1日,过几天就要到深圳实训去了,实训核心内容是Android开发.尽管Android现在很火,但作为程序猿的我们必须时刻保持清醒的头脑.我虽不是什么预言家,但近期接触的Androi ...

  4. phantomjs环境搭建已经运行

    1.下载phantomjs http://phantomjs.org/ 2.运行 新建phantomjs.bat,记得改目录路径 里面内容为: D:\java\phantomjs\phantomjs. ...

  5. 安卓开发-Activity中finish() onDestroy() 和System.exit()的区别

    Activity.finish()Call this when your activity is done and should be closed. 在你的activity动作完成的时候,或者Act ...

  6. 工作随记 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 ...

  7. 输入输出函数 I/O函数之perror()

    perror()函数的函数原型 void perror(char const *message); 它会将message信息输出出来,后面再加上错误原因字符串. 下面是来自百度百科的实例: #incl ...

  8. Qt 状态机框架学习(没学会)

    Qt状态机框架是基于状态图XML(SCXML) 实现的.从Qt4.6开始,它已经是QtCore模块的一部分.尽管它本身是蛮复杂的一套东西,但经过和Qt的事件系统(event system).信号槽(s ...

  9. 动态网页爬取例子(WebCollector+selenium+phantomjs)

    目标:动态网页爬取 说明:这里的动态网页指几种可能:1)需要用户交互,如常见的登录操作:2)网页通过JS / AJAX动态生成,如一个html里有<div id="test" ...

  10. jQuery操作checkbox的问题

    问题: 使用 jquery 的 attr('checked',false) 和 attr('checked',true) 方法给 checkbox 设置选中和未选中状态时,失效. 原因: jquery ...