Codeforces Round #461 (Div. 2) D. Robot Vacuum Cleaner[贪心+sort]

本文介绍了一种算法问题,目标是在给定多个只包含字符S和H的字符串时,通过调整这些字符串的顺序来最大化“SH”子序列的数量。文章详细解释了问题背景及解决思路,并提供了完整的代码实现。

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

D. Robot Vacuum Cleaner
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

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
Copy
4
ssh
hs
s
hhhs
output
18
input
Copy
2
h
s
output
1
Note

The optimal concatenation in the first sample is ssshhshhhs.


题意:给n个字符串,仅由s和h组成,不改变每个字符串的内部顺序,但可以改变字符串的出现顺序.问最多能出现多少个sh子序列???

思路:假设现在对比2个字符串s1,s2  .   那么如果  s1.cnts*s2.cnth>s2.cnts*s1.cnth 那么说明s1在s2前面是最好的,移项可知,串中s:h比例最大的放前面对答案的贡献值最大.

#include <bits/stdc++.h>
#define MOD 1000000007
#define INF 0x3f3f3f3f
#define bug cout << "bug" << endl
#pragma comment(linker, "/STACK:102400000,102400000")


using namespace std;
typedef long long ll;

const int MAX_N=1e5+5;

struct node{
    string s;
    int cnts,cnth;
    double rate;
    node(){
        cnts=0,cnth=0,rate=0;
    }
}p[MAX_N];

bool cmp(const node a,const node b){
    return a.rate>b.rate;
}

int main(void){
    int n;
    cin >> n;
    ll toth=0;
    for(int i=1;i<=n;i++){
        cin >> p[i].s;
        for(int j=0;j<(int)p[i].s.size();++j){
            if(p[i].s[j]=='s'){
                p[i].cnts++;
            }
            else toth++,p[i].cnth++;
        }
        if(p[i].cnth==0)    p[i].rate=INF;
        else p[i].rate=(double)p[i].cnts/p[i].cnth;
    }
    sort(p+1,p+1+n,cmp);
    ll ans=0;
    for(int i=1;i<=n;i++){
        for(int j=0;j<(int)p[i].s.size();++j){
            if(p[i].s[j]=='s')  ans+=toth;
            else    toth--;
        }
    }
    cout << ans << endl;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值