当前位置:网站首页>【编程强训】删除公共字符(哈希映射)+组队竞赛(贪心)
【编程强训】删除公共字符(哈希映射)+组队竞赛(贪心)
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;
}
边栏推荐
- 为什么这么多人转行产品经理?产品经理发展前景如何?
- How to permanently configure local opencv4.5.5 for vs2019
- rclone 访问web界面
- 用手机在指南针上开户靠谱吗?这样有没有什么安全隐患
- LeetCode+ 71 - 75
- Kdtree notes
- [FPGA frame difference] FPGA implementation of frame difference target tracking based on vmodcam camera
- Are there any practical skills for operation and maintenance management
- iNFTnews | 从《雪崩》到百度“希壤”,元宇宙30年的16件大事
- 自动化测试平台(十三):接口自动化框架与平台对比及应用场景分析及设计思路分享
猜你喜欢
![[Tikhonov] image super-resolution reconstruction based on Tikhonov regularization](/img/49/719496e014f4766d22aba44dbed19e.png)
[Tikhonov] image super-resolution reconstruction based on Tikhonov regularization

ctfshow-web354(SSRF)

Operation and maintenance management system, humanized operation experience

(I) apple has open source, but so what?

Illusory and simple screen raindrop post-processing effect

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

Esp32 - ULP coprocessor reading Hall sensor in low power mode

【LINGO】求解二次规划

ctfshow-web351(SSRF)

WiFi settings for raspberry Pie 4
随机推荐
How to enter the Internet industry and become a product manager? How to become a product manager without project experience?
為什麼這麼多人轉行產品經理?產品經理發展前景如何?
Automated test platform (13): interface automation framework and platform comparison, application scenario analysis and design ideas sharing
解决kaniko push镜像到harbor时报错(代理导致):unexpected status code 503 Service Unavailable
Webapck packaging principle -- Analysis of startup process
Système de gestion de l'exploitation et de l'entretien, expérience d'exploitation humanisée
Is the account opening of GF Securities safe and reliable? How to open GF Securities Account
ctfshow-web354(SSRF)
Challenges faced by operation and maintenance? Intelligent operation and maintenance management system to help you
Chinese explanation of common rclone subcommands
[lingo] find the shortest path problem of undirected graph
[classification model] Q-type cluster analysis
1286_ Implementation analysis of task priority setting in FreeRTOS
【推荐技术】基于协同过滤的网络信息推荐技术matlab仿真
Programming examples of stm32f1 and stm32subeide infrared receiving and decoding of NEC protocol
Jena default inference query based on OWL
5G Massive MIMO的概念和优点总结
【LINGO】求七个城市最小连线图,使天然气管道价格最低
【电气介数】电气介数及考虑HVDC和FACTS元件的电气介数计算
rclone中文文档:常用命令大全