百度2013年校招笔试题——pszStringRotate

504 篇文章 0 订阅

转自出处


百度2013校园招聘笔试题 算法与程序设计 第4题:
         字符串左移,void *pszStringRotate(char *pszString, int nCharsRotate),比如ABCDEFG,移3位变DEFGABC,要求空间复杂度O(1),时间复杂度O(n)。

        字符串位移,再简单不过了,方法也很多. 有朴素算法(逐位移动,移动nCharsRotate轮)时间复杂度O(n^2),或者另开一等长新串,计算好原串中各个字符偏移之后在新串中的映射位置,时间复杂度O(n),空间复杂度O(n)。而题目要求空间复杂度O(1),时间复杂度O(n),就必须开辟新思路,想想时空复杂度更优的新算法了。
        我的算法思路如下:
        1.设下标索引cnt为当前在原串操作的下标位置,初始为0。
        2.进行strlen(pszString)-1轮操作(i表示当前是第几次操作):
            1.判断cnt和cnt循环左移i*nCharsRotate个长度位置是否是同一位置,若是,跳转至3。
            2.交换cnt位置和cnt循环左移i*nCharsRotate个长度位置上的字符。
            3.cnt循环右移一个长度,跳转至2。 

        手工模拟一个例子:ABCDEFG,左移3位
            初始:ABCDEFG ,cnt = 0
            交换A和(cnt+7-3%7)%7位置的字符E,得:EBCDAFG
            此时cnt依然等于0,但左移长度变为2*3 = 6
            交换E和(cnt+7-6%7)%7位置的字符B,得:BECDAFG
            同理,第3步,B与F交换,得FECDABG
            第4步,F与C交换,得CEFDABG
            第5步,C与G交换,得GEFDABC
            第6步,G与D交换,得DEFGABC
            算法完成,得到目标串:DEFGABC

        但此例比较特殊,由于(偏移长度%串长度)和串长度互素,所以任意两个交换的位置不会相等,所以cnt得不到更新,一直为0,下面看一个一般的例子:ABCDEFGH,左移4位
            初始:ABCDEFGH,cnt = 0
            交换A和(cnt+8-4%8)%8位置的字符E,得:EBCDAFGH
            进行第2轮操作,发现cnt和(cnt+8-8%8)%8相等,故cnt循环右移,变为1.
            交换B(cnt位置)和(cnt+8-12%8)%8位置的字符F,得:EFCDABGH
            同理,第4步,C与G交换,得:EFGDABCH
            第5步,位置相同,cnt循环右移
            第6步,D与H交换,得:EFGHABCD
            第7步,位置相同,cnt循环右移
            算法完成,得到目标串:EFGHABCD

C++实现代码如下:

  1. #include <cstdio>   
  2. #include <cstring>   
  3.   
  4. using namespace std;  
  5.   
  6. char str[1010];  
  7. int  n;  
  8.   
  9. void swap(char *c1, char *c2) {  
  10.     char tmp = *c1;  
  11.     *c1      = *c2;  
  12.     *c2      = tmp;  
  13. }  
  14.   
  15. void pszStringRotate(char *pszString, int nCharsRotate) {  
  16.     int len   = strlen(pszString);  
  17.     int cnt   = 0;                  // Current operate position on pszString   
  18.     int shift = nCharsRotate % len; // Current shift length   
  19.     for (int i = 1; i <= len - 1; i ++) { // Operation times upper bound : len - 1   
  20.         if (cnt == (cnt + len - shift) % len) { // Collision found   
  21.             cnt ++;    // Update current operate position   
  22.             cnt %= len;  
  23.         }  
  24.         swap(&pszString[cnt], &pszString[(cnt + len - shift) % len]); // Do operate   
  25.         shift += nCharsRotate; // Update shift length   
  26.         shift %= len;  
  27.     }  
  28. }  
  29.   
  30. int main () {  
  31.     while (~scanf("%s%d", str, &n)) {  
  32.         pszStringRotate(str, n);  
  33.         printf("%s\n", str);  
  34.     }  
  35.     return 0;  
  36. }  
#include <cstdio>
#include <cstring>

using namespace std;

char str[1010];
int  n;

void swap(char *c1, char *c2) {
    char tmp = *c1;
    *c1      = *c2;
    *c2      = tmp;
}

void pszStringRotate(char *pszString, int nCharsRotate) {
    int len   = strlen(pszString);
    int cnt   = 0;                  // Current operate position on pszString
    int shift = nCharsRotate % len; // Current shift length
    for (int i = 1; i <= len - 1; i ++) { // Operation times upper bound : len - 1
        if (cnt == (cnt + len - shift) % len) { // Collision found
            cnt ++;    // Update current operate position
            cnt %= len;
        }
        swap(&pszString[cnt], &pszString[(cnt + len - shift) % len]); // Do operate
        shift += nCharsRotate; // Update shift length
        shift %= len;
    }
}

int main () {
    while (~scanf("%s%d", str, &n)) {
        pszStringRotate(str, n);
        printf("%s\n", str);
    }
    return 0;
}



        算法正确性简单证明:
             第1次操作,cnt位置上的字符需要循环左移nCharsRotate个长度以达到正确位置,如果这两个位置是相等的,说明cnt位置上的字符不需要操作了,循环右移cnt(由于从0开始,可以保证其不重复)。如果这两个位置不相等,交换这两个位置的字符,此时cnt位置上的字符是新字符,这个新字符被相对循环右移了nCharsRotate个长度,而要使其达到正确位置,则需要循环左移2*nCharsRotate个长度。可以归纳出第i轮操作需要循环左移i*nCharsRotate个长度。操作完第strlen(pszString)-1轮操作即可终止,因为此轮操作只有两种可能:1.与自身交换,则最后一个字符也与自身交换,不需要额外操作;2.与最后一个字符交换,则最后一个字符在此轮操作已经被移动到了正确的位置,不需要额外操作。综上,不需要第strlen(pszString)次操作。

            算法应该还可以进一步优化,比如先判断是否互素,再进行对应操作,终止轮数也相应减少,会少一些无谓的判断,现在的strlen(pszString)-1轮只是上界,真正的交换次数与上述两个量的最大公约数有关。

        此外,本题还有利用串翻转性质的高效算法,本人才疏学浅,就不再详述了

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值