[Programmer programming art] Learning Record 2: Circular shift of left-rotated strings

Source: Internet
Author: User
[Programmer programming art] Learning Record 2: Circular shift of left-rotated strings

GCD algorithm: (moving phase division/Euclidean Algorithm)
GCD is the algorithm used to calculate the maximum public approx. It is the first algorithm of taocp.
GCD algorithm flow:
First, given two integers m, n (m greater than or equal to N) if less than then directly exchange and then process
① Calculate the remainder r = m % N
② If r = 0, the algorithm ends, and N is the result.
Otherwise, re-order m <-n, n <-R and then loop
<<<
Rotate Algorithm in STL:
The following method can be used for the array shift problem:
① Dynamically allocate an array of the same length, copy the data to the array, change the order, and then copy back to the original array
② Using three-way reverse exchange, the first part of the sequence is in reverse order, then the second half is in reverse order, and then all the whole sequence is in reverse order. BA = (BR) ^ t (AR) ^ t = (arbr) ^ t
③ Group switching-and the result that may be the first several consecutive numbers of Arrays
For example, if the length of a is less than B, divide AB into a0a1b, exchange A0 and B, and get ba1a0. You only need to switch A1 and A0. If the length of a is less than B, divide AB into ab0b1, exchange A and B0 to obtain b0ab1. You only need to exchange a and B0.
We consider that the above group exchange idea is very similar to our GCD algorithm.
④ All sequence numbers are (J + I * m) % N (J indicates the start position of each loop chain, I indicates the Count variable, M indicates the left rotation digits, and N indicates the length of the string) in this way, a loop chain (gcd (n, m) will be formed. The elements on each loop chain can be exchanged n times as long as they move one position.
<<<
Rotate string left
Discussion:
1) if m and n are prime numbers)

for i = 0:n-1    k = i * m % n;end

Example: M = 3, n = 4 ABCD-> dabc
Ch [0]-> temp, CH [3]-> CH [0], CH [2] = CH [3], CH [1] [2], temp-> CH [1]
The K search column is, and found that this is the sequence of values assigned in sequence above.
2) If m and n are not mutually prime numbers, we need to divide them into independent loops with all serial numbers (J + I * m) % N (J is an integer between 0 and gcd (n, m)-1, I = 0: N-1) will constitute a loop chain, a total of gcd (n, (m) cycle chain. You can perform an internal loop for each cycle chain.

Void rotate (string & STR, int m) {int lenofstr = Str. length (); int numofgroup = gcd (lenofstr, m); // calculate the maximum approximate number int eleminsub = lenofstr/numofgroup; // The number of external loops J is the number of loops for (Int J = 0; j <numofgroup; j ++) {char TMP = STR [J]; for (INT I = 0; I <eleminsub-1; I ++) // The number of inner cycles I is the number of elements in each loop chain, N/gcd (m, n) times; STR [(j + I * m) % lenofstr] = STR [(j + (I + 1) * m) % lenofstr]; STR [(j + I * m) % lenofstr] = temp ;}// rewrite void my_rotate (char * begi N, char * mid, char * End) {int n = end-begin; int K = mid-begin; int d = gcd (n, k); int I, J; for (I = 0; I <D; I ++) {int temp = begin [I]; int last = I; // I + k is the position where I shifts right K, % N: When I + k> N, start from left again for (j = (I + k) % N; J! = I; j = (J + k) % N) {begin [last] = begin [J]; last = J;} begin [last] = temp ;}}

Example:
1. For example, five students, whose numbers start from 0, that is, 0, 1, 2, and 4. The instructor reports the number. The rule is to start from the first student and separate the number of students. The student ID must be 0 2 4 1 3. Here it is equivalent to I 0, K is 2, n is 5; 2. then the teacher said, "If the number is 0, the other students will go to the student's position in the previous Report, then the student starts from 0 1 2 3 4 = "2 3 4 _ 1. Finally, the teacher said, number 0 to the remaining space to get the final ranking 2 3 4 0 1. In this case, the result is actually equivalent to moving K = 2 places left in the above program. As for why the students numbered 0 are listed. Actually, this sentence is: int last = I; because we want to achieve this effect 0 1 2 3 4 => 2 3 4
0 1, SO 2 3 4 must be moved to the front.
Further understanding is needed here.
<<
Three-Step flip:
Invert x ^ t x = "ABC" x ^ t = "CBA" (x ^ ty ^ t) ^ t = Yx



Reference code:

// Triplicate method. cppchar * invert (char * Start, char * End) {char TMP, * pTMP = start; while (start! = NULL & End! = NULL & start <End) {TMP = * start; * Start = * end; * end = TMP; Start ++; end --;} return pTMP ;} char * Left (char * s, int POS) // POS is the number of characters to rotate {int Len = strlen (s); invert (S, S + (pos-1 )); // ABC-> CBA invert (S + POs, S + (len-1); // def-> fed invert (S, S + (LEN-1 )); // cbafed-> defabc return s ;}

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.