当前位置:网站首页>【编程强训】删除公共字符(哈希映射)+组队竞赛(贪心)
【编程强训】删除公共字符(哈希映射)+组队竞赛(贪心)
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;
}
边栏推荐
- ctfshow-web355,356(SSRF)
- 為什麼這麼多人轉行產品經理?產品經理發展前景如何?
- [the path of system analysts] Chapter 5: software engineering of double disk (reverse clean room and Model Driven Development)
- [Tikhonov] image super-resolution reconstruction based on Tikhonov regularization
- Will Internet talents be scarce in the future? Which technology directions are popular?
- Product learning (I) - structure diagram
- Pourquoi tant de gens sont - ils devenus des gestionnaires de produits? Quelles sont les perspectives de développement des gestionnaires de produits?
- K8s set up redis cluster
- Problem: officeexception: failed to start and connect (III)
- WiFi settings for raspberry Pie 4
猜你喜欢
关于图灵测试和中文屋Chinese room的理解
【Tikhonov】基于Tikhonov正则化的图像超分辨率重建
Why are so many people turning to product managers? What is the development prospect of product manager?
iNFTnews | 从《雪崩》到百度“希壤”,元宇宙30年的16件大事
电脑有网络,但所有浏览器网页都打不开,是怎么回事?
Product learning (II) - competitive product analysis
【MATLAB】求解非线性规划
Introduction to spark (one article is enough)
Product learning (I) - structure diagram
【推荐系统】美团外卖推荐场景的深度位置交互网络DPIN的突破与畅想
随机推荐
Why did grayscale fall from the altar?
AI视频智能平台EasyCVR设备录像出现无法播放现象的问题修复
LeetCode+ 71 - 75
[the path of system analysts] Chapter 5: software engineering of double disk (reverse clean room and Model Driven Development)
rclone常用子命令中文解释
go-etcd
DC-4 target
如何画产品架构图?
TDB中多个model情况下使用fuseki查询
1286_FreeRTOS的任务优先级设置实现分析
Is it safe to buy funds on Alipay? Where can I buy funds
广发证券开户是安全可靠的么?怎么开广发证券账户
[lingo] solve quadratic programming
How to use Alibaba vector font files through CDN
Problem solving: officeexception: failed to start and connect (I)
為什麼這麼多人轉行產品經理?產品經理發展前景如何?
运维管理有什么实用的技巧吗
在长城证券上做基金定投安全吗?
【LINGO】求解二次规划
关于“2022年度网络安全教育线上培训”相关问题的复盘和说明