1. 程式人生 > >CF R461 D.2 D. Robot Vacuum Cleaner

CF R461 D.2 D. Robot Vacuum Cleaner

D. Robot Vacuum Cleanertime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard 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.

Examplesinput
4
ssh
hs
s
hhhs
output
18
input
2
h
s
output
1
Note

The optimal concatenation in the first sample is ssshhshhhs.


題意· 已知n串只含's'和'h'的字串  對著n串字串進行排序組成新的一串字元 使得新字串中能組成‘sh’的數目最多

思路:s 和後面的 h 也能組成 sh   要得到最多的 sh ,s 所佔比重大的字串應該放到前面 這樣排出來的字串就是要求的字串

ac程式碼

#include<bits/stdc++.h>
#define inf 0x3f3f3f3f

using namespace std;

struct T
{
    string str;
    int nums;
    int numh;
    float val;
};

bool cmp(T a,T b)
{
    return a.val > b.val;
}


T t[100005];
int main()
{
    int n;
    scanf("%d",&n);

    long long cnth = 0;
    long long sum = 0;
    for(int i = 0; i < n; i++)
    {
        t[i].nums = 0;
        t[i].numh = 0;
        cin>>t[i].str;

        for(int k = 0; t[i].str[k] != '\0'; k++)
        {
            if(t[i].str[k] == 's')
                t[i].nums++;
            else if(t[i].str[k] == 'h')
                t[i].numh++;
        }
        cnth += t[i].numh;
        if(t[i].numh == 0)
        t[i].val = inf;
        else if(t[i].nums == 0)
            t[i].val = 0;
        else
            t[i].val = (float)t[i].nums/t[i].numh;

    }

    sort(t,t+n,cmp);
    for(int i = 0; i < n; i++)
    {
        for(int j = 0; t[i].str[j] != '\0'; j++)
        {
            if(t[i].str[j] == 's')
                sum += cnth;
            else if(t[i].str[j] == 'h')
                cnth--;
        }
    }

    printf("%lld\n",sum);

    return 0;
}

剛開始的時候結構體定義用了字元陣列  結果空間爆了.....  最後看了一下別人過的程式碼之後改成了 string 。。。