922 D. Robot Vacuum Cleaner

10 篇文章 0 订阅

题目链接:922D
D. Robot Vacuum Cleaner
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard 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
inputCopy
4
ssh
hs
s
hhhs
outputCopy
18
inputCopy
2
h
s
outputCopy
1
Note
The optimal concatenation in the first sample is ssshhshhhs.

题意:给你n个字符串,只包含字符s和h,你可以改变字符串的顺序,使得sh最多,当i<j ai=s,aj=h,
就算构成一个sh。

思路:如果要构成多的sh,那么一定要让字符串中s多的放前面,h多的放后面,所以只需要根据
字符串中s的个数/h的个数排序就可以了。

代码:

#include<bits/stdc++.h>
#define ULL unsigned long long
#define LL long long
#define Max 100005
#define mem(a,b) memset(a,b,sizeof(a));
#define pb push_back
#define mp make_pair
#define input ios::sync_with_stdio(false);cin.tie(0);
const LL mod=1e9+7;
const ULL base=131;
const LL LL_MAX=9223372036854775807;
using namespace std;
struct node
{
    string str;
    int s,h;
    double p;
}a[Max];
bool cmp(node a,node b){
    return a.p>b.p;
}
int sum[Max];
string str;
int main()
{
    input;
    int t;
    cin>>t;
    for(int i=1;i<=t;i++){
        cin>>a[i].str;
        int len=a[i].str.size(),s=0,h=0;
        for(int j=0;j<len;j++)
            if(a[i].str[j]=='s')
                s++;
            else
                h++;
            a[i].s=s;
            a[i].h=h;
            a[i].p=s/(h*1.0);
    }
    sort(a+1,a+t+1,cmp);
    for(int i=1;i<=t;i++)
        str=str.append(a[i].str);//不能用+,会超时
    //cout<<str<<endl;
    int len=str.size();
    sum[0]=str[0]=='s'?1:0;
    for(int i=1;i<len;i++){
        if(str[i]=='s')
            sum[i]=sum[i-1]+1;
        else
            sum[i]=sum[i-1];
    }
    LL ans=0;
    for(int i=0;i<len;i++)
        if(str[i]=='h')
            ans+=sum[i-1];
    cout<<ans<<endl;
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值