PAT-B 1093 字符串A+B 【集合】

                                             PAT-B 1093 字符串A+B

                    https://pintia.cn/problem-sets/994805260223102976/problems/1071785884776722432

题目

给定两个字符串 A 和 B,本题要求你输出 A+B,即两个字符串的并集。要求先输出 A,再输出 B,但重复的字符必须被剔除

输入

输入在两行中分别给出 A 和 B,均为长度不超过 10​^6​​的、由可见 ASCII 字符 (即码值为32~126)和空格组成的、由回车标识结束的非空字符串。

输出

在一行中输出题面要求的 A 和 B 的和。

样例输入

This is a sample test
to show you_How it works

样例输出

扫描二维码关注公众号,回复: 5343425 查看本文章
This ampletowyu_Hrk

分析

使用集合记录已经出现过的字符,具体看程序。

C++程序

#include<iostream>
#include<string>
#include<set>

using namespace std;

set<char>s;//存放已经出现的字符

int main()
{
	string A,B;
	getline(cin,A);
	getline(cin,B);
	A+=B;
	for(int i=0;i<A.length();i++)
	{
		if(s.count(A[i])==0)//还未出现过 
		{
			s.insert(A[i]);
			cout<<A[i];
		}
	}
	return 0;
}
 

猜你喜欢

转载自blog.csdn.net/SongBai1997/article/details/87905588