1093 字符串A+B (20分)
题目
给定两个字符串 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
输出样例
This ampletowyu_Hrk
解析
getline()读取两字符串,然后合并为str输出,用数组a标记某字符是否已经被输出过,遍历字符串,若a[str[i]] == false,则输出,并将其设为false答案
#include<iostream>
#include<string>
using namespace std;
int main(){
string s1,s2;
getline(cin,s1);
getline(cin,s2);
string str(s1+s2);
int a[150] = {0};
for(int i = 0 ; i < str.length() ; i ++){
if(!a[str[i]]){
a[str[i]] = true;
cout << str[i];
}
}
}