当前位置:网站首页>【编程强训】删除公共字符(哈希映射)+组队竞赛(贪心)
【编程强训】删除公共字符(哈希映射)+组队竞赛(贪心)
2022-07-01 07:07:00 【…狂奔的蜗牛~】
1.删除公共字符 ->链接

【解题思路】:
本题如果使用传统的暴力查找方式,如判断第一个串的字符是否在第二个串中,在再挪动字符删除这个字符
的方式,效率为O(N^2),效率太低,很难让人满意。
将第二个字符串的字符都映射到一个hashtable数组中,用来判断一个字符在这个字符串。
判断一个字符在第二个字符串,不要使用删除,这样效率太低,因为每次删除都伴随数据挪动。这里可 以考虑使用将不在字符添加到一个新字符串,最后返回新新字符串。
实现代码:
#include<iostream>
#include<string>
using namespace std;
int main()
{
string s1,s2;
// 注意这里不能使用cin接收,因为cin遇到空格就结束了。
// oj中IO输入字符串最好使用getline。
getline(cin,s1);
getline(cin,s2);
int hash[256]={
0};
// 使用哈希映射思想先str2统计字符出现的次数
for(int i=0;i<s2.size();i++)
{
hash[s2[i]]++;
}
// 遍历str1,str1[i]映射hashtable对应位置为0,则表示这个字符在
// str2中没有出现过,则将他+=到ret。注意这里最好不要str1.erases(i)
// 因为边遍历,边erase,容易出错。
string ret="";
for(int j=0;j<s1.size();j++)
{
if(hash[s1[j]]==0)
ret+=s1[j];
}
cout<<ret<<endl;
return 0;
}
2.组队竞赛 -> 链接

【题目解析】:
队伍的水平值等于该队伍队员中第二高水平值,为了所有队伍的水平值总和最大的解法,也就是说每个队伍
的第二个值是尽可能大的值。所以实际值把最大值放到最右边,最小是放到最左边。
【解题思路】:
本题的主要思路是贪心算法,贪心算法其实很简单,就是每次选值时都选当前能看到的局部最解忧,所以这里的贪心就是保证每组的第二个值取到能选择的最大值就可以,我们每次尽量取最大,但是最大的 数不可能是中位数,所以退而求其次,取每组中第二大的。
- 例如 现在排序后 有 1 2 5 5 8 9 ,那么分组为1 8 9 和 2 5 5
- 关系式:
arr[arr.length-(2*(i+1))](取到的是一个队伍的水平值)
代码实现:
#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
int main()
{
int n=0;
//循环实现多组测试
while(cin>>n)
{
vector<int> a;
long long sum=0;
a.resize(3*n);
for(int i=0;i<3*n;i++)
{
cin>>a[i];
}
std::sort(a.begin(),a.end());//用sort实现升序排序
for(int i=0;i<n;i++)
{
sum+=a[a.size()-(2*(i+1))];
}
cout<<sum<<endl;
}
return 0;
}
边栏推荐
- JAX的深度学习和科学计算
- ctfshow-web354(SSRF)
- Programming examples of stm32f1 and stm32subeide infrared receiving and decoding of NEC protocol
- Fix the problem that the AI video intelligent platform easycvr device video cannot be played
- 比赛即实战!中国软件杯发布全新产业创新赛项,校企可联合参赛
- 【图像处理】图像直方图均衡化系统含GUI界面
- Introduction to spark (one article is enough)
- Ctfhub port scan (SSRF)
- 【LINGO】求无向图的最短路问题
- 灰度何以跌下神坛?
猜你喜欢

盘点华为云GaussDB(for Redis)六大秒级能力
![[network planning] (I) hub, bridge, switch, router and other concepts](/img/7b/fcef37496517c854ac1dbfb35fa3f4.png)
[network planning] (I) hub, bridge, switch, router and other concepts

8 figures | analyze Eureka's first synchronization registry

ctfshow-web351(SSRF)

Image style migration cyclegan principle

C# 读写自定义的Config文件

运维管理系统,人性化操作体验

北漂程序员深夜emo发帖求助:女朋友走了我很孤独 ......

【计网】(一) 集线器、网桥、交换机、路由器等概念

【Tikhonov】基于Tikhonov正则化的图像超分辨率重建
随机推荐
Challenges faced by operation and maintenance? Intelligent operation and maintenance management system to help you
赌上了绩效,赢了公司CTO,我要搭DevOps平台!
kdtree(kd树)笔记
【深圳IO】精确食品称(汇编语言的一些理解)
Reply and explanation on issues related to "online training of network security education in 2022"
为什么这么多人转行产品经理?产品经理发展前景如何?
Easynvs cloud management platform function reconfiguration: support adding users, modifying information, etc
Rclone Chinese document: a collection of common commands
WiFi settings for raspberry Pie 4
iNFTnews | 从《雪崩》到百度“希壤”,元宇宙30年的16件大事
记一次线上接口慢查询问题排查
rclone 访问web界面
【图像处理】图像直方图均衡化系统含GUI界面
JAX的深度学习和科学计算
Rclone configuring Minio and basic operations
[recommendation technology] matlab simulation of network information recommendation technology based on collaborative filtering
Automated test platform (13): interface automation framework and platform comparison, application scenario analysis and design ideas sharing
Unity2021-Scene视图中物体无法直接选中的解决办法
ctfshow-web355,356(SSRF)
比赛即实战!中国软件杯发布全新产业创新赛项,校企可联合参赛