当前位置:网站首页>[programming training] delete public characters (hash mapping) + team competition (greedy)
[programming training] delete public characters (hash mapping) + team competition (greedy)
2022-07-01 07:15:00 【... running snail ~】
1. Delete public characters -> link
【 Their thinking 】:
If we use the traditional violence search method , For example, judge whether the character of the first string is in the second string , Move the character again to delete this character
The way , The efficiency is O(N^2), Efficiency is too low , It's hard to satisfy .
Map the characters of the second string to a hashtable Array , Used to determine a character in this string .
Judge a character in the second string , Do not use delete , That's inefficient , Because every deletion is accompanied by data movement . Here To consider the use of adding an absent character to a new string , Finally, a new string is returned .
Implementation code :
#include<iostream>
#include<string>
using namespace std;
int main()
{
string s1,s2;
// Be careful not to use cin receive , because cin It ends when you encounter a space .
// oj in IO The best input string is getline.
getline(cin,s1);
getline(cin,s2);
int hash[256]={
0};
// Using the idea of hash mapping, first str2 Count the number of times characters appear
for(int i=0;i<s2.size();i++)
{
hash[s2[i]]++;
}
// Traverse str1,str1[i] mapping hashtable The corresponding position is 0, It means that this character is in
// str2 There's no such thing as , Then take him += To ret. Be careful not to str1.erases(i)
// Because edge traversal , edge erase, It's easy to make mistakes .
string ret="";
for(int j=0;j<s1.size();j++)
{
if(hash[s1[j]]==0)
ret+=s1[j];
}
cout<<ret<<endl;
return 0;
}
2. Team competition -> link
【 title 】:
The level value of the team is equal to the second highest level value among the team members , For the solution of the maximum sum of the level values of all teams , That is to say, every team
The second value of is the largest possible value . So the actual value puts the maximum value to the far right , The smallest is on the far left .
【 Their thinking 】:
The main idea of this problem is greedy algorithm , The greedy algorithm is actually very simple , That is, each time you select a value, you choose the most worry free part you can see at present , So the greed here is to ensure that the second value of each group is the maximum value that can be selected , We try to maximize each time , But the biggest The number cannot be the median , So back to second place , Take the second largest in each group .
- for example Now after sorting Yes 1 2 5 5 8 9 , Then group them into 1 8 9 and 2 5 5
- Relational :
arr[arr.length-(2*(i+1))]
( What you get is the level value of a team )
Code implementation :
#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
int main()
{
int n=0;
// Loop to implement multiple sets of tests
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());// use sort Implement ascending sort
for(int i=0;i<n;i++)
{
sum+=a[a.size()-(2*(i+1))];
}
cout<<sum<<endl;
}
return 0;
}
边栏推荐
- Automated test platform (13): interface automation framework and platform comparison, application scenario analysis and design ideas sharing
- LeetCode+ 71 - 75
- Unity2021-Scene视图中物体无法直接选中的解决办法
- Record an online interface slow query problem troubleshooting
- 如何制作专属的VS Code主题
- 1286_ Implementation analysis of task priority setting in FreeRTOS
- [recommendation technology] matlab simulation of network information recommendation technology based on collaborative filtering
- 手机开户选哪个证券公司比较好,哪个更安全
- 如何画产品架构图?
- 【LINGO】求解二次规划
猜你喜欢
EasyNVS云管理平台功能重构:支持新增用户、修改信息等
Félicitations pour l'inscription réussie de wuxinghe
【Flutter 问题系列第 72 篇】在 Flutter 中使用 Camera 插件拍的图片被拉伸问题的解决方案
1286_FreeRTOS的任务优先级设置实现分析
The game is real! China software cup releases a new industrial innovation competition, and schools and enterprises can participate in it jointly
【推荐系统】美团外卖推荐场景的深度位置交互网络DPIN的突破与畅想
Problem: officeexception: failed to start and connect (III)
Warm congratulations on the successful listing of five elements hehe liquor
Those high-frequency written tests and interview questions in [Jianzhi offer & Niuke 101] - linked list
盘点华为云GaussDB(for Redis)六大秒级能力
随机推荐
[the path of system analysts] Chapter 5: software engineering of double disk (reverse clean room and Model Driven Development)
rclone常用子命令中文解释
Automated test platform (13): interface automation framework and platform comparison, application scenario analysis and design ideas sharing
Why are so many people turning to product managers? What is the development prospect of product manager?
继妹变继母,儿子与自己断绝关系,世界首富马斯克,为何这么惨?
【Tikhonov】基于Tikhonov正则化的图像超分辨率重建
MySQL table partition creation method
Do securities account opening affect the security of account opening
C# 读写自定义的Config文件
Rclone configuring Minio and basic operations
Is it safe to buy funds on the brokerage account
Warm congratulations on the successful listing of five elements hehe liquor
C# Newtonsoft. Use of job in JSON
(I) apple has open source, but so what?
1286_FreeRTOS的任务优先级设置实现分析
C language implementation [minesweeping game] full version (implementation source code)
C# Newtonsoft.Json中JObject的使用
Is the account opening of GF Securities safe and reliable? How to open GF Securities Account
Alibaba OSS postman invalid according to policy: policy condition failed: ["starts with", "key", "test/"]
【编程强训】删除公共字符(哈希映射)+组队竞赛(贪心)