Compress the n-base array into a string (0-9 a-z) and decompress it.

Source: Internet
Author: User

Compress the n-base array into a string (0-9 a-z) and decompress it.

There are two points to define such a question:

1. Create a table: index the character with an array subscript. Note that if the character is returned to a number:

Int index = (str [I]> = '0' & str [I] <= '9 ')? (Str [I]-'0'-radix) :( str [I]-'A'-radix + 10 );

2. Pay attention to whether the low position is in front of the high position. If the first position isLow * radix ^ IYou can.

3. Calculate the number of radix hexadecimal arrays in one bits...


This is mainly troublesome...


#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;}




C # Sort the string in the order of A-Z, save it in an array first Add a new string, and ensure that the array is orderly

The array itself is unordered-the storage order. Therefore, Array Storage is not required for sorting. You can use the data sorting function sort or the lambda expression to sort the data and then take the value, don't worry about the order of Arrays-unless you are willing to open up an array space for storage-this requires memory.

C language, how to determine whether an element in a character array contains anything other than letters (a-z, A-Z) and numbers (0-9)

Yes. Use a loop.
The following only determines whether it is a letter or number. If not, the position and symbol are printed.
/* Note: Your choice is c ide */
# Include "stdio. h"
Void main ()
{

Int I, c = 0;
Char a [6];
Scanf ("% s", & );
For (I = 0; I <5; I ++)
{
If (a [I] <= 'Z' & a [I]> = 'A' | a [I] <= 'Z' & a [I]> = 'A' | A [I]> = 48 & a [I] <= 57)
{
C ++;
}
Else
Printf ("LOCATION % d: % c \ n", I + 1, a [I]);

}

If (c = 5)
{
Printf ("all letters or numbers \ n ");
}

}
If you don't understand it, you can ask

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.