1 /*2 Title Description:3 for a string, and a position in the string, design an algorithm that moves the left part, including the I position, to the right,4 move the right part to the left. Given the string a and its length n and the specific position p, return the rotated result. 5 Test Examples:6 "Abcdefgh", 8,47 return: "FGHABCDE"8 */9 /*Ten eg:a = "Abcdefgh", n = 8, p = 4 One first, A is reversed, a = "HGFEDCBA"; A then put [0,n-1-p-1] and [N-1-p, n-1] respectively anti-war: - get FGHABCDE. - The time complexity is O (n) and the spatial complexity is O (1). the use the reverse function to invert. - */ -#include <iostream> -#include <string> + using namespacestd; - + stringRotatestring (stringAintNintp) { A if(N <=0|| P <=0|| P >=N) at return ""; - Reverse (A.begin (), A.end ()); -Reverse (A.begin (), A.begin () +n-1-p); -Reverse (A.begin () +n-1-p, A.end ()); - returnA; - } in - intMain () { to stringA; +CIN >>A; -cout << rotatestring (A,a.length (),4); the return 0; *}
1-5-Rotation of the string