Problem description: Special strings must be encoded in data encryption and data compression. The given alphabet consists of 26 lower-case letters. The ascending string generated by the English alphabet indicates that letters appear in the same order as letters appear in the English alphabet from left to right, and each character can appear at most once. For example, a, B, AB, bc, and xyz are all ascending strings. Now all the ascending strings generated in the alphabet with a length not greater than 6 are listed in lexicographically and encoded as follows:
1 |
2 |
... |
26 |
27 |
28 |
... |
A |
B |
... |
Z |
AB |
Ac |
... |
For any ascending character with a length not greater than 6, the encoding in the preceding dictionary is quickly calculated.
Task: Calculate the encoding of a given ascending string with a length not greater than 6 in the preceding dictionary.
Solution:
The key to this question is to correctly understand the Lexicographic Order given in the topic description. The key is to first show a string with a length of 1, then a string with a length of 2 ,....... In a string of the same length, it is executed in lexicographically. For example, for a string cfkp with a length of 4, if you can calculate the number of strings before it, add 1 to get the encoding of the string. The strings listed before the string can be analyzed as follows:
1) a string of 1, a string of 2, and a string of 3;
(2) In a 4-character string starting with the letter c, strings starting with cd, ce, and 4 are also arranged in front of the string; in the string with the cf header 4, the string with the cfg, cfh, cfi, and cfj headers 4 is arranged before the string; in the string with the cfk header, A string with the header length of cfkl, cfkm, cfkn, and cfko is also in front of it.
Analysis of 2nd cases can be divided into the following situations:
The number of string headers starting with cd and ce with a length of 4 is the same as the number of string headers starting with d and e with a length of 3. Other cases can be described as follows: the number of strings starting with g, h, I, and j; the number of strings starting with l, m, n, o, and 1.
Analyze the above situation and summarize the rules. The number of strings to be calculated can be divided into two types:
1) Number of strings with a length of k, expressed by g (k;
2) It is a string with a length of k and indicated by count1 (ch, I.
Apparently, there are
Count2 (k) = sum _ {ch = 1} ^ 26 count1 (ch, k)
Likewise, the following rules can be found:
Count1 (ch, 1) = 1
Count1 (ch, k) = sum _ {I = ch + 1} ^ 26 count1 (I, k-1)
On this basis, you can calculate the encoding of each string.
The reference procedure is as follows:
Import java. util. ArrayList;
Import java. util. HashMap;
Import java. util. List;
Import java. util. collections;
Public class Section1_2_1 {
/**
* TODO
*
* @ Author LiuYong
* @ Version 2013-9-3 2:52:17
*/
Public static void main (String [] args ){
Repeated scan = new partition (System. in );
System. out. println ("Enter the string to be compared ");
String s = "";
S = scan. next ();
Int sum1 = 0;
If (s. length ()> 6 | s. equals ("") | s. length () = 0 ){
System. out. println ("the length of the input string should not exceed 6 ");
} Else {
Sum sum = new Sum ();
Classify c = new Classify ();
List <Classify> list = new ArrayList <Classify> ();
List = c. Sort (s );
System. out. println (sum. sum1 (list) + "" + sum. sum2 (list) + "" + sum. sum3 (list ));
Sum1 = sum. sum1 (list) + sum. sum2 (list) + sum. sum3 (list );
System. out. println (sum1 );
}
}
}
/**
* Process the input string and treat each letter as an object
* @ Author LiuYong
* @ Version 10:34:37
*/
Class Classify {
Int length = 0; // length, used for Loop
Double sum = 0; // quantity
Int num = 0; // The first letter number, which is processed by map.
List <Classify> list;
// No parameter Constructor
Public Classify (){
}
// Constructor with Parameters
Public Classify (int len, int num ){
This. length = len;
This. num = num;
}
/**
* Encapsulate each letter in a string as an object and return it to the linked list. The encoding position of each letter is 1-26) and the length of subsequent strings.
* Records the attributes of the object.
*
*
* @ Author LiuYong
* @ Version 2013-9-2 1:41:52 pm
*/
Public List <Classify> Sort (String st ){
List = new ArrayList <Classify> ();
HashMap <Character, Integer> map = new HashMap <Character, Integer> ();
For (int I = 1; I <= 26; I ++ ){
Map. put (char) (97 + I-1), I); // used to determine the location of a -- z26 letters, that is, encoding 1-26 ), in this example, key-value pairs are used for one-to-one correspondence.
}
Char [] ch = new char [st. length ()]; // creates an array of characters whose length is the string length.
For (int I = 0; I <st. length (); I ++ ){
Ch [I] = st. charAt (I); // put the characters at the corresponding position in the array
Classify linoleic = new Classify (st. length ()-i-1, map. get (ch [I]);
List. add (linoleic );
}
Return list;
}
}
/**
* Core computing
* @ Author LiuYong
* @ Version 10:44:36
*/
Class Deal {
/**
* Calculates the number of strings with a letter k headers not exceeding len.
*
* @ Author LiuYong
* @ Version 2013-9-3 3:48:11
*/
Public int count1 (int I, int len ){
If (len = 1)
Return 1;
Else {
Int sum = 0;
Int j;
For (j = I + 1; j <= 26; j ++ ){
Sum + = count1 (j, len-1 );
}
Return sum;
}
}
/**
* Calculate the number of strings whose length cannot exceed len.
*
* @ Author LiuYong
* @ Version 2013-9-3 3:50:35
*/
Public int count2 (int len ){
Int sum = 0;
For (int I = 1; I <= 26; I ++)
Sum + = count1 (I, len );
Return sum;
}
}
/**
* Summation class
* @ Author LiuYong
* @ Version 10:44:07
*/
Class Sum {
Deal d = new Deal ();
/**
* Calculate the number of characters whose length is less than the string length.
*
* @ Author LiuYong
* @ Version 2013-9-3 3:58:57
*/
Public int sum1 (List <Classify> list ){
Int sum = 0;
For (int I = 1; I <list. size (); I ++ ){
Sum + = d. count2 (I );
}
Return sum;
}
/**
* The length is the string length but before the first character.
*
* @ Author LiuYong
* @ Version 2013-9-3 4:00:51
*/
Public int sum2 (List <Classify> list ){
Int sum = 0;
For (int I = 1; I <list. get (0). num; I ++ ){
Sum + = d. count1 (I, list. size ());
}
Return sum;
}
/**
* Characters with the same initial character and no longer than the string length
* @ AuthorLiuYong
* @ Version 2013-9-3 7:18:33
*/
Public int sum3 (List <Classify> list ){
Int sum = 0;
For (int I = 1; I <list. size (); I ++ ){
Int len = list. get (I-1). length;
Int temp1 = list. get (I-1). num;
Int temp2 = list. get (I). num;
For (int j = temp1 + 1; j <temp2; j ++ ){
Sum + = d. count1 (j, len );
}
}
If (list. size () = 1 ){
Return sum;
} Else {
Return sum + 1;
}
}
}