Sword refers to the offer series source-left rotation string

Source: Internet
Author: User

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

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.