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. sql server sql语句

    添加联合唯一索引 create unique index 索引名 on 表名(列名1,列名2……)

  2. HTML <pre> 标签

    需求 错落有致的规则说明 ps.我真的是一个后端开发... pre 元素中的文本通常会保留空格和换行符.而文本也会呈现为等宽字体.

  3. webpack webpack-dev-server报错

    Error: getaddrinfo ENOTFOUND localhost at errnoException (dns.js:28:10) at GetAddrInfoReqWrap.onlook ...

  4. 两道sql面试题

    两道sql面试题:    1. 数据库表A的数据如下:             year   quarter             2001      1             2001      ...

  5. H5移动端原生长按事件

    // 函数名longpress// 参数为: 需长按元素的id.长按之后处理函数func function longPress(id, func,timeout=500) { var timeOutE ...

  6. JavaSE_5_线程

    1.多线程中的i++线程安全吗?为什么? 不安全,因为每个线程都有自己的工作内存,每个线程需要对共享变量操作时必须把共享变量从主内存中加载到自己的工作内存,等完成操作后再保存到内存中,如果一个线程运算 ...

  7. win10 asp+access

    今天是灰色的一天. 大清早来到单位,告知:单位主页访问不了! 我远程看了下.所有的文件后缀都变成了.crab 赶紧上网查下,哎呀我的妈呀,这是中了勒索病毒啊. 还用查?打开服务器,有个打开的文本文件写 ...

  8. eaysui 子页面刷新父页面datagrid

    近期碰到这样一个问题,子页面操作后需要刷新父页面datagrid元素,刚开始用这种方式刷新,$("#talbe",window.parent.document).datagrid( ...

  9. Java笔记--动态代理

    Java动态代理 1.概念 代理: 有时我们并不想直接访问对象A,或者不能直接访问对象A.而是通过访问一个中间对象B,让中间对象B去访问A.这种方式就称为代理. 这里的对象A所属的类就为委托类,或者被 ...

  10. CRM User Status profile中Business Transaction字段的用途

    有朋友问到User Status profile中Business Transaction字段的用途,如下图INPR, FINI所示. 实际上,这个字段作为一个桥梁,连接了User Status和Sy ...