PTA B1093 字符串A+B C++/Python3
题目描述
给定两个字符串 A 和 B,本题要求你输出 A+B,即两个字符串的并集。要求先输出 A,再输出 B,但重复的字符必须被剔除。
输入格式
输入在两行中分别给出 A 和 B,均为长度不超过 10 6的、由可见 ASCII 字符 (即码值为32~126)和空格组成的、由回车标识结束的非空字符串。
输出格式
在一行中输出题面要求的 A 和 B 的和。
样例 #1
样例输入 #1
This is a sample test
to show you_How it works
样例输出 #1
This ampletowyu_Hrk
C++
#include <bits/stdc++.h>
using namespace std;
signed main() {
string a,b;
getline(cin,a);
getline(cin,b);
//把两个字符串拼接到一起
a=a+b;
map<char,int> m;
for(int i=0; i<a.size(); ++i) {
//如果是第一次遇见字符,则输出
if(m[a[i]]==0) {
m[a[i]]++;
cout<<a[i];
}
}
return 0;
}
Python3/Pypy3
a = input()
b = a + input()
lst = set()
for i in b:
if i not in lst:
lst.add(i)
print(i, end="")