当前位置:网站首页>Problem - 922D、Robot Vacuum Cleaner - Codeforces
Problem - 922D、Robot Vacuum Cleaner - Codeforces
2022-07-06 09:28:00 【你好_Ä】
Problem - 922D、Robot Vacuum Cleaner - Codeforces
Pushok the dog has been chasing Imp for a few hours already.
Fortunately, Imp knows that Pushok is afraid of a robot vacuum cleaner.While moving, the robot generates a string t consisting of letters ‘s’ and ‘h’, that produces a lot of noise. We define noise of string t as the number of occurrences of string “sh” as a subsequence in it, in other words, the number of such pairs (i, j), that i < j and
and
.The robot is off at the moment. Imp knows that it has a sequence of strings ti in its memory, and he can arbitrary change their order. When the robot is started, it generates the string t as a concatenation of these strings in the given order. The noise of the resulting string equals the noise of this concatenation.Help Imp to find the maximum noise he can achieve by changing the order of the strings.
Input
The first line contains a single integer n (1 ≤ n ≤ 105) — the number of strings in robot’s memory.Next n lines contain the strings t1, t2, …, tn, one per line. It is guaranteed that the strings are non-empty, contain only English letters ‘s’ and ‘h’ and their total length does not exceed 105.
Output
Print a single integer — the maxumum possible noise Imp can achieve by changing the order of the strings.
Examples
input
4
ssh
hs
s
hhhs
output
18
问题解析
这题是说,给你n个字符串,这些字符串全都是由字符’s’和‘h’组成的。让你把这些字符串任意顺序组合起来,使得拼成的字符串含有的’sh‘子序列最多。
关于这题,用到的是一种叫“邻项交换”的方法写的,想法就是这样:两个字符串a和b,如果a+b组成的字符串含有的’sh‘子序列比b+a组成的字符串含有的多,那么最后拼接时把a放在b前是更好的选择。所以这种方法就是让我们写个cmp然后sort一下就行。
cmp内容就是把两个字符串按照不同顺序组合起来,分别计算出他们含有的’sh‘个数,至于’sh‘序列个数,就是每个h字符前面的所有s字符数量加起来。注意一下,如果两个字符串的’sh‘序列数相等,那要返回false(我也不知道为啥,但就是要这样,不然会re)。
AC代码
#include<iostream>
using namespace std;
#include<vector>
#include<algorithm>
#include<math.h>
#include<set>
#include<numeric>
#include<string>
#include<string.h>
#include<iterator>
#include<map>
#include<unordered_map>
#include<stack>
#include<list>
#include<queue>
#include<iomanip>
#define endl '\n'
#define int ll
typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ll>PII;
const int N = 2e5 + 50;
int get_sh(string& s)
{
int cnt = 0, nums = 0, numh = 0;
for (auto i : s)
{
if (i == 's')
{
nums++;
}
else
{
numh++;
cnt += nums;
}
}
return cnt;
}
bool cmp(string a, string b)
{
string s1 = a + b, s2 = b + a;
return get_sh(s1)>get_sh(s2);
}
signed main()
{
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
int n;
cin >> n;
vector<string>v(n);
for (int i = 0; i < n; i++)
cin >> v[i];
sort(v.begin(), v.end(),cmp);
string s;
for (auto i : v)s += i;
cout << get_sh(s) << endl;
return 0;
}
结尾
邻项交换法也是比较常见的贪心方法了,类似的题目还有算法提高 最小字符串和新国王游戏 - 题目 - Daimayuan Online Judge
边栏推荐
- 渗透测试 ( 3 ) --- Metasploit Framework ( MSF )
- HDU - 6024 building shops (girls' competition)
- Understand what is a programming language in a popular way
- Flask框架配置loguru日志庫
- window11 conda安装pytorch过程中遇到的一些问题
- QT有关QCobobox控件的样式设置(圆角、下拉框,向上展开、可编辑、内部布局等)
- Information security - Epic vulnerability log4j vulnerability mechanism and preventive measures
- [exercise-9] Zombie's Treasury test
- E. Breaking the Wall
- [analysis of teacher Gao's software needs] collection of exercises and answers for level 20 cloud class
猜你喜欢

渗透测试 ( 1 ) --- 必备 工具、导航

969. Pancake sorting

【高老师软件需求分析】20级云班课习题答案合集

TCP's three handshakes and four waves

628. Maximum product of three numbers

2078. Two houses with different colors and the farthest distance

渗透测试 ( 8 ) --- Burp Suite Pro 官方文档

605. Planting flowers
![[exercise-4] (UVA 11988) broken keyboard = = (linked list)](/img/59/78ca7170ab1fd364ec44cfbcdc7ab5.png)
[exercise-4] (UVA 11988) broken keyboard = = (linked list)

Flask框架配置loguru日志庫
随机推荐
【练习-1】(Uva 673) Parentheses Balance/平衡的括号 (栈stack)
Information security - threat detection - Flink broadcast stream broadcaststate dual stream merging application in filtering security logs
【练习-5】(Uva 839)Not so Mobile(天平)
The most complete programming language online API document
PySide6 信号、槽
【练习-4】(Uva 11988)Broken Keyboard(破损的键盘) ==(链表)
Opencv learning log 27 -- chip positioning
QT实现窗口置顶、置顶状态切换、多窗口置顶优先关系
If you want to apply for a programmer, your resume should be written like this [essence summary]
渗透测试 ( 3 ) --- Metasploit Framework ( MSF )
Common configuration files of SSM framework
Truck History
最全编程语言在线 API 文档
Luogu P1102 A-B number pair (dichotomy, map, double pointer)
7-1 understand everything (20 points)
渗透测试 ( 7 ) --- 漏洞扫描工具 Nessus
Alice and Bob (2021 Niuke summer multi school training camp 1)
1013. Divide the array into three parts equal to and
Information security - threat detection engine - common rule engine base performance comparison
[exercise-4] (UVA 11988) broken keyboard = = (linked list)