题解 | #删除字符串中出现次数最少的字符#C++解法,很晚了,就不优化了

删除字符串中出现次数最少的字符

http://www.nowcoder.com/practice/05182d328eb848dda7fdd5e029a56da9

#include<bits/stdc++.h>
using namespace std;
int main()
{
    string str;
    while(cin>>str)
    {
        map<char,int> m;
        for(auto x:str)
        {
            m[x]++;
        }
        vector<char> vc;
        int tmp=m.begin()->second;
        for(auto it=m.begin();it!=m.end();it++)
        {
           if(tmp>it->second)
           {
               tmp=it->second;//最小的个数是tmp个
           }
        }
        for(auto it=m.begin();it!=m.end();it++)
        {
            if(it->second==tmp)
            {
                vc.push_back(it->first);//找出个数为tmp个的字符
            }
        }
        {
            for(int j=0;j<vc.size();j++)//删除出现次数最少的字符
            {
                for(auto x=str.begin();*x!='\0';x++)
                {
                    if(*x==vc[j])
                    {
                        str.erase(x);
                    }
                }
            }
        }
        cout<<str<<endl;
    }
}
全部评论

相关推荐

点赞 收藏 评论
分享
牛客网
牛客企业服务