/* Two strings A, B. Remove the characters that exist in B from a. For example a = "Hello World", B = "er", then a becomes "Hllo wold" after culling. The space complexity requirement is O (1), the more time complexity is better. */#include <iostream>#include <string.h>using namespace STD;voidGrial (Char*STR,Char*PTR) {Charsp[ +];//Use 32 char to represent the character string. memset(SP,' + ',sizeof(SP));//Must be zeroed here. Char*p = ptr; while(*p! =' + ') {Sp[*p >>3] |= (0x1<< (*p &0x7)); p++;//Map PTR to 32 char arrays, if present //its position 1. } p = str;Char*q = p; while(*p! =' + ')//start culling. {if(Sp[*p >>3] & (0x1<< (*p &0x7)) {p++; }Else{if(q!=p) *q = *p; q++; p++; }} *q =' + ';}intMain () {CharA[] ="Hello word";CharB[] ="ER"; Grial (A, B);cout<< A << Endl;return 0;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Algorithm title: Culling string (very interesting)