题目
给定两个字符串 A A A 和 B B B,本题要求你输出 A + B A+B A+B,即两个字符串的并集。要求先输出 A A A,再输出 B B B,但重复的字符必须被剔除。
输入格式:
输入在两行中分别给出 A A A 和 B B B,均为长度不超过 1 0 6 10^6 106 的、由可见 ASCII 字符 (即码值为32~126)和空格组成的、由回车标识结束的非空字符串。
输出格式:
在一行中输出题面要求的 A A A 和 B B B 的和。
输入样例:
This is a sample test
to show you_How it works
输出样例:
This ampletowyu_Hrk
思路(2020-4-15 17:16:25)
- 两串输入依次遍历,一个字符一个字符遍历,没输出过的输出,输出过的跳过。
- 可以用一个数组,保存输出过的字符。
- 在柳神dalao那学来的。
1、
getline()
获取输入保存到string
类型变量的方法。
2、getline(stream cin,string s);
2、大部分getline都是保存成char
数组
答案(2020-4-15 17:21:18)
#include <iostream>
#include <string.h>
using namespace std;
int main()
{
string s1,s2;
getline(cin,s1);
getline(cin,s2);
string s = s1+s2;
int beenOutPut[200] = {0};// the soul of this code
for(int i = 0 ; i<s.size();i++)
if(beenOutPut[(int)s[i]]==0)
{
cout<< s[i];
beenOutPut[(int)s[i]] = 1;
}
return 0;
}