【PAT】1050. String Subtraction (20)

题目描述

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.

翻译:给你两个字符串S1和S2,S = S1 - S2被定义为从S1中去掉所有S2中的字符后剩下的字符串。你的任务很简单,就是对给定的字符串计算S1-S2。但是,做的迅速可能不简单。

INPUT FORMAT

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.

翻译:对于每组输入数据包括一组测试数据。每组测试数据包括两行,分别为给定的S1和S2。字符串的长度都不超过10^4。数据保证所有的字母都是ASCll编码和空格,一个新的换行符代表一个字符串的末尾。

OUTPUT FORMAT

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

翻译:对于每组输入数据,输出一行S1-S2。


Sample Input:

They are students.
aeiou

Sample Output:

Thy r stdnts.


解题思路

将S2字符串中的字符用hash数组保存,如果S1字符串中的字符在S2中出现过,就置为-1。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<string>
#include<algorithm>
#define INF 99999999
using namespace std;
string S1,S2;
bool Hash[500]; 
int main(){
    memset(Hash,0,sizeof(Hash));
    getline(cin,S1);
    getline(cin,S2);
    for(int i=0;i<S2.size();i++){
        if(!Hash[S2[i]])Hash[S2[i]]=true;
    }
    for(int i=0;i<S1.size();i++){
        if(Hash[S1[i]])S1[i]=-1;
    }
    for(int i=0;i<S1.size();i++){
        if(S1[i]>=0)printf("%c",S1[i]);
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值