给定两个字符串 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
输出是一行,可以将两个字符串串成一个再继续做。个人感觉时间限制比较紧,随便多遍历一下就超时(所以加了一个if(str[i]!=’\n’)的判断)。还有处理相同字符的时候,本来选择的标记是“!”,然而“!”也在ASCII 表上……这一点还想好久不知道错哪了……还好,回车“\n”成功吸引我的注意,运行完美通过。
code:
#include<bits/stdc++.h>
using namespace std;
int num[6];//可以直接初始化
int main()
{
string str1,str2;
getline(cin,str1);
getline(cin,str2);
string str = str1+str2;
for(int i=0; i<str.length()-1; i++)
{
if(str[i]!='\n')
{
for(int j=i+1; j<str.length(); j++)
{
if(str[i] == str[j])
{
str[j] = '\n';
}
}
}
}
for(int i=0; i<str.length(); i++)
{
if(str[i]!='\n')
cout<<str[i];
}
}