牛客:HJ23 删除字符串中出现次数最少的字符[华为机考][字符串]
学习要点
- 注意题目要求
- 注意哈希会改变原来顺序
题目链接
删除字符串中出现次数最少的字符_牛客题霸_牛客网
题目描述
解法:map
#include <bits/stdc++.h>
#include <bits/types/struct_itimerspec.h>
#include <climits>
#include <iostream>
using namespace std;int main() {string line;getline(cin, line);map<char, int> ch_map;for (auto& i : line) {ch_map[i]++;}int min_ch = INT_MAX;char ch;for (auto& i : ch_map) {min_ch = min(min_ch, i.second);}// 若有多个字符出现的次数都最少,则把这些字符都删map<char,int> no_str;for (auto& i : ch_map) {if(min_ch == i.second){no_str[i.first]++;}}// 字符串中其它字符保持原来的顺序。bool flag = false;for (auto& i : line) {if(no_str[i] == 0){flag = true;cout << i;}}// 保证这个字符串至少包含一个字符。if(!flag){cout <<line;}}
// 64 位输出请用 printf("%lld")