Interview Questions: coding and decoding of five lexicographic orders

Source: Internet
Author: User

A friend told me a face-to-face question a few days ago: the five encoding ranges from 25 letters A to Y, from 1 to 4 digits,

If you sort the five encodings in Lexicographic Order, the following arrays are formed: A, AA, AAA, AAAA, aaab, aaac ,..., b, Ba, Baa, baaa, Baab... yyyx, yyyy

Where the index of A is 0, the index of AA is 1, the index of AAA is 2, and the index of AAAA is 3, and so on:

1) Compile a function. The input is a legal string and the index corresponding to the string is output;

2) Compile a function. The input is any legal index and the corresponding string of this index is output.

My friend gave me his current code. I watched the code for about 20 minutes and didn't know how he started it. Then I asked him: How do you think of your ideas? He said: naturally, arrange and combine!

After thinking about it for a while, I realized: gap. This is the gap.

Write down the train of thought and make a train of thought. The slave database is also good. [The following train of thought is a summary like Zhuge Liang later, but it is also a train of thought]

Note: if they are all 4-character fixed-length strings, It is very simple, it is a 25-digit notation, but here it is not long.

1. Observe the first few strings: A --> 0, AA-> 1, AAA-> 2, AAAA-> 3: You can see that the index here is: string Length-1

2. If the index of A is known, calculate the index of B: Because A to B is separated from the following four conditions of the string: string a followed by two characters has 25 (AA, AB ,... ay), a followed by 2 Characters of string has 25*25 (AAA, AAB ,... ayy), A is followed by a 3 character string with 25*25*25 (aaaa, aaab ,... ayyy), and then B, so B's Index = A's index + 25 + 25*25 + 25*25*25 + 1, add 1 because B is placed after the characters a and the middle.

3. Obtain the AB index based on the known AA index: Similarly, the AB Index = AA index + 25 + 25*25 + 1

4. Obtain the AAB index for known AAA indexes: Similarly, AAB indexes = aaa indexes + 25 + 15 and known AAAA indexes, and aaab indexes = AAAA indexes + 1

The index of AAAA, AAA, AA, and A is from 1: can be summarized as String Length-1

Therefore, a weight array can be used to indicate the modified hexadecimal format: factor [4] = {1 + 25 + 25*25 + 25*25*25*25, 1 + 25 + 25*25, 1 + 25, 1}

Then the index function of string is: Index (string) = (string. length-1) + sum [factor [I] * (string [I]-'A'), {I, 0, String. length-1}]

Sum is the sum of the internal expressions.

int encode(const char *str){int len = 0;int index = 0;int factor[] = {1+25+25*25+25*25*25, 1+25+25*25, 1+25, 1};  while(*str)index += factor[len++] * (*str++ - 'a');return index + (len - 1);}

2. decoding: the decoding process is the inverse process of the encoding process. With the factor array, it is much simpler.

// The DST character array void decode (char * DST, int index) {int I = 0; int factor [] = {1 + 25 + 25*25 + 25*25*25, 1 + 25 + 25*25, 1 + 25, 1 }; while (index> = 0) {* DST ++ = 'A' + index/factor [I]; index % = factor [I ++]; -- index; // reduce 1 to the next character.} * DST = '\ 0 ';}

3. Deformation: with the help of a friend, I was thinking that the longer the five encodings are, the less frequently used the strings, and the less sort by the dictionary, it should be better to sort by letter length:

A, B ,... x, Y, AA, AB ,... YY, AAA, AAB ,..., YYY, AAAA, aaab ,..., yyyy; if this sorting is done, the index is much simpler?

At least the index can be divided by the length of a string. For example, the string range of 1 is [0, 25-1], and the string range of 2 is [25, 25*25-1]. and so on.

But if segmentation is not required, what else can be done?

Initially, according to the above analysis, the distance between A and B is 1, and the distance between AA and AB is also 1. It seems that the factor array is not available.

Later, I observed the above array: The index of A is 0, the index of AA is 25, the gap between A and A is 0, and the second digit is 1*25; let's look at the BA index 50. The gap between B and A is 1. When the second place is reached, it will become 2*25.

Later, I contacted a previous question: the digit inversion technique used to judge the number of replies.

It seems that there is a bottom line. Write the following code:


// Index a string sorted by length. Ignore the error check.

int encode(const char *str){    int index = *str++ - 'a';    while(*str)        index = 25 * (1 + index) + (*str++ - 'a');    return index;}

After verification, we found it was correct. Well, we don't need to evaluate the split length.

The decoding process is also the inverse process of the above process, but the string must be reversed:

// Reverse the string strvoid reversestr (char * Str) {int I; int Len = strlen (STR); for (I = 0; I <Len/2; ++ I) {char c = STR [I]; STR [I] = STR [Len-1-I]; STR [Len-1-I] = C ;}}
// The Void decode (char * DST, int index) {Int J; char * P = DST; while (index> = 0) character array with DST at least 5 Characters) {* P ++ = 'A' + index % 25; Index = index/25-1;} * P = '\ 0'; reversestr (DST );}

Iv. Reality: in practical application, if such a requirement is met, the strings sorted by length are better, and the Chinese characters used by users within one day are basically limited, use hash to buffer user input strings, which is more efficient.

5. Original Intention: writing this article mainly feels that programming is not so easy, especially the grasp of programming thinking, which is prone to errors. I felt a little uncontrollable, so I wrote down my mind while still remembering it, so that I could view it from time to time in the future. In addition, if you have better ideas, I hope you can tell me.

PS. A few days ago, I encountered the problem of checking the number of input files on a website. I opened a large array, split the numbers one by one into the array, and then checked whether the array was symmetric in sequence; after reading the answer, I suddenly realized it. But I did this answer a year ago, two years ago ..., six years ago, I had already read it again. I forgot it when I saw it, and I forgot to go back to my idea when I first learned programming. At work, the code is easy to write and the Self-feeling is not bad. However, when these problems are encountered, the best solution cannot be analyzed.

Programming is really hard to grasp.

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.