劍指offer系列源碼-左旋轉字串

來源:互聯網
上載者:User

標籤:blog   io   ar   os   sp   on   資料   2014   art   

題目1362:左旋轉字串(Move!Move!!Move!!!)時間限制:2 秒記憶體限制:32 兆特殊判題:否提交:1577解決:669題目描述:組合語言中有一種移位指令叫做迴圈左移(ROL),現在有個簡單的任務,就是用字串類比這個指令的運算結果。對於一個給定的字元序列S,請你把其迴圈左移K位後的序列輸出。例如,字元序列S=”abcXYZdef”,要求輸出迴圈左移3位後的結果,即“XYZdefabc”。是不是很簡單?OK,搞定它!輸入:多組測試資料,每個測試資料包含一個字元序列S和非負整數K。其中S的長度不超過1000。輸出:對應每個測試案例,輸出新序列。範例輸入:UDBOJ 4abba 1範例輸出:JUDBObbaa


輸入的k可能大於字串長度,故做取餘操作。

#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;}


劍指offer系列源碼-左旋轉字串

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.