Codeforces Round #461 D(排序)

本文介绍了一道编程题的解法,题目要求通过重新排列给定的字符串序列,使组合后的字符串满足特定条件的字符对数量达到最大。文章详细解释了解决方案,并提供了一个具体的实现代码示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Robot Vacuum Cleaner

time limit per test

1 second

memory limit per test

256 megabytes

 

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

Note

The optimal concatenation in the first sample is ssshhshhhs.

题目大意:给定 n 个字符串,重排他们的顺序,使得连起来后满足条件的 (i,j) 数最多(其中 i < j 且 t[i] = 's' 且 t[j] = 'h')。

其实解法很简单,贪心即可,就是对这 n 个字符串重排序。比较两个字符串时,例如 x、y,若 x 在前 y 在后时(i,j)数多则按 x 在前排,这样可保证答案尽可能多。

排序后统计一下就得到答案。

代码:

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define ll long long
using namespace std;
const int maxn = 1e5 + 5;
string a[maxn];
bool cmp(string x,string y)
{
    ll s1 = 0,s2 = 0;
    ll h1 = 0,h2 = 0;
    ll sum1 = 0,sum2 = 0;
    ll len1 = x.length();
    ll len2 = y.length();
    for(int i = 0;i < len1; ++i)
    {
        if(x[i] == 's') s1++;
        if(x[i] == 'h') sum1 += s1,h1++;
    }
    for(int i = 0;i < len2; ++i)
    {
        if(y[i] == 's') s2++;
        if(y[i] == 'h') sum2 += s2,h2++;
    }
    return s1 * h2 > s2 * h1;
}
int main()
{
    int n;
    ll s = 0;
    ll sum = 0;
    scanf("%d",&n);
    for(int i = 0;i < n; ++i) cin >> a[i];
    sort(a,a + n,cmp);
    for(int i = 0;i < n; ++i)
    {
        int len = a[i].length();
        for(int j = 0;j < len; ++j)
        {
            if(a[i][j] == 's') s++;
            if(a[i][j] == 'h') sum += s;
        }
    }
    printf("%I64d\n",sum);
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值