1050. String Subtraction (20)

来源:互联网 发布:java url webservice 编辑:程序博客网 时间:2024/06/05 22:40

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
Sample Output:
Thy r stdnts.

给出两个字符串s和t,求s-t,即求s删去t中含有的字符得到的字符串。这里要注意输入要用getline,因为字符串可能含有空格。用一个数组记录t中有哪些字符,然后新建一个字符串res,用于储存结果,然后把含于s中但不含于t中的字符添加到res中,最后输出res。


代码:

#include <iostream>#include <cstring>#include <vector>#include <cstdlib>#include <cstdio>#include <set>using namespace std;int main(){    string s,t;    getline(cin,s);    getline(cin,t);    string res;    bool isvalid[1000];    memset(isvalid,true,1000);    for(int i=0;i<t.size();i++)    {        isvalid[int(t[i])]=false;    }    for(int i=0;i<s.size();i++)    {        if(isvalid[int(s[i])])        {            res+=s[i];        }    }    cout<<res;}


0 0