演算法題:剔除字串(很有意思)

來源:互聯網
上載者:User

標籤:字串剔除

/*兩個字串A、B。從A中剔除存在於B中的字元。比如A = “hello world”, B = "er",那麼剔除之後A變為"hllo wold"。空間複雜度要求是O(1),時間複雜度越優越好。*/#include <iostream>#include <string.h>using namespace std;void Grial(char *str,char *ptr){    char sP[32];//用32個char表示字元表。    memset(sP,‘\0‘,sizeof(sP));//這裡必須清零。    char *p = ptr;    while (*p != ‘\0‘)    {        sP[*p >> 3] |= (0x1 << (*p & 0x7));        p++;        //將ptr映射到32個char型的數組裡面,如果存在就將        //其位置1。    }    p = str;    char *q = p;    while (*p != ‘\0‘)//開始剔除。    {        if (sP[*p >> 3] & (0x1 << (*p & 0x7)))        {            p++;        }        else        {            if (q!=p)            *q = *p;            q++;            p++;        }    }    *q = ‘\0‘;}int main(){    char A[] = "hello word";    char B[] = "er";    Grial(A, B);    cout << A << endl;    return 0;}

著作權聲明:本文為博主原創文章,未經博主允許不得轉載。

演算法題:剔除字串(很有意思)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.