当前位置:网站首页>ACM. The beauty of hj45 name ●●
ACM. The beauty of hj45 name ●●
2022-06-22 21:47:00 【chenyfan_】
HJ45 The beauty of the name ●●
describe
Give a string , The string consists only of lowercase letters , The definition of this string is “ Beauty ” It's all the letters “ Beauty ” The sum of .
Every letter has one “ Beauty ”, The scope is 1 To 26 Between . No two different letters have the same “ Beauty ”. Letters ignore case .
Give multiple strings , Calculate the maximum possible for each string “ Beauty ”.
This question contains several groups of data .
Data range : The length of the entered name meets 1 ≤ n ≤ 10000 1 \le n \le 10000 1≤n≤10000
Input description :
The first line is an integer N, Next N Line one string per line
Output description :
The maximum possible beauty of each string
Example
Input :
2
zhangsan
lisi
Output :
192
101
explain :
For example lisi, Give Way i Your beauty is 26,l Your beauty is 25,s Your beauty is 24,lisi Your beauty is 25+26+24+26=101.
Answer key
1. Array statistics + greedy
Traversal string , Count the frequency of letters , After completion of Statistics , Because the final result only requires a score , So there is no need to know the frequency of a letter , So sort the array from large to small , The greater the frequency , The more beautiful ( Up to 26), Finally, calculate the total beauty .
- Time complexity : O ( N l e n ) O(Nlen) O(Nlen), You need to iterate through each letter of each string .
- Spatial complexity : O ( l e n + 26 ) O(len + 26) O(len+26), Space required to store strings .
/* HJ45 The beauty of the name */
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
using namespace std;
int main(){
int n;
cin >> n;
while(n--){
string str;
cin >> str;
vector<int> table(26, 0);
int cnt = 0;
for(char ch: str){
if(table[ch-'a'] == 0) ++cnt; // Statistics frequency , And the number of letters
++table[ch-'a'];
} // Sort by frequency from large to small
sort(table.begin(), table.end(), [=](int a, int b){
// greater<int>()
return a > b;
});
int ans = 0, score = 26; // score , The greater the frequency , The higher the score
for(int i = 0; i < cnt; ++i){
ans += table[i] * score;
--score;
}
cout << ans << endl;
}
return 0;
}
边栏推荐
- Lesson 020: functions: embedded functions and closures | after class test questions and answers
- 软考必备资料大放送,全科目软考资料都给你备好了!
- 第028讲:文件:因为懂你,所以永恒 | 课后测试题及答案【无标题】
- Jericho uses DP and DM for IO key detection [chapter]
- 长安旗下阿维塔科技增资扩股落定:宁德时代将持股约24%!
- ACM. HJ45 名字的漂亮度 ●●
- 安卓kotlin sp dp转px
- Final review of scientific and technological literature of Shandong University (Personal Crash Course)
- The problem that Jericho can't play the prompt tone in the music mode using the interrupt mode [chapter]
- [redis]redis persistence
猜你喜欢

【ICML2022】利用虚拟节点促进图结构学习

TC397 Flash

2022 a special equipment related management (elevator) examination questions and simulation examination

Cannot re-register id: PommeFFACompetition-v0问题解决
![[redis]配置文件](/img/1c/05c06d59c9efb5983f877822db333c.png)
[redis]配置文件

redis学习笔记

ICML2022 | 利用虚拟节点促进图结构学习

杰理之MUSIC 模式获取播放文件的目录【篇】
![Kali2021 installing the rtl8188gu wireless network card [tl-wn726n] driver](/img/29/8dd188cc4e909562862b5f2c57c898.png)
Kali2021 installing the rtl8188gu wireless network card [tl-wn726n] driver

What is a data asset? How should data asset management be implemented?
随机推荐
杰理之MUSIC 模式获取播放文件的目录【篇】
View Apple product warranty status
How to operate redis on the IntelliJ idea database console
70 root cause analysis Oracle database sudden performance problems, who will take the blame
86- to attend & lt; SQL writing and rewriting training & gt; 's participants add a second-hand case
Oracle数据库中文字符串和英文字符串的截取不同
Jerry's problem of opening the near end of four channel call [chapter]
88- widely circulated parameter optimization, honey or poison?
Lesson 028: Documents: because I know you, I will never forget the after-school test questions and answers [no title]
79- do not create desc descending index when you see order by XXX desc - there is book donation benefit at the end of the article
第032讲:异常处理:你不可能总是对的 | 课后测试题及答案
牛客 52次月赛 C 说谎的机器 (区间赋值操作由O(n^2)转为O(n)的复杂度)
第022讲:函数:递归是神马 | 课后测试题及答案
Kali2021 installing the rtl8188gu wireless network card [tl-wn726n] driver
微软 Edge 浏览器将支持网络测速,内置计算器和单位转换工具
Learning websites that programmers must see
2022 question bank and simulated examination for work license of main principals of hazardous chemical business units
2022年A特种设备相关管理(电梯)考题及模拟考试
Simulated 100 questions and simulated examination of hoisting machinery command examination in 2022
74- how to remedy the loss of Oracle to MySQL for this kind of SQL optimization?