Skip to content

Latest commit

 

History

History
41 lines (30 loc) · 1.03 KB

1093. 字符串A+B.md

File metadata and controls

41 lines (30 loc) · 1.03 KB

【PAT B-1093】字符串 A+B

题意概述

给定两个字符串 A 和 B,本题要求你输出 A+B,即两个字符串的并集。要求先输出 A,再输出 B,但重复的字符必须被剔除。

输入输出格式

输入在两行中分别给出 A 和 B。

在一行中输出题面要求的 A 和 B 的和。

数据规模

A 和 B 均为长度不超过${10}^6$的、由可见 ASCII 字符和空格组成的、由回车标识结束的非空字符串。

算法设计

由于 ASCII 码只有 128 位,可以定义 bitset<128>类型变量 h 来记录字符是否出现过。然后遍历 A 和 B 两个字符串,输出第一次出现的字符并随时更新 h 即可。

C++代码

#include <bits/stdc++.h>
using namespace std;
int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    string s;
    bitset<128> h;
    while (getline(cin, s)) {
        for (char c : s) {
            if (not h[c]) {
                h[c] = true;
                cout << c;
            }
        }
    }
    return 0;
}