/*
两个字符串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; }

posted on 2017-07-11 14:55  yutingliuyl  阅读(255)  评论(0编辑  收藏  举报