当前位置:网站首页>[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;
}
边栏推荐
- Félicitations pour l'inscription réussie de wuxinghe
- rclone 访问web界面
- 【剑指offer&牛客101】中那些高频笔试,面试题——链表篇
- Programming examples of stm32f1 and stm32subeide infrared receiving and decoding of NEC protocol
- [matlab] solve nonlinear programming
- Système de gestion de l'exploitation et de l'entretien, expérience d'exploitation humanisée
- Are there any practical skills for operation and maintenance management
- How to choose a product manager course when changing to a product manager?
- Pourquoi tant de gens sont - ils devenus des gestionnaires de produits? Quelles sont les perspectives de développement des gestionnaires de produits?
- AI视频智能平台EasyCVR设备录像出现无法播放现象的问题修复
猜你喜欢

Easynvs cloud management platform function reconfiguration: support adding users, modifying information, etc

Image style migration cyclegan principle

【图像处理】图像直方图均衡化系统含GUI界面

【推荐系统】美团外卖推荐场景的深度位置交互网络DPIN的突破与畅想

【目标检测】目标检测界的扛把子YOLOv5(原理详解+修炼指南)
![[network planning] (I) hub, bridge, switch, router and other concepts](/img/7b/fcef37496517c854ac1dbfb35fa3f4.png)
[network planning] (I) hub, bridge, switch, router and other concepts

女生适合学产品经理吗?有什么优势?

Product learning (II) - competitive product analysis

Product learning (I) - structure diagram

Jena default inference query based on OWL
随机推荐
【Flutter 问题系列第 72 篇】在 Flutter 中使用 Camera 插件拍的图片被拉伸问题的解决方案
Introduction to spark (one article is enough)
[network planning] (I) hub, bridge, switch, router and other concepts
MySQL table partition creation method
ctfshow-web351(SSRF)
为什么这么多人转行产品经理?产品经理发展前景如何?
Automated test platform (13): interface automation framework and platform comparison, application scenario analysis and design ideas sharing
C language implementation [minesweeping game] full version (implementation source code)
ctfshow-web352,353(SSRF)
[the path of system analysts] Chapter 5: software engineering of double disk (reverse clean room and Model Driven Development)
The game is real! China software cup releases a new industrial innovation competition, and schools and enterprises can participate in it jointly
代码实战——从零开始搭建自己的Diffusion models/Score-based generative models
[lingo] solve quadratic programming
女生适合学产品经理吗?有什么优势?
Is it safe to do fund fixed investment on Great Wall Securities?
Open source! Wenxin large model Ernie tiny lightweight technology, accurate and fast, full effect
ctfshow-web355,356(SSRF)
盘点华为云GaussDB(for Redis)六大秒级能力
C语言实现【扫雷游戏】完整版(实现源码)
转行做产品经理,如何挑选产品经理课程?