Algorithm title: Culling string (very interesting)

Source: Internet
Author: User

/* 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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.