Given two strings S1 and S2, S = S1 - S2 is defined to be the remaining string after taking all the characters in S2from 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. aeiouSample Output:
Thy r stdnts.
#include <cstdio>
#include <vector>
#include <string>
#include <algorithm>
#include <iostream>
#include <map>
using namespace std;
int main(){
string s1,s2,output;
getline(cin, s1);
getline(cin, s2);
map<char, bool> c_map;
for(int i=0; i<256; i++)c_map.insert(make_pair( i , 1));
for(string::iterator it=s2.begin() ; it!=s2.end() ; ++it)c_map[*it]=0;
for(string::iterator it=s1.begin() ; it!=s1.end() ; ++it)if(c_map[*it])output+=*it;
cout<<output;
return 0;
}
本文介绍了一种计算两个字符串差集的高效算法,并提供了完整的C++实现代码。输入为两个字符串S1和S2,输出为从S1中去除所有S2中存在的字符后的剩余字符串。文章通过使用标准模板库(map和string)来简化操作并提高效率。
353

被折叠的 条评论
为什么被折叠?



