Lexicographic Order (Computer Algorithm and Analysis 1-2 Wang Xiaodong)

Source: Internet
Author: User

Problem description:

Special strings must be encoded in data encryption and data compression. the given alphabet A consists of 26 lower-case English letters, that is, a = {A, B... z }. the

The long-order string generated by the alphabet indicates that letters appear from left to right in the same order as letters appear in the alphabet, and each character has the largest

Appears once more. for example, a, B, AB, BC, XYZ, and other strings are ascending strings. now all ascending strings of the alphabet A with a length not greater than 6 are represented by words.

The Code is as follows:

1 2 3... 26 27 28...

A B c... z AB ac...

For an ascending string of no more than 6 characters, the encoding of the string in the preceding dictionary is quickly calculated.

Analysis:

The calculation takes two steps:

Given a string, assuming there are K digits

<1> calculate the sum of all strings smaller than k characters.

<2> the computation is equal to k bits, but the encoding is smaller than its sum.

Set the ascending string digit f (I, k) starting with the I character and the total number of ascending strings of the length k to G (K), then

G (K) = accumulate f (I, k), I from 1 to 26

F (I, 1) = 1G (1) = accumulate f (I, 1), I from 1 to 26

F (I, 2) = accumulate F (J, 1) = 26-i, J from I + 1 to 26G (2) = accumulate f (I, 2) = accumulate (26-i), I from 1 to 26

In general, there are:

F (I, K) = accumulate F (J, k-1), J from I + 1 to 26

G (K) = accumulate f (I, K) = accumulate I (accumulate J, F (J, k-1), I from 1 to 26, J from I + 1 to 26

The Code is as follows:

 

# Include "stdafx. H "# include <iostream >#include <string> using namespace STD; int F (int I, int K) {/* starts with the I character, the number of characters in ascending order */INT sum = 0; If (k = 1) return 1; for (Int J = I + 1; j <= 26; j ++) sum + = f (J, k-1); Return sum;} int g (int K) {// String Length of K int sum = 0; For (INT I = 1; I <= 26; I ++) {sum + = f (I, k);} return sum;} int calculate (string s) {int sum = 0; int K = S. length (); For (INT I = 1; I <K; I ++) sum + = g (I ); int H = s [0]-'A' + 1; // The first letter for (INT I = 1; I 

Related Article

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.