Rotate string left

Source: Internet
Author: User
Description:
In assembly language, there is a shift instruction called ROL. Now a simple task is to simulate the operation result of this instruction using strings. For a given Character Sequence S, please shift its loop left K-bit sequence output. For example, the Character Sequence S = "abcxyzdef" requires that the output result of the loop be shifted three places left, that is, "xyzdefabc ". Is it easy? OK, get it done!
Input:
Multiple groups of test data. Each test data contains a Character Sequence s and a non-negative integer k. The length of S cannot exceed 1000.
Output:

Output a new sequence for each test case.

Package test21; import Java. util. extends; public class main {public static void main (string [] ARGs) {extends = new loads (system. in); While (partial. hasnext () {string STR = success. next (); int n = random. nextint (); system. out. println (revstring (STR, n) ;}/ *** ①: Pay attention to the N value. Because it is a cyclic shift, you can use the remainder method ②: using stringbuffer: to improve performance ** ③: Using substring (beginindex, endindex) to simplify computing * Performance Analysis: time complexity is O (n) --- substring; space complexity Degree is O (n) --- stringbuffer ** @ Param Str * @ Param N * @ return */public static string reverse (string STR, int N) {stringbuffer = new stringbuffer (); int length = Str. length (); int M = n % length; If (M <0) {return STR ;}// you can directly split the substring, then splice the split substrings into stringbuffer. append (Str. substring (M, str. length ())). append (Str. substring (0, M); Return stringbuffer. tostring ();}/*** thought: we still think of the string as composed of two segments, and remember XY. * The left rotation is equivalent to converting the string XY to Yx. We first define a flip operation on the string, * That is, the character order in the flip string. After turning X, record it as XT. Obviously (XT) t = x. ** First, we perform the flip operation on the X and Y segments respectively to obtain the xtyt. * Perform the xtyt flip operation to obtain (xtyt) t = (yt) T (XT) t = Yx. * This is exactly the expected result. * ①: First, the first half is in reverse order. * ②: Then the second half is in reverse order. * ③: Then, the whole is in reverse order. * performance analysis: the time complexity is O (n ), the space complexity is O (1) ** @ Param Str * @ Param N * @ return */public static string revstring (string STR, int N) {If (Str. length () = 0) {return STR;} Char [] C = Str. tochararray (); int M = n % Str. length (); // flip (0, m) characters int firsta = 0, lasta = m-1; while (firsta <lasta) {char temp = C [firsta]; C [firsta] = C [lasta]; C [lasta] = temp; firsta ++; lasta --;} // then (M, str. length flip) int firstb = m, lastb = Str. length ()-1; while (firstb <lastb) {char temp = C [firstb]; C [firstb] = C [lastb]; C [lastb] = temp; firstb ++; lastb --;} // flip all strings to int firstc = 0, lastc = Str. length ()-1; while (firstc <lastc) {char temp = C [firstc]; C [firstc] = C [lastc]; C [lastc] = temp; firstc ++; lastc --;} return new string (c );}}



Rotate string left

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.