Codeforces Round #461 (Div. 2) D. Robot Vacuum Cleaner(sort)

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
output
18
inputCopy
2
h
s
output
1
Note
The optimal concatenation in the first sample is ssshhshhhs.
题意:给你n个字符串之包含s,h,要求将这n个字符串拼成一个字符串,问最多能组成多少个‘sh’(每个s都可以和后面的h组合形成一个‘sh’)。
思路:将这n个子串组合后暴力数答案即可。现考虑如何排序,对于两个字符串a,b的位置,区别只有:前面的字符串中的每个s都可以与后面字符串的h组成答案,只需要考虑这种情况下对于答案贡献的多少即可,即比较a.s*b.h与a.h*b.s的大小
注意:用string存子串,题目没有说明子串的长度。
中间变量用long long。
AC代码:

#include<cstdio>
#include<iostream>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<queue>
#include<vector>
#include<map>
#include<set>
#define met(s,k) memset(s,k,sizeof s)
#define scan(a) scanf("%d",&a)
#define scanl(a) scanf("%lld",&a)
#define scann(a,b) scanf("%d%d",&a,&b)
#define scannl(a,b) scanf("%lld%lld",&a,&b)
#define scannn(a,b,c) scanf("%d%d%d",&a,&b,&c)
#define prin(a) printf("%d\n",a)
#define prinl(a) printf("%lld\n",a)
using namespace std;
typedef long long ll;
const int maxn=505*505;
struct strin
{
    string str;
    ll len,s,h;
}cab[100005];
int cmp(strin a,strin b)
{
    return a.s*b.h>a.h*b.s;
}
ll cont,ans;
int main()
{
    int n;
    scan(n);
    for(int i=0;i<n;i++)
    {
        cin>>cab[i].str;
        cab[i].len=cab[i].str.size();
        for(int j=0;j<cab[i].len;j++)
        {
            if(cab[i].str[j]=='s')cab[i].s++;
            else cab[i].h++;
        }
    }
    sort(cab,cab+n,cmp);
    for(int i=0;i<n;i++)
    {
        for(int j=0;j<cab[i].len;j++)
        {
            if(cab[i].str[j]=='h')ans+=cont;
            else cont++;
        }
    }
    printf("%lld\n",ans);
    return  0;
}

猜你喜欢

转载自blog.csdn.net/swust5120160705/article/details/79381301