D. Robot Vacuum Cleaner

time limit per test 1 second

memory limit per test 256 megabytes

Problem Description

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 ≤ 1e5) — 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 1e5.

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

Input

2

h

s

Output

1

Note

The optimal concatenation in the first sample is ssshhshhhs.


解题心得:

  1. 就是让你将这写字符串拼接成一个,要求子序列出现sh的次数要最多,并且打印出现的次数。
  2. 一开始以为是一个dp,推了一会儿发现推不出来。其实就是一个贪心,s要尽可能的放在前面,h要尽可能的放在后面,那么如果排序后拼接,是以s数目为准还是h呢,好像都不太对,然后根据直觉写了个s和h数目的比例,按照比例排序,然后过了。感觉有点迷。其实想想确实是这样,比例反映的是s和h两个标准对整个字符串出现sh数目尽可能多的贡献,s贡献大就放在前面,h贡献大就放在后面,符合前面提出的贪心的思想。
  3. 要注意一下在计算比例的时候分母出现0的情况,还有就是题意中说的是字符串拼接之后总长度不超过1e5,不然就1s的时间总长度都1e10了还怎么写。

#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5+100;
const long long Max = 1e11;
struct String{
double p;
int num_s,num_h,temp;
friend bool operator < (const String a,const String b) {//按照s和h的比例从大到小排序
return a.p > b.p;
}
}st[maxn]; int n; void init(){
char s[maxn];
for(int i=0;i<n;i++){
scanf("%s",s);
int len = strlen(s);
int num_s,num_h,temp;
num_s = num_h = temp = 0;
for(int j=0;j<len;j++){
if(s[j] == 's')
num_s++;
else{
temp += num_s;//字串自身能形成多少个sh
num_h++;
}
}
double p;
if(num_h == 0)//分母是0直接赋予最大值
p = Max;
else
p = (double)num_s/(double)num_h;
st[i].num_s = num_s;
st[i].num_h = num_h;
st[i].temp = temp;
st[i].p = p;
}
sort(st,st+n);
} long long get_ans(){
long long ans = 0,num_s = 0;
for(int i=0;i<n;i++){
ans += st[i].temp;//自身出现的sh数目
ans += num_s*st[i].num_h;//当前串和前面已经拼接好的串形成的sh数目
num_s += st[i].num_s;
}
return ans;
} int main(){
scanf("%d",&n);
init();
long long ans = get_ans();
printf("%lld\n",ans);
return 0;
}

Codeforces Round #461 (Div. 2) D. Robot Vacuum Cleaner的更多相关文章

  1. CF922 CodeForces Round #461(Div.2)

    CF922 CodeForces Round #461(Div.2) 这场比赛很晚呀 果断滚去睡了 现在来做一下 A CF922 A 翻译: 一开始有一个初始版本的玩具 每次有两种操作: 放一个初始版 ...

  2. Codeforces Round #461 (Div. 2) B C D

    题目链接:http://codeforces.com/contest/922 B. Magic Forest time limit per test 1 second memory limit per ...

  3. Codeforces Round #461 (Div. 2)

    A - Cloning Toys /* 题目大意:给出两种机器,一种能将一种原件copy出额外一种原件和一个附件, 另一种可以把一种附件copy出额外两种附件,给你一个原件, 问能否恰好变出题目要求数 ...

  4. Codeforces Round #575 (Div. 3) C. Robot Breakout (模拟,实现)

    C. Robot Breakout time limit per test3 seconds memory limit per test256 megabytes inputstandard inpu ...

  5. Codeforces Round #461 (Div. 2) C. Cave Painting

    C. Cave Painting time limit per test 1 second memory limit per test 256 megabytes Problem Descriptio ...

  6. Codeforces Round #461 (Div. 2) B. Magic Forest

    B. Magic Forest time limit per test 1 second memory limit per test 256 megabytes Problem Description ...

  7. Codeforces Round #461 (Div. 2) A. Cloning Toys

    A. Cloning Toys time limit per test 1 second memory limit per test 256 megabytes Problem Description ...

  8. Codeforces Round #461 (Div. 2)B-Magic Forest+位运算或优雅的暴力

    Magic Forest 题意:就是在1 ~ n中找三个值,满足三角形的要求,同时三个数的异或运算还要为0: , where  denotes the bitwise xor of integers  ...

  9. Codeforces Round #552 (Div. 3) 题解

    Codeforces Round #552 (Div. 3) 题目链接 A. Restoring Three Numbers 给出 \(a+b\),\(b+c\),\(a+c\) 以及 \(a+b+c ...

随机推荐

  1. 19.CentOS7下PostgreSQL安装过程

    CentOS7下PostgreSQL安装过程 装包 sudo yum install postgresql-server postgresql-contrib 说明: 这种方式直接明了,其他方法也可以 ...

  2. 执行命令npm install XXX后仍然提示 Cannot find Module XXX

    最近遇到一个问题,在服务器上配置完node环境后 执行npm start 命令后提示 Cannot find Module "Jquery" 然后就知道可能没有安装jquery 就 ...

  3. 从零开始的全栈工程师——html篇1.6

    浮动与伪类选择器 一.浮动(float) 1.标准文档流 标准文档流是一种默认的状态 浏览器的排版是根据元素的特征(块和行级) 从上往下 从左往右排版 这就是标准文档流 2.浮动(float)floa ...

  4. vscode jsx语法自动补全html代码

    1.点击文件——>首选项——>设置 注意:只有在js文件里的jsx才可以自动补全,html文件里的jsx不能.

  5. click事件的累加绑定

    click事件的累加绑定,绑定一次点击事件,执行多次. 在页面中为一个元素绑定事件,事件执行后页面未刷新且元素还在,然后你再次点击,元素又被绑定一个点击事件,这样第二次点击就会执行两次,以此类推. 如 ...

  6. mybatis-generator.xml

    <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE generatorConfiguration ...

  7. 爬虫基础-http请求的基础知识

    百度百科上这么介绍爬虫: 网络爬虫(又被称为网页蜘蛛,网络机器人,在FOAF社区中间,更经常的称为网页追逐者),是一种按照一定的规则,自动地抓取万维网信息的程序或者脚本. 在开发爬虫时常用的工具:ch ...

  8. linux下如何实现mysql数据库定时自动备份

    概述   备份是容灾的基础,是指为防止系统出现操作失误或系统故障导致数据丢失,而将全部或部分数据集合从应用主机的硬盘或阵列复制到其它的存储介质的过程.而对于一些网站.系统来说,数据库就是一切,所以做好 ...

  9. LeetCode Search Insert Position (二分查找)

    题意: 给一个升序的数组,如果target在里面存在了,返回其下标,若不存在,返回其插入后的下标. 思路: 来一个简单的二分查找就行了,注意边界. class Solution { public: i ...

  10. 如何在SAP Server Side JavaScript里消费destination

    在SAP云平台里打开SAP HANA Web-Based Development Workbench进行服务器端JavaScript的开发. 创建一个新的package: 创建一个新的applicat ...