给定两个字符串 A 和 B,本题要求你输出 A+B,即两个字符串的并集。要求先输出 A,再输出 B,但重复的字符必须被剔除。
输入格式:
输入在两行中分别给出 A 和 B,均为长度不超过 106的、由可见 ASCII 字符 (即码值为32~126)和空格组成的、由回车标识结束的非空字符串。
输出格式:
在一行中输出题面要求的 A 和 B 的和。
输入样例:
This is a sample test
to show you_How it works
结尾无空行
输出样例:
This ampletowyu_Hrk
结尾无空行
思路大同小异,只不过判断的方式一个是用set辅助,一个是用数组辅助。
遍历中,set的思路是如果能插入,说明它不是重复的,输出,插入;
数组同理。
记得用getline()输入字符串,因为有空格
#include<iostream>
#include<iomanip>
#include<vector>
#include<set>
#include<list>
#include<deque>
#include<stack>
#include<unordered_map>
#include<map>
#include<string>
#include<algorithm>
#include<cmath>
using namespace std;
int main()
{
string a, b;
getline(cin, a);
getline(cin, b);
string c = a + b;
set<char> s;
vector<char> v;
for (int i = 0; i < c.length(); i++)
{
char cc = c[i];
if (s.insert(cc).second)
v.push_back(cc);
}
vector<char>::iterator it = v.begin();
while (it != v.end())
{
cout << *it;
it++;
}
return 0;
}
#include <iostream>
using namespace std;
char arr[150] = { 0 }
int main()
{
string a, b, c;
getline(cin, a);
getline(cin, b);
c = a + b;
for (int i = 0; i < c.size(); i++)
{
if (arr[c[i]] == 0)
{
cout << c[i];
arr[c[i]] = 1;
}
}
return 0;
}