题解 | #删除字符串中出现次数最少的字符#
删除字符串中出现次数最少的字符
http://www.nowcoder.com/practice/05182d328eb848dda7fdd5e029a56da9
最复杂的代码哈哈
#include <string>
#include <map>
#include <vector>
#include <climits>
using namespace std;
map<char,int> m;
int min1 = INT_MAX,minprev=INT_MAX;
char c = ' ',cprev=' ';
string::size_type n;
void minstr(string&str,int&min1,char&c){
for(int i = 0;i<str.size();i++){
++m[str[i]];
}
for(auto it:m)
if(it.second<=min1){
min1=it.second;
c=it.first;
}
}
string& erasestr(string &str,char&c){
n=str.find_first_of(c);
while(n!=string::npos){
str=str.erase(n, 1);
n=str.find_first_of(c);
}
return str;
}
int main() {
string str;
while(cin>>str){
while((min1==minprev)){
m.clear();
minstr(str, min1, c);
str = erasestr(str, c);
minprev=min1;
cprev=c;
m.clear();
minstr(str, min1, c);
if(c==cprev)break;
}
cout<<str<<endl;
}
}