字符串减法 (PAT甲级 哈希)

字符串减法

题目大意
给定两个字符串 S1 和 S2,S=S1−S2 定义为将 S1 中包含的所有在 S2 中出现过的字符删除后得到的字符串。

你的任务就是计算 S1−S2。

输入格式
共两行,第一行包含字符串 S1,第二行包含字符串 S2。

输出格式
输出共一行,表示 S1−S2 的结果。

Code:

#include<iostream>
#include<string>
#include<unordered_set>
#include<cstdio>

using namespace std;

int main(){
    string s1,s2;
    
    getline(cin,s1);
    getline(cin,s2);
    
    unordered_set<char> hash;               //stl实现哈希表
    for(auto c: s2) hash.insert(c);           //无序不重复
    
    string ans;
    for(auto c: s1){
        if(!hash.count(c)) ans+=c;
    }
    
    cout<<ans<<endl;
    return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_43872264/article/details/107771809