【PAT甲级 - C++题解】1050 String Subtraction

简介: 【PAT甲级 - C++题解】1050 String Subtraction

1050 String Subtraction


Given two strings S1 and S2, S=S1−S2 is defined to be the remaining string after taking all the characters in S2 from S1. Your task is simply to calculate S1−S2 for any given strings. However, it might not be that simple to do it fast.


Input Specification:


Each input file contains one test case. Each case consists of two lines which gives S1 and S2, respectively. The string lengths of both strings are no more than 104. It is guaranteed that all the characters are visible ASCII codes and white space, and a new line character signals the end of a string.

Output Specification:

For each test case, print S1−S2 in one line.



Sample Input:

They are students.
aeiou
• 1
• 2

Sample Output:

Thy r stdnts.


思路

用哈希表标记 b bb 串中所有字符。

枚举 a aa 串中所有字符,如果当前字符在哈希表中即在 b bb 串中已经出现过,就跳过,否则就加入 r e s resres 当中。

输出 r e s resres 。


代码

#include<bits/stdc++.h>
using namespace std;
int main()
{
    string a, b;
    getline(cin, a);
    getline(cin, b);
    //将b中所有字符放入哈希表标记
    unordered_map<char, bool> st;
    for (int i = 0; i < b.size(); i++)
        st[b[i]] = true;
    //枚举a中每个字符观察是否在哈希表中出现过
    string res = "";
    for (int i = 0; i < a.size(); i++)
        if (!st.count(a[i]))
            res += a[i];
    cout << res << endl;
    return 0;
}


目录
相关文章
|
15天前
|
C++ 容器
|
5天前
|
存储 安全 C++
【C++打怪之路Lv8】-- string类
【C++打怪之路Lv8】-- string类
10 1
|
15天前
|
C++ 容器
|
15天前
|
C++ 容器
|
15天前
|
存储 C++ 容器
|
10天前
|
C语言 C++
深度剖析C++string(中)
深度剖析C++string(中)
32 0
|
10天前
|
存储 编译器 程序员
深度剖析C++string(上篇)(2)
深度剖析C++string(上篇)(2)
30 0
|
10天前
|
存储 Linux C语言
深度剖析C++string(上篇)(1)
深度剖析C++string(上篇)(1)
22 0
|
15天前
|
C++
|
17天前
|
C语言 C++
C++番外篇——string类的实现
C++番外篇——string类的实现
18 0