当前位置:网站首页>【编程强训】删除公共字符(哈希映射)+组队竞赛(贪心)
【编程强训】删除公共字符(哈希映射)+组队竞赛(贪心)
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;
}
边栏推荐
- We found a huge hole in MySQL: do not judge the number of rows affected by update!!!
- Système de gestion de l'exploitation et de l'entretien, expérience d'exploitation humanisée
- 比赛即实战!中国软件杯发布全新产业创新赛项,校企可联合参赛
- 【Tikhonov】基于Tikhonov正则化的图像超分辨率重建
- 自动化测试平台(十三):接口自动化框架与平台对比及应用场景分析及设计思路分享
- Insufficient free space after clearing expired cache entries - consider increasing the maximum cache space
- [image processing] image histogram equalization system with GUI interface
- Is it safe to do fund fixed investment on Great Wall Securities?
- 1286_FreeRTOS的任务优先级设置实现分析
- Open source! Wenxin large model Ernie tiny lightweight technology, accurate and fast, full effect
猜你喜欢

JSP - 分页

电脑有网络,但所有浏览器网页都打不开,是怎么回事?

運維管理系統,人性化操作體驗

Problem: officeexception: failed to start and connect (II)

【电气介数】电气介数及考虑HVDC和FACTS元件的电气介数计算
![[lingo] solve quadratic programming](/img/4d/3f7de69943f29a71c4039299c547f7.png)
[lingo] solve quadratic programming
![[Tikhonov] image super-resolution reconstruction based on Tikhonov regularization](/img/49/719496e014f4766d22aba44dbed19e.png)
[Tikhonov] image super-resolution reconstruction based on Tikhonov regularization

Unity2021-Scene视图中物体无法直接选中的解决办法

AI视频智能平台EasyCVR设备录像出现无法播放现象的问题修复

ctfhub-端口扫描(SSRF)
随机推荐
Problem: officeexception: failed to start and connect (II)
Fix the problem that the AI video intelligent platform easycvr device video cannot be played
【推荐技术】基于协同过滤的网络信息推荐技术matlab仿真
【电气介数】电气介数及考虑HVDC和FACTS元件的电气介数计算
How to choose a product manager course when changing to a product manager?
北漂程序员深夜emo发帖求助:女朋友走了我很孤独 ......
[lingo] solve quadratic programming
Chinese explanation of common rclone subcommands
SQL learning notes nine connections 2
K8s set up redis cluster
How to draw a product architecture diagram?
[matlab] solve nonlinear programming
灰度何以跌下神坛?
Esp32 esp-idf GPIO key interrupt response
8 figures | analyze Eureka's first synchronization registry
1286_ Implementation analysis of task priority setting in FreeRTOS
We found a huge hole in MySQL: do not judge the number of rows affected by update!!!
TDB中多个model情况下使用fuseki查询
C语言实现【扫雷游戏】完整版(实现源码)
DC-4靶机