CodeForces 922D Robot Vacuum Cleaner(贪心)

3 篇文章 0 订阅
D. Robot Vacuum Cleaner
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard 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
input
Copy
4
ssh
hs
s
hhhs
output
Copy
18
input
Copy
2
h
s
output
Copy
1
Note

The optimal concatenation in the first sample is ssshhshhhs.

题意改变字符串的顺序,看最多能有多少个i,j使得i<j,ti==‘s',tj=='h',是改变这几个字符串之间的顺序,而内部顺序是不能变的。

我们假设TA和TB分别为两个字符串,

如果顺序是TATB的话,这种情况下的'sh'数就是 A的‘sh'数+B的’sh‘数+B的'h'数 * A的‘s'数  即 Ash+Bsh+Bh*As

如果是TBTA的顺序的话, 总数为 A的‘sh'数+B的’sh‘数+A的'h'数 * B的‘s'数,即 Ash+Bsh+Ah*Bs

比较之后得到,如果 Bh*As>Ah*Bs ,就把A排在前面,如果 Bh*As<Ah*Bs 就把B放在前面

那相等的情况呢,我们很容易就能想到,s多的串放在前面当然就可以让答案更大。

所以我们创建这样的一个结构体

[cpp]  view plain  copy
  1. struct node  
  2. {  
  3.     long long ns; //s的个数  
  4.     long long nh; //h的个数  
  5.     long long ans;  //该串的sh数  
  6. };  
然后根据上面的方法进行排序,就可以得到解了
我本来是先把字符串整个读进来再计算s数,h数之类的,但是超时了,所以改成了读字符算

下面是代码:
[cpp]  view plain  copy
  1. #include <bits/stdc++.h>  
  2. #include <cstring>  
  3. using namespace std;  
  4. char s[100005];  
  5.   
  6. struct node  
  7. {  
  8.     long long ns;  
  9.     long long nh;  
  10.     long long ans;  
  11. }arr[100005];  
  12.   
  13. bool comp(node a,node b)  
  14. {  
  15.     if(a.nh*b.ns<a.ns*b.nh)  
  16.         return true;  
  17.     else if(a.nh*b.ns>a.ns*b.nh)  
  18.         return false;  
  19.     else  
  20.     {  
  21.         if(a.ns>b.ns)  
  22.             return true;  
  23.         if(a.ns<b.ns)  
  24.             return false;  
  25.         return a.nh<b.nh;  
  26.     }  
  27. }  
  28. int main()  
  29. {  
  30.     int n;  
  31.     scanf("%d ",&n);  
  32.     char c;  
  33.     for(int i=0;i<n;i++)  
  34.     {  
  35.         arr[i].nh=arr[i].ns=arr[i].ans=0;  
  36.         while(1)  
  37.         {  
  38.             scanf("%c",&c);  
  39.             if(c=='\n')  
  40.                 break;  
  41.             if(c=='s')  
  42.                 arr[i].ns++;  
  43.             else  
  44.             {  
  45.                 arr[i].nh++;  
  46.                 arr[i].ans+=arr[i].ns;  
  47.             }  
  48.         }  
  49.   
  50.     }  
  51.     sort(arr,arr+n,comp);  
  52.     long long ans=0;  
  53.     long long num=0;  
  54.     for(int i=0;i<n;i++)  
  55.     {  
  56.         ans+=arr[i].ans+arr[i].nh*num;  
  57.         num+=arr[i].ns;  
  58.     }  
  59.     printf("%I64d\n",ans);  
  60.     return 0;  
  61. }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值