蓝桥杯-求字符串中出现最多的字符的个数和任务相关性分析

直接用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;
	
}
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

书中藏着宇宙

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值