CodeForces - 922D Robot Vacuum Cleaner (贪心)

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 = 100086;
const int maxm = 100086;
const int inf = 2.1e9;
const ll Inf = 999999999999999999;
const int mod = 1000000007;
const double eps = 1e-6;
const double pi = acos(-1);

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=0;
    for(int i=1;i<=n;i++){
        scanf("%s",s);
        for(int j=0;s[j];j++){
            if(s[j]=='s'){a[i].S++;}
            else{
                ans+=a[i].S;
                a[i].H++;
            }
        }
    }

    sort(a+1,a+1+n,cmp);


    ll tmp=0;
    for(int i=1;i<=n;i++){

        ans+=a[i].H*tmp;
        tmp+=a[i].S;
    }
    printf("%lld\n",ans);

    return 0;
}
View Code

猜你喜欢

转载自www.cnblogs.com/ZGQblogs/p/10848108.html