Here the rotate operation refers to cyclic shift. For example, if the string "abcdefg" is rotated in the center of D, it is equivalent to moving the string to the left until the first element is d, and the new string "defgabc" is obtained ".
To conveniently perform the rotate operation, a common technique is as follows: first reverse the first half and then reverse the second half, finally, you can reverse the entire string (the first half and the second half are divided by the rotation center ). Take the string "abcdefg" and take D as the center rotation as an example. Take D as the split point, reverse the first half and the second half respectively, and get "cbagfed ", finally, the entire string is reversed to "defgabc ".
This algorithm has been mentioned in many books and I believe everyone will be familiar with it. So how efficient is it? Assuming the length of the string is t, it takes about t swap operations to complete the entire rotation process, that is, it takes about 3 T value assignment, at the same time, only the temporary space of the constant is required. Because of its simple implementation, it is still unsatisfactory. Therefore, sgi stl uses this algorithm when processing two-way iterator containers.
However, looking at the STL source code, we can find that sgi stl adopts another algorithm when processing strings stored in containers with random access capabilities, the principle of this algorithm is ignored by hjj in STL source code analysis, so I will simply sort it out here.
First, post the source code of this algorithm:
template < class RandomAccessIterator, class Distance > void __rotate(RandomAccessIterator first, RandomAccessIterator middle, RandomAccessIterator last, Distance * , random_access_iterator_tag) { Distance n = __gcd(last - first, middle - first); while (n -- ) __rotate_cycle(first, last, frist + n, middle - first, value_type(first)); } template < class EuclideanRingElement > EuclideanRingElement __gcd(EuclideanRingElement m, EuclideanRingElement n) { while (n != 0 ) { EuclideanRingElement t = m % n; m = n; n = t; } return m; } template < class _RandomAccessIterator, class Distance, class T > void __rotate_cycle(RandomAccessIterator first, RandomAccessIterator last, RandomAccessIterator initial, Distance shift, T * ) { T value = * initial; RandomAccessIterator ptr1 = initial; RandomAccessIterator ptr2 = ptr1 + shift; while (ptr2 != initial) { * ptr1 = * ptr2; ptr1 = ptr2; if (last - ptr2 > shift) ptr2 += shift; else ptr2 = first + (shift - (last - ptr2)); } * ptr1 = value; }
The preceding three functions are involved: __rotate, _ GCD, and _ rotate_cycle. The last two functions are easy to understand: __gcd is nothing to say, XXX. Of course, it is to find the maximum common number.
_ Rotate_cycle is an element starting from an initial element and replacing it with an element with a fixed distance. If there is not enough offset distance behind it, return to the header for further computation (equivalent to modulus ). Until a replacement circle is formed.
Now let's take a closer look at the _ rotate function. This function is actually not complicated. We just need to partition the three rows: first, we can find the maximum public appointment between the offset distance and the total length of the string, and then loop it for n times, perform the _ rotate_cycle operation based on the first n elements of the string respectively, over. But how can this ensure that the rotate operation on the input string can be completed correctly?
This involves a small Theorem in number theory: if there are two positive integers m, n, and gcd (m, n) = d, then the sequence {M % N, 2 m % N, 3 M % N ,..., NM % n} must be {0, D, 2d ,..., n-d} Of a certain arrangement and repeated d times, where % represents a modulo operation.
For example, if M = 6, n = 8, D = gcd (m, n) = 2, then {6% 8, 12% 8, 18% 8 ,..., 48% 8} indicates an arrangement of {,} And repeats it twice. In fact, it is {, 2, 0 }. In particular, if m, n, D = 1, then the sequence {M % N, 2 m % N, 3 M % N ,..., (n-1) M % n} is actually {1, 2, 3 ,..., n-1.
The process of proving this theorem can be found in many books (for example, section 4.8 of mathematics.
After understanding this theorem, we can easily see the meaning of the _ rotate function. If the maximum number N obtained in the first step is 1, only one _ rotate_cycle operation is required to traverse all elements, and correctly replace each element with an element that is located at a certain distance from each other, so the cyclic left shift operation is completed. If n is greater than 1, each _ rotate_cycle can only shift the T/N element to the left, where T is the total length of the string, the moving elements take D as the Equi-spacing, so they are cycled n times, and the _ rotate_cycle operation starts from the first n elements of the string, respectively, this ensures that all elements are moved to the correct position.
In this new algorithm, every _ rotate_cycle requires T/N + 1 assignment and N cycles. Therefore, only T + N assignment operations are required, obviously, it is much faster than the three-way inversion algorithm mentioned above.
For example, if string a = {1, 2, 3, 4, 5} is shifted to the left by two places, that is, the string {3, 4, 5, 1, 2} is expected to be obtained }, the assignment process of this algorithm is as follows:
TMP = A [0]-> TMP = 1
A [0] = A [2]-> {3, 2, 3, 4, 5}
A [2] = A [4]-> {3, 2, 5, 4, 5}
A [4] = A [1]-> {3, 2, 5, 4, 2}
A [1] = A [3]-> {3, 4, 5, 4, 2}
A [3] = TMP-> {3, 4, 5, 1, 2}
Here, because of the two and five mutual elements, the assignment is completed for six times. If the three-way flip algorithm is used, the assignment is required for about three * five times (actually 12 times ).
Http://www.cnblogs.com/atyuwen/archive/2009/11/08/rotate.html