Topic Description:
In assembly language There is a shift instruction called cyclic left-shift (ROL), now has a simple task, is to use strings to simulate the operation of this instruction results. For a given sequence of characters s, ask you to shift its loop left K-bit after the sequence output. For example, the character sequence s= "Abcxyzdef", requiring the output loop to move left 3 digits after the result, namely "XYZDEFABC". Is it simple? OK, take care of it!
Input:
Multiple sets of test data, each containing a character sequence s and a nonnegative integer k. The length of which s is not more than 1000.
Output:
corresponding to each test case, output a new sequence.
Sample input:
Udboj 4
ABBA 1
Sample output:
Judbo
Bbaa
This problem was previously done on the cracking the coding interview similar, the same three times inversion, you can get, time complexity of O (n), space complexity of O (1). This problem in nine degrees OJ test, mainly to consider moving the number of digits greater than the length of the string, the rest can be.
The AC code is as follows:
#include <stdio.h> #include <string.h> void Swap (char *a,char *b) {int temp = *a;
*a = *b;
*b = temp;
} * * This part of the flip string from start to end/void ReverseString (char *str,int start,int end) {while (Start < end)
{Swap (&str[start],&str[end]);
start++;
end--; }/* Find the characters after the left K bit more highlights of this column: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/sjjg/*/void rolstring
(Char *str,int k)
{if (str = NULL | | k<=0) return;
int len = strlen (str);
To consider K greater than Len's case k = K%len;
if (k = = 0) return;
ReverseString (str,0,k-1);
ReverseString (str,k,len-1);
ReverseString (str,0,len-1);
int main () {char str[1010];
int k;
while (scanf ("%s%d", str,&k)!= EOF) {rolstring (str,k);
Puts (str);
return 0; }