题目链接:点击打开链接

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

Copy
4
ssh
hs
s
hhhs
output
18
input

Copy
2
h
s
output
1
Note

The optimal concatenation in the first sample is ssshhshhhs.

题意:给一堆由s和h组成的字符串,让你拼接,使得子序列为“sh”的数量最多,并且输出这个最多的值

题解:对于每个给的字符串,无论怎么拼接,它本身拥有的sh是不变的,而将str1与str2拼接之后(str1在前)增加的sh数量为str1中的s数量乘str2中的h数量。

由此,可以构造数据结构,每个字符串拥有“s数量、h数量、sh数量”三个变量,注意struct的使用方法以及初始化方式。

接下来要解决的问题就是如何进行拼接,既然整体的sh数量只根据拼接方式决定,我们可以让s多的在前面?显然不阔仪。

我们将问题进行简化:如何拼接两个字符串可以让sh最多?授衔,两个字符串分别私有的的sh数量是无论如何都不变的,我们只看拼接之后增加的sh数量如何最多即可。如上面说的,我们只需要比较“str1中的s数量乘str2中的h数量”与“str2中的s数量乘str1中的h数量”哪个最多即可,前者多则str1排在前面,后者多str2排在前面。

知道如何排序了,怎么实现数据结构的排序呢?阔仪自己写一个冒泡,no! algorithm中的sort函数是可以对复杂数据结构进行排序的,只是要自己定义比较函数,所以嘿嘿。。。下面介绍sort中的cmp怎么写

cmp函数是一个bool类型的函数,假如我们定义的数据结构叫sh,那么bool cmp(sh a,sh b)是我们要定义的比较函数,如果返回true,则a排在前面,否则b排在前面  

(加粗染红下划线)一定要注意cmp函数一定要保证涵盖了所有的情况!也就是说只要调用成功,cmp一定要有返回值出来,不然就会re。text 5 的噩梦。。

这题如果还有坑点的话,那就是日常char类型读入回车。。也很好解决,看代码

#include<cstdio>
#include<cstring>
#include<algorithm>
#define min(a, b) a>=b?b:a
#define max(a, b) a>=b?a:b
typedef long long ll;
using namespace std; struct sh{
ll ns;
ll nh;
ll nsh;
sh(): ns(0), nh(0), nsh(0){}
}str[100000+ 1000]; bool cmp(sh a, sh b){
if(a.ns * b.nh > a.nh * b.ns) return true;
else if(a.ns * b.nh < a.nh * b.ns) return false;
else {
if(a.ns > b. ns) return true;
else return false;
}
} int main(){
int n;
scanf("%ld", &n);
char d;
scanf("%c", &d);//接收回车符
for(int i = 0; i < n; i++){
while(1){ scanf("%c", &d);
if(d == 's') str[i].ns++;
else if (d == 'h') {str[i].nh++;str[i].nsh+=str[i].ns;}
else break;
} }
sort(str, str + n, cmp);
long long cnt = 0;
long long cnts = 0, cnth = 0;
for(int i = 0; i < n ; i++){
cnt+= str[i].nsh + cnts * str[i].nh;
cnts += str[i].ns;
}
printf("%lld\n", cnt);
return 0;
}

Codeforces 922 C - Robot Vacuum Cleaner (贪心、数据结构、sort中的cmp)的更多相关文章

  1. CF922D Robot Vacuum Cleaner 贪心+排序

    正确的贪心方法:按照比例排序. code: #include <bits/stdc++.h> #define N 200000 #define ll long long #define s ...

  2. 【Codeforces 922D】Robot Vacuum Cleaner

    [链接] 我是链接,点我呀:) [题意] 让你把n个字符串重新排序,然后按顺序连接在一起 使得这个组成的字符串的"sh"子序列最多 [题解] /* * 假设A的情况好于B * 也就 ...

  3. CodeForces - 922D Robot Vacuum Cleaner (贪心)

    Pushok the dog has been chasing Imp for a few hours already. Fortunately, Imp knows that Pushok is a ...

  4. Codeforces Round #461 (Div. 2) D. Robot Vacuum Cleaner

    D. Robot Vacuum Cleaner time limit per test 1 second memory limit per test 256 megabytes Problem Des ...

  5. Codeforces 583 DIV2 Robot's Task 贪心

    原题链接:http://codeforces.com/problemset/problem/583/B 题意: 就..要打开一个电脑,必须至少先打开其他若干电脑,每次转向有个花费,让你设计一个序列,使 ...

  6. Codeforces 437C The Child and Toy(贪心)

    题目连接:Codeforces 437C  The Child and Toy 贪心,每条绳子都是须要割断的,那就先割断最大值相应的那部分周围的绳子. #include <iostream> ...

  7. Codeforces Round #546 (Div. 2) D 贪心 + 思维

    https://codeforces.com/contest/1136/problem/D 贪心 + 思维 题意 你面前有一个队列,加上你有n个人(n<=3e5),有m(m<=个交换法则, ...

  8. CodeForces 24D Broken robot(期望+高斯消元)

    CodeForces 24D Broken robot 大致题意:你有一个n行m列的矩形板,有一个机器人在开始在第i行第j列,它每一步会随机从可以选择的方案里任选一个(向下走一格,向左走一格,向右走一 ...

  9. Codeforces 1154D - Walking Robot - [贪心]

    题目链接:https://codeforces.com/contest/1154/problem/D 题解: 贪心思路,没有太阳的时候,优先用可充电电池走,万不得已才用普通电池走.有太阳的时候,如果可 ...

随机推荐

  1. Java工作流引擎节点接收人设置20种规则讲解一

    关键字: 驰骋工作流程快速开发平台 工作流程管理系统 工作流引擎 asp.net工作流引擎 java工作流引擎. 开发者表单  拖拽式表单 工作流系统CCBPM节点访问规则接收人规则 适配数据库: o ...

  2. SpringCloud入门系列0-Nacos的安装与配置

    背景 工作有一些年头了,自从19年初彻底转了java(这又是另一篇心酸的故事),突然感觉自己荒废了好几年(不是说.net不好,而是回顾自己这几年做的很多东西都浮于表面,有时候弄成很忙的样子,回头看看自 ...

  3. 【一起学源码-微服务】Feign 源码二:Feign动态代理构造过程

    前言 前情回顾 上一讲主要看了@EnableFeignClients中的registerBeanDefinitions()方法,这里面主要是 将EnableFeignClients注解对应的配置属性注 ...

  4. OpenGL ES for Android

    经过半年的准备OpenGL ES for Android系列文章终于要和大家见面了,在这里定一个小目标-先吸引1000个粉丝,万一实现了呢.写关于OpenGL ES的文章开始是有一些犹豫的,因为Ope ...

  5. 树上点分治 poj 1741

    Give a tree with n vertices,each edge has a length(positive integer less than 1001). Define dist(u,v ...

  6. Python解析json字符串,json字符串用法

    json数据简介 json数据是一个轻量级的数据交换格式,采用完全独立于语言的文本格式,这些特性使json称为理想的数据交换语言,易于人阅读和编写,同时易于机器解析和生成. json中的字符集必须是U ...

  7. 1.异常(Error和Exception)

    什么是异常 比如上午我们一般是开车去上班,正常情况下,一般都不会迟到,但是今天突然有个问题,车坏了或者限行了,于是乎你改坐公交了,就有可能会迟到,这就属于一种异常的情况.在实际生活中呢,可能会遇到很多 ...

  8. 8.JavaSE之变量、常量、作用域

    变量variable: 变量是什么:就是内存中开辟的可以变化的量! Java是一种强类型语言,每个变量都必须声明其类型. Java变量是程序中最基本的存储单元,其要素包括变量名,变量类型,作用域   ...

  9. javaWeb传收参数方式总结

    有时候,我真会被传参搞得头晕,这样传要怎么接收,那样传又要怎么接收? get可以json吗?什么是json方式提交?等等问题,已困扰我许久 所以,在此想做个总结,整理一下思绪,不再为传收参烦恼!如有错 ...

  10. emeditor安装及插件信息

    原文地址:https://www.52pojie.cn/thread-658917-1-1.html 废话不多说 官网:https://www.emeditor.com/download/ 安装版:6 ...