PAT A1050 String Subtraction

题意

给定两个字符串 S1 和 S2,S=S1−S2 定义为将 S1 中包含的所有在 S2 中出现过的字符删除后得到的字符串。

你的任务就是计算 S1−S2。

输入格式

共两行,第一行包含字符串 S1,第二行包含字符串 S2。

输出格式

输出共一行,表示 S1−S2 的结果。

数据范围

两个给定字符串的长度都不超过 10^4

输入样例:

They are students.
aeiou

输出样例:

Thy r stdnts.

思路

  • 因为字符串中有空格,所以输入用getline来输入。
  • 将s1中所有s2的字母都去除掉即可,遍历就vans了

AC代码1

#include <bits/stdc++.h>

using namespace std;

bool st[128];

int main()
{
	string a,b;
	getline(cin,a);
	getline(cin,b);
	for(auto &c : b)
		st[c] = true;
	for(auto &c : a)
		if(!st[c])
			cout << c;
	
	return 0;
}

AC代码2

#include <bits/stdc++.h>

using namespace std;

int main()
{
    string s1,s2;
    getline(cin,s1);
    getline(cin,s2);
    
    unordered_set<char> hash;
    for(auto c : s2)    hash.insert(c);
    
    string res = "";
    for(auto c : s1)
        if(!hash.count(c))
            res += c;
    
    cout << res << endl;
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值