【PAT甲级】String Subtraction

Problem Description:

Given two strings S_{1} and S_{2}, S = S_{1}S_{2}​​​​​​​​​ is defined to be the remaining string after taking all the characters in S_{2}​​​​​​​​​ from S_{1}​​​​​​​​​. Your task is simply to calculate S_{1}​​​​​​​​​−S_{2}​​​​​​​ 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 S_{1}​​​​​​​​​ and S_{2}​​, respectively. The string lengths of both strings are no more than 10​4​​. 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 S_{1}​​−S_{2} in one line.

Sample Input:

They are students.
aeiou

Sample Output:

Thy r stdnts.

解题思路:

这道水题跟【GPLT】L1-011 A-B 可以说是一模一样的,上次我是用map求解的,那这次就用set来玩下吧。在字符串A中删除字符串B中含有的字符后输出字符串A-B。换个角度来想就是只输出字符串A中字符串B不含有的字符。

AC代码: 

#include <bits/stdc++.h>
using namespace std;

int main()
{
    string s1,s2;
    getline(cin,s1);
    getline(cin,s2);
    set<int> s;   //记录s2中的字符
    for(auto it : s2)
    {
        s.insert(it);
    }
    for(auto it : s1)
    {
        if(s.count(it) == 0)  //避免输出s2中的字符
        {
            cout << it;
        }
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_42449444/article/details/88782967