版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/Lfhase/article/details/79288612
题目链接:点击打开链接
题意:给n个仅包含s和h的字符串,排列这n个字符串,使得这样的(i,j)对最多,即i<j且t_i = s,t_j = h。
思路:使用排序的思想,每个字符串看作一个单位,对于两个字符串A和B,只考虑其先后顺序。若AB组合的对数大于BA,则需要A在B前,否则B在A前。
AC代码:
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <cstring>
#include <string>
#include <cmath>
#include <string>
using namespace std;
#define INREP(i,b,e) for(i=b;i<=e;++i)
#define DEREP(i,b,e) for(i=b;i>=e;--i)
#define FSIO ios::sync_with_stdio(0);cin.tie(0);
#define DEBUG(a) cout<<"DEBUG: "<<(a)<<endl;
const int MAXN = 1e9;
struct sbs{
long long cnts;
long long cnth;
string s;
};
bool operator <(const sbs& a, const sbs& b)
{
return a.cnts*b.cnth > b.cnts*a.cnth;
}
sbs sss[100005];
int main()
{
FSIO;
int n;
while(cin>>n)
{
for(int i=1;i<=n;++i)
{
cin>>sss[i].s;
sss[i].cnts = sss[i].cnth = 0;
for(int j=0;j<sss[i].s.length();++j)
{
if(sss[i].s[j] == 's') sss[i].cnts++;
else sss[i].cnth++;
}
}
sort(sss+1,sss+1+n);
long long ans = 0;
long long cnt = 0;
for(int i=1;i<=n;++i)
{
//cout<<sss[i].s;
for(int j=0;j<sss[i].s.length();++j)
if(sss[i].s[j]=='s') cnt++;
else ans += cnt;
}
cout<<ans<<endl;
}
return 0;
}