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. Python编程实现USB转RS485串口通信

    ---作者吴疆,未经允许,严禁转载,违权必究--- ---欢迎指正,需要源码和文件可站内私信联系--- -----------点击此处链接至博客园原文----------- 功能说明:Python编程 ...

  2. windows 2012 r2 x64 安装IIS注意事项

    详细安装可以参考下面; https://jingyan.baidu.com/article/93f9803f234eade0e46f559f.html 下面只说一些注意事项,如果项目要用到wcf 的话 ...

  3. C 碎片十一 扫雷源码

    // C语言版本扫雷 #include <stdio.h> #include <stdlib.h> /* 1(0,0) 1(0,1) 0(0,2) 1(0,3) 1(0,4) ...

  4. Java8 如何正确使用 Optional

    原文出处:https://blog.kaaass.net/archives/764 Optional是Java8提供的为了解决null安全问题的一个API.善用Optional可以使我们代码中很多繁琐 ...

  5. html5 知识总结

    Meta基础知识:  H5页面窗口自动调整到设备宽度,并禁止用户缩放页面    //一.HTML页面结构<meta name="viewport" content=" ...

  6. Life here can be a dream come true!

    Life here can be a dream come true!美梦迟早会成真的!

  7. Android仿微信高效压缩图片(libjpeg)

    用过ios手机的同学应该很明显感觉到,ios拍照1M的图片要比安卓拍照排出来的5M的图片还要清晰.这是为什么呢? 这得了解android底层是如何对图片进行处理的. 当时谷歌开发Android的时候, ...

  8. web安全防御之RASP技术

    作者:      我是小三 博客:      http://www.cnblogs.com/2014asm/ 由于时间和水平有限,本文会存在诸多不足,希望得到您的及时反馈与指正,多谢! 0x00:we ...

  9. 《Unity預計算即時GI》笔记:二、光照图

    说明 这篇文章是对<Unity預計算即時GI>这个系列文章的笔记. 光照图 什么是光照图 光照图在第三章中有如下的定义,读起来很是费解. 一個光照圖(Chart)是表示一個光照貼圖的區域, ...

  10. DB错误代码大全

    db2错误代码大全  sqlcode sqlstate 说明000 00000 SQL语句成功完成01xxx SQL语句成功完成,但是有警告+012 01545 未限定的列名被解释为一个有相互关系的引 ...