1050 String Subtraction

题意:给出两个字符串s1和s2,在s1中删去s2中含有的字符。

思路:注意,因为读入的字符串可能有空格,因此用C++的getline(cin,str)。PAT系统迁移之后C语言中的gets()函数被禁用了。

代码:

#include <iostream>
#include <string>
using namespace std;
int main() { string str1,str2; getline(cin,str1); getline(cin,str2); bool needDeleted[128]={false};//needDeleted[ch]为true表示字符ch应当删去 for(auto ch:str2) needDeleted[ch]=true; for(auto ch:str1){ if(needDeleted[ch]) continue;//遇到需要删去的字符不输出 else cout<<ch; } return 0; }

猜你喜欢

转载自www.cnblogs.com/kkmjy/p/9551648.html
今日推荐