实现删除字符串中出现次数最少的字符,若多个字符出现次数一样,则都删除。输出删除这些单词后的字符串,字符串中其它字符保持原来的顺序。
字符串只包含小写英文字母, 不考虑非法输入,输入的字符串长度小于等于20个字节。
删除字符串中出现次数最少的字符后的字符串。
注意:此处不能直接用string::erase()循环删除pos位置的字符,因为每次根据pos删除一个字符后索引号已改变,下一次删除无效。
完整AC的代码如下:
#include <iostream> #include <vector> #include <string> #include <algorithm> using namespace std; int main() { string inStr; while(getline(cin,inStr)){ int tempCnt=0; int minCnt=100; vector<int > minPos; for(int i=0;i<inStr.size();i++){ tempCnt= count(inStr.begin(),inStr.end(),inStr[i]); if(tempCnt<=minCnt){ if(tempCnt<minCnt)minPos.clear(); minCnt=tempCnt; minPos.push_back(i); } } for(auto pos:minPos) inStr.replace(pos,1,"0"); int pos; while((pos=inStr.find('0'))!=string::npos){ inStr.erase(pos,1); } cout<<inStr<<endl; } }
