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
Input
2
h
s
Output
1

题意:
给定n个字符串自由组合,求最多出现多少个sh这样的子序列。
思路:
对于s1,s2,产生字符串的个数就是S1.SH+s2*SH+max(s1.S*s2.H+s2.S*s1.H)
所以按照s1.S*s2.H-s2.S*s1.H排序即可。
#include<iostream>
#include<algorithm>
#include<vector>
#include<stack>
#include<queue>
#include<map>
#include<set>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<ctime>
#define fuck(x) cout<<#x<<" = "<<x<<endl;
#define debug(a,i) cout<<#a<<"["<<i<<"] = "<<a[i]<<endl;
#define ls (t<<1)
#define rs ((t<<1)+1)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int maxn = ;
const int maxm = ;
const int inf = 2.1e9;
const ll Inf = ;
const int mod = ;
const double eps = 1e-;
const double pi = acos(-); struct node{
ll S,H;
}a[maxn]; char s[maxn]; bool cmp(node a,node b){
return a.S*b.H>a.H*b.S;
} int main()
{
// ios::sync_with_stdio(false);
// freopen("in.txt","r",stdin); int n;
scanf("%d",&n);
ll ans=;
for(int i=;i<=n;i++){
scanf("%s",s);
for(int j=;s[j];j++){
if(s[j]=='s'){a[i].S++;}
else{
ans+=a[i].S;
a[i].H++;
}
}
} sort(a+,a++n,cmp); ll tmp=;
for(int i=;i<=n;i++){ ans+=a[i].H*tmp;
tmp+=a[i].S;
}
printf("%lld\n",ans); return ;
}

CodeForces - 922D Robot Vacuum Cleaner (贪心)的更多相关文章

  1. Codeforces 922 C - Robot Vacuum Cleaner (贪心、数据结构、sort中的cmp)

    题目链接:点击打开链接 Pushok the dog has been chasing Imp for a few hours already. Fortunately, Imp knows that ...

  2. CF922D Robot Vacuum Cleaner 贪心+排序

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

  3. 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 ...

  4. 【Codeforces 922D】Robot Vacuum Cleaner

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

  5. codeforces Gym 100338E Numbers (贪心,实现)

    题目:http://codeforces.com/gym/100338/attachments 贪心,每次枚举10的i次幂,除k后取余数r在用k-r补在10的幂上作为候选答案. #include< ...

  6. [Codeforces 1214A]Optimal Currency Exchange(贪心)

    [Codeforces 1214A]Optimal Currency Exchange(贪心) 题面 题面较长,略 分析 这个A题稍微有点思维难度,比赛的时候被孙了一下 贪心的思路是,我们换面值越小的 ...

  7. Codeforces 583 DIV2 Robot's Task 贪心

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

  8. 489. Robot Room Cleaner扫地机器人

    [抄题]: Given a robot cleaner in a room modeled as a grid. Each cell in the grid can be empty or block ...

  9. codeforces 349B Color the Fence 贪心,思维

    1.codeforces 349B    Color the Fence 2.链接:http://codeforces.com/problemset/problem/349/B 3.总结: 刷栅栏.1 ...

随机推荐

  1. 微信小程序组件——bindtap和catchtap的区别

    了解知识点 DOM模型是一个树形结构,在DOM模型中,HTML元素是有层次的.当一个HTML元素上产生一个事件时,该事件会在DOM树中元素节点与根节点之间按特定的顺序传播,路径所经过的节点都会收到该事 ...

  2. 微信小程序错误——mpvue小程序:未找到 app.json 中的定义的 pages "pages/XXX/XXX" 对应的 WXML 文件

    背景 在刚开始学习开发小程序时,使用微信开发工具在app.json建立页面,写好配置文件名称后,应该会自动生成页面的4个文件,结果没有生成文件,反而报错:mpvue小程序:未找到 app.json 中 ...

  3. select引起的服务端程序崩溃问题

    现象: 某个线上的服务最近频繁崩溃.该服务使用C++编写,是个网络服务端程序.作为TCP服务端,接收和转发客户端发来的消息,并给客户端发送消息.该服务跑在CentOS上,8G内存.线上环境中,与客户端 ...

  4. SpringBoot @Transactional的rollbackFor属性

    1.简单回顾Java Exception 该图摘自:https://blog.csdn.net/zhangerqing/article/details/8248186 一方面,我们可以将异常分为运行时 ...

  5. 二维数组初始化 遍历 动态赋值 内存图 Day08

    package com.sxt.arraytest3; /* * 二维数组 */ public class TestArray { public static void main(String[] a ...

  6. chrome谷歌浏览器怎么清除指定网站cookie

    https://jingyan.baidu.com/article/fa4125aced30cc28ac709230.html 在使用电脑的情况下,由于到部分网站的cookie的问题导致的部分功能失效 ...

  7. hdu 1384 Intervals (差分约束)

    Problem - 1384 好歹用了一天,也算是看懂了差分约束的原理,做出第一条查分约束了. 题意是告诉你一些区间中最少有多少元素,最少需要多少个元素才能满足所有要求. 构图的方法是,(a)-> ...

  8. Ubuntu 14.04 使用ntfs-config解决开机自动挂载NTFS分区的方法

    先安装: sudo apt-get install ntfs-3g ntfs-config 再配置一下: sudo ntfs-config 然后就会弹出来一个对话框,选择你需要挂载的分区,点应用,再选 ...

  9. 2018-9-28-WPF-自定义-TextBoxView-的-Margin-大小

    title author date CreateTime categories WPF 自定义 TextBoxView 的 Margin 大小 lindexi 2018-09-28 17:16:17 ...

  10. JavaScript中判断整数的方法

    一.使用取余运算符判断 任何整数都会被1整除,即余数是0.利用这个规则来判断是否是整数. 1 2 3 4 5 function isInteger(obj) {     return obj%1 == ...