码迷,mamicode.com
首页 > 编程语言 > 详细

算法题:剔除字符串(非常有意思)

时间:2017-07-11 16:02:00      阅读:237      评论:0      收藏:0      [点我收藏+]

标签:word   set   com   存在   clu   markdown   else   []   iostream   

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

算法题:剔除字符串(非常有意思)

标签:word   set   com   存在   clu   markdown   else   []   iostream   

原文地址:http://www.cnblogs.com/yutingliuyl/p/7150530.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!