pat-A1050-String Subtraction

本文介绍了一种用于计算两个字符串差集的算法,通过使用布尔型hashmap记录字符状态,实现从字符串s1中去除所有在s2中出现的字符。文章详细解释了算法的思路,并提供了完整的C++代码实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目链接->link

题意描述

输入两字符串s1,s2,输出s1-s2,即s2所有字符在s1不能出现。

思路

  1. 输入s2,遍历s2并记录bool型hashmap[]值为false;然后遍历s1,如果hashmap[s1[i]]的值为false则跳过。

代码

#include <stdio.h>
#include <math.h>
#include <string.h>
#include <iostream>

using namespace std;
const int maxn=10005;
char str1[maxn],str2[maxn];
bool hashmap[128];//存储字符能否输出

int main(){
    memset(hashmap, true, sizeof(hashmap));
    fgets(str1,maxn,stdin);
    fgets(str2,maxn,stdin);
    int len1=strlen(str2);
    for(int i=0;i<len1;i++){//遍历str2
        hashmap[str2[i]]=false;
    }
    int len2=strlen(str1);
    for(int i=0;i<len2;i++){//遍历str1
        if(hashmap[str1[i]])printf("%c",str1[i]);
    }
    printf("\n");
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值