L1-011 A-B (20 分)【四种方法】

7-6 A-B (20分)

本题要求你计算A−B。不过麻烦的是,A和B都是字符串 —— 即从字符串A中把字符串B所包含的字符全删掉,剩下的字符组成的就是字符串A−B。

输入格式:

输入在2行中先后给出字符串A和B。两字符串的长度都不超过10e​4 ,并且保证每个字符串都是由可见的ASCII码和空白字符组成,最后以换行符结束。

输出格式:

在一行中打印出A−B的结果字符串。

输入样例:

I love GPLT!  It's a fun game!
aeiou

输出样例:

I lv GPLT!  It's  fn gm!

AC【map】

简单又容易理解,但是我不会想到的啊!!!

#include<bits/stdc++.h>
#include <iostream>
#include <algorithm>
using namespace std;

int main() {
	string a,b;
	getline(cin,a);
	getline(cin,b);
	map<char,int> m;
	for(int i=0; i<b.length(); i++) {
		m[b[i]]=1;
	}
	for(int i=0; i<a.length(); i++) {
		if(m[a[i]]!=1) {
			cout<<a[i];
		}
	}
	return 0;
}

【set】

#include <bits/stdc++.h>
using namespace std;
int main() {
	string s1,s2;
	getline(cin,s1);
	getline(cin,s2);
	set<char> s;
	for(int i=0; i<s2.length(); i++) {
		s.insert(s2[i]);
	}
	for(int i=0; i<s1.length(); i++) {
		if(s.find(s1[i])==s.end())
			cout<<s1[i];
	}
	return 0;
}
#include<bits/stdc++.h>
using namespace std;
int main() {
	string s1,s2;
	getline(cin,s1);
	getline(cin,s2);
	set<char> b;
	//cout<<s1<<endl<<s2<<endl;
	for(int i=0; i<s2.length(); i++) {
		b.insert(s2[i]);
	}
	for(int i=0; i<s1.length(); i++) {
		set<char>::iterator it=b.find(s1[i]);
		if(*it!=s1[i])
			cout<<s1[i];
	}
	return 0;
}

普通方法1

#include<bits/stdc++.h>
#include <iostream>
#include <algorithm>
using namespace std;

int main() {
	string a,b,c="";
	getline(cin,a);
	getline(cin,b);
	for(int i=0; i<a.length(); i++) {
		bool f=0;
		for(int j=0; j<b.length(); j++) {
			if(a[i]==b[j])
				f=1;
		}
		if(f==0)
			c=c+a[i];
	}
	cout<<c;
	return 0;
}

普通方法2

#include<bits/stdc++.h>
#include <iostream>
#include <algorithm>
using namespace std;

int main() {
	string a,b;
	getline(cin,a);
	getline(cin,b);
	int len=a.length();
	for(int i=0; i<b.length(); i++) {
		char s=b[i];
		for(int j=0; j<len; j++) {
			if(s==a[j]) {
				for(int k=j; k<len-1; k++) {
					a[k]=a[k+1];
				}
				len--;
				j--;//这里要-- 就是排除连着两个一样的字符
			}
		}
	}
	for(int i=0; i<len; i++)
		cout<<a[i];

	return 0;
}
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

摆烂.MVP

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值