Pushok the dog has been chasing Imp for a few hours already.
![](https://i-blog.csdnimg.cn/blog_migrate/15286d3d3176bc0eb4f37ca5b67169c4.png)
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.
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.
Print a single integer — the maxumum possible noise Imp can achieve by changing the order of the strings.
4 ssh hs s hhhs
18
2 h s
1
The optimal concatenation in the first sample is ssshhshhhs.
题意:就是让你将这写字符串拼接成一个,要求子序列出现sh的次数要最多,并且打印出现的次数。
思路:其实就是一个贪心,让s尽可能在前,h尽可能在后边,由于字符串不能拆分,则让s占比重大的字符串在前边,
让h占比重大的字符串在后边
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
typedef long long ll;
const int N = 1e5+5;
struct node{
int s;//s的数量
int sh;//sh的数量
int h;//h的数量
double p;// s/h
}str[N];
int cmp(node a,node b){
return a.p>b.p;
}
int main(){
int n;
scanf("%d",&n);
for(int i=0;i<n;i++){
char ss[N];
scanf("%s",ss);
int len=strlen(ss);
int s=0,h=0,sh=0;
for(int j=0;j<len;j++){
if(ss[j]=='s') s++;
else{
sh+=s;
h++;
}
}
str[i].s=s;
str[i].h=h;
str[i].sh=sh;
if(h==0){//注意h为0的情况
str[i].p=N;
continue;
}
str[i].p=1.0*s/h;
}
sort(str,str+n,cmp);
ll ans=0,cnt=0;
for(int i=0;i<n;i++){
ans+=str[i].sh;
ans+=str[i].h*cnt;
cnt+=str[i].s;
}
printf("%lld\n",ans);
return 0;
}