当前位置:网站首页>[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;
}
边栏推荐
- 灰度何以跌下神坛?
- ctfshow-web354(SSRF)
- [recommendation technology] matlab simulation of network information recommendation technology based on collaborative filtering
- [lingo] find the shortest path problem of undirected graph
- 转行做产品经理,如何挑选产品经理课程?
- C language implementation [minesweeping game] full version (implementation source code)
- 【电气介数】电气介数及考虑HVDC和FACTS元件的电气介数计算
- 【LINGO】求七个城市最小连线图,使天然气管道价格最低
- [lingo] find the minimum connection diagram of seven cities to minimize the price of natural gas pipelines
- 自动化测试平台(十三):接口自动化框架与平台对比及应用场景分析及设计思路分享
猜你喜欢

Paging in servlets and JSPS

Ctfhub port scan (SSRF)

Is it suitable for girls to study product manager? What are the advantages?

DC-4靶机
![[FPGA frame difference] FPGA implementation of frame difference target tracking based on vmodcam camera](/img/0f/045957961725716435439316078347.png)
[FPGA frame difference] FPGA implementation of frame difference target tracking based on vmodcam camera

1286_FreeRTOS的任务优先级设置实现分析

Warm congratulations on the successful listing of five elements hehe liquor

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

C# Newtonsoft. Use of job in JSON

C语言实现【扫雷游戏】完整版(实现源码)
随机推荐
[classification model] Q-type cluster analysis
未来互联网人才还稀缺吗?哪些技术方向热门?
关于图灵测试和中文屋Chinese room的理解
Introduction to spark (one article is enough)
北漂程序员深夜emo发帖求助:女朋友走了我很孤独 ......
Ctfhub port scan (SSRF)
ctfshow-web351(SSRF)
Which securities company is better or safer for mobile phone account opening
C# Newtonsoft.Json中JObject的使用
Why are so many people turning to product managers? What is the development prospect of product manager?
[Tikhonov] image super-resolution reconstruction based on Tikhonov regularization
【电气介数】电气介数及考虑HVDC和FACTS元件的电气介数计算
Subclasses call methods and properties of the parent class with the same name
[recommendation technology] matlab simulation of network information recommendation technology based on collaborative filtering
The game is real! China software cup releases a new industrial innovation competition, and schools and enterprises can participate in it jointly
Is it safe to buy funds on the brokerage account
如何制作专属的VS Code主题
Using fuseki query when there are multiple models in TDB
Easynvs cloud management platform function reconfiguration: support adding users, modifying information, etc
How to enter the Internet industry and become a product manager? How to become a product manager without project experience?