直接用map就可以了,map完之后进行排序就对了
#include<bits/stdc++.h>
using namespace std;
std::map<char,int>cnt;
std::pair<char,int> p1={'A',-1};
std::pair<char,int> find_max_alpha(string& s)
{
for(char ch : s)//遍历其中的字符
cnt[ch]++;
//寻找最大的value,sort函数只可以用于顺序容器,不适合关联容器
for(auto s:cnt)
{
if(p1.second<s.second &&s.first<'z'&& s.first>'a')
{
p1.second=s.second;
p1.first =s.first;
}
}
return p1;
}
signed main()
{
string s;
cin>>s;
cout<<find_max_alpha(s).first <<endl<<find_max_alpha(s).second;
return 0;
}
任务相关性分析,直接求出每个字符出现的个数,然后再做判断
#include<bits/stdc++.h>
using namespace std;
int match(string& s,int k)
{
std::set<int> s1;
std::set<int> s2;
int pos1=0;
int pos2=0;
int cnt=0;
for(int i=0;i<s.size();++i)
{
//cout<<"==============="<< s.size();
pos1=s.find("Alice",pos1+1);
pos2=s.find("Bob",pos2+1);
cout<< "pos1: "<<pos1<<" pos2: "<<pos2<<endl;
// 将每次他们的位置给存储下来
if(pos1>0)
s1.insert(pos1);
if(pos2>0)
s2.insert(pos2);
}
for(auto it =s1.begin();it != s1.end();++it)
for(auto it1 =s2.begin();it1 != s2.end();++it1)
{
if(abs(*it - *it1)<=k)
cnt++;
}
return cnt;
}
signed main()
{
// This is a story about Alice and Bob.Alice wants to send a private message to Bob
string s;
cin>> s;
std::getline(std::cin, s);
//cout<<s;
cout<<match(s,20);
return 0;
}