PAT甲级1050 String Subtraction (20分)|C++实现

一、题目描述

原题链接
在这里插入图片描述

Input Specification:

在这里插入图片描述

​​Output Specification:

在这里插入图片描述

Sample Input:

They are students.
aeiou

Sample Output:

Thy r stdnts.

二、解题思路

20分简单题,时间限制很大。我们可以建立一个哈希表来表示各个字母,然后用一个数组记录该字母是否存在于 S 2 S_2 S2中,我这里使用的是unordered_map。随后遍历 S 1 S_1 S1,如果遍历到的字符不在 S 2 S_2 S2中,则进行输出。

三、AC代码

#include<iostream>
#include<cstdio>
#include<unordered_map>
using namespace std;
int main()
{
    
    
    unordered_map<char, int> mp;
    string str1, str2;
    getline(cin, str1);
    getline(cin, str2);
    for(int i=0; i<str2.size(); i++)    mp[str2[i]] = 1;
    for(int i=0; i<str1.size(); i++)
        if(mp[str1[i]] == 0)    printf("%c", str1[i]);
    return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_42393947/article/details/108597437