Topic 1362: Left spin string (move! move!! Move!!! ) time limit: 2 seconds memory limit: 32 trillion special sentence: No submission: 1577 resolution: 669 title Description: There is a shift instruction in assembly language called Cyclic Left (ROL), now there is a simple task, is to use a string to simulate the results of the operation of this instruction. For a given character sequence s, you turn the output of the sequence to the left of the K-bit after it is moved. For example, the character sequence s= "Abcxyzdef", which requires the output loop to move left 3 bits after the result, i.e. "XYZDEFABC". Isn't it simple? OK, Fix it! Input: Multiple sets of test data, each of which contains a character sequence s and a nonnegative integer k. where S is not more than 1000 in length. Output: Corresponds to each test case, outputting a new sequence. Sample input: Udboj 4abba 1 Sample output: Judbobbaa
The input k may be greater than the string length, so take the remainder operation.
#include <iostream> #include <stdio.h> #include <string.h>using namespace std;void reversestr (char* Begin,char*end) {if (begin==null| | End==null) {return; } while (Begin<end) {swap (*begin,*end); begin++; end--; }}char* leftrotatestring (char* pdata,int N) {if (pdata!=null) {int length =strlen (pData); if (length>0&&n>0&&n<length) {char* pfirststart = PData; char* pfirstend = pdata+n-1; char* Psecondstart = pdata+n; char* psecondend = pdata+length-1; Reversestr (Pfirststart,pfirstend); Reversestr (Psecondstart,psecondend); Reversestr (Pfirststart,psecondend); }} return pData;} int main () {char data[1001]; int n; while (scanf ("%s%d", Data,&n)!=eof) {printf ("%s\n", Leftrotatestring (Data,n%strlen (data))); } return 0;}
Sword refers to the offer series source-left rotation string