Analysis of a C # Algorithm Analysis Solution in midea0978 (2)

Source: Internet
Author: User
Tags string indexof

I. Preparations
First, process the following calc functions for easy viewing. In fact, it is the renaming of variables, thanks to the help of vs2005.
Input string STR, which is a byte array reference;
I is the cyclic variable of the byte array.ProgramUsing the Big K as the location variable of the byte array.
K limits the maximum string to 18. The original text is k <90, K + = 5, which is 5 times smaller;
N is actually the position of the character in the orders table. The orders table is actually a password table;
J, the inexplicable J. It actually happens to be thisAlgorithm.

1 Public   Static   Void Calc ( String STR, Ref   Byte [] BTS)
2 {
3 Ushort J =   11 ;
4 Uint I =   0 ;
5 Uint K =   0 ; // (Uint) math. Min (BTS. length, 18 );
6 Int M =   0 ;
7 While (I < BTS. Length && K <   18 )
8 {
9 If ( ! Jlist. Contains (j) jlist. Add (j );
10
11 Int N = Calcnum (STR [m ++ ]);
12 If (N =   - 1 ) Throw   New Exception ( " Error " );
13 // N = n <(J % 32 );
14 // J. The maximum possible value is (8-1) + 3 = 10, and the initial value is 10. Therefore, the actual maximum value is 11.
15 // N = n <J;
16 // BTS [I] = convert. tobyte (BTS [I] | (N/256 ));
17 // BTS [I + 1] = convert. tobyte (BTS [I + 1] | (N % 256 ));
18 If (J <   8 )
19 {
20 // J = 3 ~ 7, n = 0 ~ 31, n <j = 0 ~ 31 <7, N/256 = n> 8 = 0 ~ 15
21 N = N < J;
22 BTS [I] = Convert. tobyte (BTS [I] | (N /   256 ));
23 BTS [I +   1 ] = Convert. tobyte (BTS [I +   1 ] | (N %   256 ));
24 J + =   3 ;
25 I ++ ;
26 }
27 Else
28 {
29 // N = n <J;
30 // BTS [I] = convert. tobyte (BTS [I] | (N/256 ));
31 N = N < (J -   8 );
32 // N = 0 ~ 31 <3 = 0 ~ 255
33 BTS [I] = Convert. tobyte (BTS [I] | N );
34 // Shift left more than 8 bits, so the 8 bits on the right must be 0, and then the BTS [I + 1] is 0
35 // BTS [I + 1] = convert. tobyte (BTS [I + 1] | (N % 256 ));
36 J -=   5 ;
37 }
38 K + =   1 ;
39 }
40 }

 

Ii. Constant sequence J
Slave Code As J keeps getting bigger and smaller, there must be a cyclical process. The following describes this point first:
The change of J is like an iterative function. If you know any J, you can get the next one;
In addition, for two identical J, the next J must be equal;
At the same time, J is limited to a limited range, 3 ~ 10 (8-5 = 3, (8-1) + 3 = 10 ),
Therefore, as the number increases, J is bound to be a loop.
After calculation, the initial vector is 11 and the loop section is [6, 9, 4, 7, 10, 5, 8, 3].
The following table shows that:
ID-N J K
0 21 43008 11 0
1 11, 704, 6, 0
2 27 13824 9 1
3 19, 304, 4, 1
4 1 128 7 2
5 25, 25600, 10, 3
6 18 576 5 3
7 17 4352 8 4
8 12 96 3 4
9 16 1024 6 5
10 4 2048 9 6
11 3 48 4 6
12 28 3584 7 7
13 5 5120 10 8
14 9 288 5 8
15 0 0 8 9
16 10 80 3 9
17 22, 1408, 6, 10

Iii. view the relationship between strings and byte arrays from J
Only found that for J, less than 8 and greater than or equal to 8 are two completely different situations.
The location variable of the byte array is added only when J is less than 8, but not 8 if J is greater than or equal to 8,
Therefore, this is the key to having more characters than bytes.
Because the J sequence has been determined, as long as the number of characters is specified, their corresponding J is determined,
Then it is easy to determine which character corresponds to the nth byte (position K.
The relationship between J and K is displayed in the data table above.

Iv. Simplified code
In the source code, calcnum is actually used to find the position of a string in the orders table. In fact, you can use the string indexof function.
Split the three core statements into if (I <8:
N = n <(J & 0x1f );
BTS [I] = convert. tobyte (BTS [I] | (N & 0xff00)> 8 ));
BTS [I + 1] = convert. tobyte (BTS [I + 1] | (N & 0xff ));
When J> = 8, n first shifts the J-bit left, so the N & 0xff of the third sentence must be 0, therefore, modifying this sentence of BTS [I + 1] is meaningless for j> = 8.

5. Construct class objects
To establish the relationship between various parameters, a charobject class corresponds to a character of a string.
Member: ID (serial number), next (next object), J (constant sequence), n (position of Characters in the password table ),
N_hight (high), n_lower (low), C (character), BTS (corresponding array), and K (corresponding to the position in the array ).
Various computing logics have been integrated into attributes.
When the next attribute is specified, the J and K of the next object can be calculated immediately;
When specifying the K attribute, you can calculate the BTS [k] and BTs [k + 1] based on the current N;
If n changes, you can call cal to recalculate BTS [k] and BTs [k + 1].
The static method calc obtains the byte array based on the string; The recalc obtains the string based on the byte array, that is, the inverse calculation.

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.