华为笔试题--删除字符串中出现次数最少的字符

 1 #include<iostream>
 2 #include<string>
 3 #include<algorithm>
 4 #include<unordered_set>
 5  
 6 using namespace std;
 7 
 8 /*给出一个字符串,将重复的字符去除,仅保留第一次出现的字符,且保持去重后的字符在原字符串中的顺序不变。
 9 
10 输入数据是一个字符串(不包含空格)
11 
12 输出去重后的字符串
13 
14 输入:12ere2
15 
16 输出:12er
17 */
18 
19 int main()
20 {
21     string instr,outstr;
22     unordered_set<char> sc; //用来去重字符串中重复字符
23     getline(cin,instr);
24     for(auto c:instr)
25     {
26         if(sc.find(c) == sc.end()) // 如果set中没找到该字符
27         {
28             sc.insert(c);
29             outstr.append(1,c);
30         }
31     }
32     cout<< outstr <<endl;
33     return 0;
34 }

猜你喜欢

转载自www.cnblogs.com/joker1937/p/10661565.html