64,645
社区成员
发帖
与我相关
我的任务
分享
# include <iostream>
# include <string>
# include <algorithm>
using namespace std;
int main(void)
{
string s = "acabadddddd";
string t(s);
sort(s.begin(), s.end());
char tempc = '\0', maxc;
string::size_type count = 0, max = 0;
string::iterator tempit = s.begin(), maxit;
for (string::iterator it = s.begin(); it != s.end(); ++it)
{
if (*it != tempc)
{
if (count > max)
{
max = count;
maxit = tempit;
maxc = tempc;
}
count = 1;
tempit = it;
tempc = *it;
}
else
count++;
}
if (count > max)//最后一组迭代完之后需要在循环外判断
{
max = count;
maxit = tempit;
maxc = tempc;
}
s.erase(maxit, maxit + max);
string::iterator i = t.begin(), ti;
while ((ti = find(i, t.end(), maxc)) != t.end())
{
i = t.erase(ti);
}
cout << s << endl;
cout << t << endl;
return 0;
}
map<char,int> cnt;
string s = "ffaaaaaaaaaaaccdffgaddfa";
int ctmax=0,maxchar;
for(char &ch : s) {
int &t =cnt[ch];
if(++t>ctmax){
ctmax =t;
maxchar =ch;
}
}
s.erase(remove(s.begin(),s.end(),maxchar),s.end());
#include <iostream>
#include <string>
#include <cstring>
char fuck(const std::string &str)
{
int buf[128];
memset(buf , 0 , sizeof(buf));
for(auto const &ch : str)
{
buf[ch]++;
}
int max = buf[0];
char ch;
for(size_t i = 0 ; i < sizeof(buf) / sizeof(int) ; ++i)
{
if(buf[i] > max)
{
max = buf[i];
ch = i;
}
}
return ch;
}
std::string& bitch(std::string &str , char ch)
{
for(auto it = str.begin() ; it != str.end() ; )
{
if(*it == ch)
{
it = str.erase(it);
}
else
{
++it;
}
}
return str;
}
int main(void)
{
//空格最多,会被删除
std::string str = "hello csdn , you are a fucking website";
auto ch = fuck(str);
bitch(str , ch);
std::cout << str << std::endl;
return 0;
}