字符串A中删除字符串B中所有字符

本文介绍了一种高效的算法,用于从一个字符串中移除另一个字符串的所有字符。通过使用哈希表和双指针技巧,该算法能在O(n)的时间复杂度内完成任务。
部署运行你感兴趣的模型镜像

一、问题描述

输入两个字符串,从第一字符串中删除第二个字符串中所有的字符。
例如,输入A串”They are students.”和B串”aeiou”,则删除之后的第一个字符串变成”Thy r stdnts.”

二、问题分析

此题分为两个部分来解答
    1)查找在A串中出现的B串字符
    2)删除A串中的字符
对于第1)个问题,能做到快速查找的方法是二分和哈希,这里明显用哈希更合适,考虑到是字符,因此设置它们的asc11编码作为键,将B串录入哈希表中,值为0或者1。0表示该字符没有在B串中,1表示该字符在B串中;
对于第2)个问题,考虑到每删除一个i位置上的字符,需将i+1后面的字符往前挪,这样在最差的情况下,会挪动(n-1+n-2+...+1)次,复杂度为O(n^2),因此要想一个聪明的方法来降低复杂度。


这里采用前后指针的方式。
初始状态:*pSlow和*pFast指针都指向字符串串首位置
    1)当A[*pFast]在哈希表中的值为0,说明该字符没有出现在B中,可将此字符纳入新串里(新串相当于用*pSlow在原串中做覆盖):A[*pSlow]=A[*pFast]; pSlow++ && pFast++
    2)当A[*pFast]在哈希表中的值为1,说明该字符出现在B中,则跳过此值:pFast++
    3)当pFast走到串尾的时候,新串(覆盖串)记得加'\0',*pSlow = '\0'

三、解题算法

/*******************************
author:tmw
date:2018-10-22
*******************************/
#include <stdio.h>
#include <stdlib.h>

char* deleAllElemInStrB(char* A, char* B)
{
    if(A == NULL || B == NULL) return NULL;

    /**建立长度为256的哈希表,并做初始化**/
    int* hash_table = (int*)malloc(256*sizeof(int));
    int i;
    for(i=0; i<256; i++)
        hash_table[i] = 0;

    /**将B表元素存入hash表中**/
    char* temp = B;
    while(*temp != '\0')
    {
        hash_table[(int)(*temp)] = 1;
        temp++;
    }
    //定义俩指针,初始状态都指向串首
    char* pSlow = A;
    char* pFast = A;
    while(*pFast != '\0')
    {
        /**如果*pFast指向的元素不在哈希表中,则纳入新的覆盖串**/
        if(hash_table[(int)(*pFast)] != 1)
        {
            *pSlow = *pFast;
            pSlow++;
            pFast++;
        }
        else
            pFast++;
    }
    /**记得新覆盖串补上结束标记符**/
    *pSlow = '\0';
    return A;
}

四、浅显的写法

bool char_in_str(char* str, int index, char* refer_str)
{
    int i = 0;
    for(i=0; i<strlen(refer_str); i++)
    {
        if(refer_str[i]==str[index])
            return true;
    }
    return false;
}
char* update_str(char* str, char* refer_str)
{
    if(str==NULL || refer_str==NULL)
        return NULL;
    int fast = 0;
    int slow = 0;
    while( str[fast] != '\0')
    {
        if(char_in_str(str,fast,refer_str)==false)
        {
            str[slow] = str[fast];
            slow++; fast++;
        }
        else
            fast++;
    }
    str[slow] = '\0';
    return str;
}

梦想还是要有的,万一实现了呢~~~~ヾ(◍°∇°◍)ノ゙~~~~~~~~~~

您可能感兴趣的与本文相关的镜像

PyTorch 2.6

PyTorch 2.6

PyTorch
Cuda

PyTorch 是一个开源的 Python 机器学习库,基于 Torch 库,底层由 C++ 实现,应用于人工智能领域,如计算机视觉和自然语言处理

以下是一个使用字符串哈希算法实现查字典功能的 C++ 程序。这里使用 BKDRHash 算法来计算字符串的哈希值,然后通过哈希表存储字典信息。 ```cpp #include <iostream> #include <vector> #include <string> // BKDRHash 字符串哈希函数 unsigned long BKDRHash(const std::string& str) { unsigned long hash = 0; const unsigned long seed = 131; for (char c : str) { hash = hash * seed + c; } return hash; } // 自定义哈希表类 class HashTable { private: struct Entry { std::string key; std::string value; bool occupied; Entry() : occupied(false) {} }; std::vector<Entry> table; size_t size; // 查找键的位置 size_t findPos(const std::string& key) const { size_t hashValue = BKDRHash(key) % table.size(); size_t step = 1; while (table[hashValue].occupied && table[hashValue].key != key) { hashValue = (hashValue + step) % table.size(); step++; } return hashValue; } public: HashTable(size_t initialSize) : size(0) { table.resize(initialSize); } // 插入键值对 void insert(const std::string& key, const std::string& value) { size_t pos = findPos(key); if (!table[pos].occupied) { table[pos].key = key; table[pos].value = value; table[pos].occupied = true; size++; } } // 查找键对应的值 std::string find(const std::string& key) const { size_t pos = findPos(key); if (table[pos].occupied && table[pos].key == key) { return table[pos].value; } return "eh"; } }; int main() { int n; std::cin >> n; std::cin.ignore(); // 忽略换行符 HashTable dictionary(2 * n); // 初始化哈希表 // 读取字典条目 for (int i = 0; i < n; ++i) { std::string line; std::getline(std::cin, line); size_t spacePos = line.find(' '); std::string english = line.substr(0, spacePos); std::string foreign = line.substr(spacePos + 1); dictionary.insert(foreign, english); } int m; std::cin >> m; std::cin.ignore(); // 忽略换行符 // 读取需要翻译的单词并进行翻译 for (int i = 0; i < m; ++i) { std::string foreign; std::getline(std::cin, foreign); std::string english = dictionary.find(foreign); std::cout << english << std::endl; } return 0; } ``` ### 代码解释 1. **BKDRHash 函数**:实现了 BKDRHash 字符串哈希算法,用于计算字符串的哈希值。 2. **HashTable 类**:自定义的哈希表类,使用开放寻址法处理哈希冲突。包含插入和查找方法。 3. **main 函数**:读取字典条目和需要翻译的单词,将字典条目插入哈希表,然后根据需要翻译的单词在哈希表中查找对应的英语单词。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值