PTA basic 1093 字符串A+B (20 分) c++语言实现(g++)

给定两个字符串 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<char>去重

#include <iostream>
#include <vector>
#include <algorithm>
#include <set>
using namespace std;
int main(){
    vector<string> str;
    string result,temp;
    set<char> charset;
    set<char>::iterator it;
    for(int i=0;i<2;i++){
        getline(cin, temp);
        str.push_back(temp);
        for(int j=0;j<str[i].size();j++){
            it=charset.find(str[i][j]);
            if(it==charset.end()){
                charset.insert(str[i][j]);
                result.push_back(str[i][j]);
            }
        }
    }
    cout <<result<<endl;
    return 0;
}

 

posted @ 2021-05-14 13:51  keiiha  阅读(65)  评论(0编辑  收藏  举报