【链接】 我是链接,点我呀:)

【题意】

让你把n个字符串重新排序,然后按顺序连接在一起
使得这个组成的字符串的"sh"子序列最多

【题解】

```java
/*
* 假设A的情况好于B
* 也就对应了A排在B的前面
* 那么
* As*Bh>Ah*Bs ①
* 现在假设B又比C来得好
* 那么有
* Bs*Ch>Bh*Cs ②
* 由①可得
* As/Ah>Bs/Bh
* 由②可得
* Bs/Bh>Cs/Ch
* 那么有
* As/Ah>As/Ch
* 即As*Ch>Ah*As
* 也就是说A排在C的前面比较优秀
* 也就是说这个大小关系有传递性
* 那么就直接排个序就好啦,按照两两之间排序的规则来写个比较函数就好了。
*/
```
StringBuilder比直接用字符串的"+"来得快

【代码】

import java.io.*;
import java.util.*; public class Main { static InputReader in;
static PrintWriter out; public static void main(String[] args) throws IOException{
//InputStream ins = new FileInputStream("E:\\rush.txt");
InputStream ins = System.in;
in = new InputReader(ins);
out = new PrintWriter(System.out);
//code start from here
new Task().solve(in, out);
out.close();
} static int N = (int)1e5;
static class Task{ class Pair{
int s,h,i;
public Pair(int s,int h,int i) {
this.s = s;
this.h = h;
this.i = i;
}
} public void solve(InputReader in,PrintWriter out) {
Pair a[] = new Pair[N+10];
String s[] = new String[N+10];
int n;
n = in.nextInt();
for (int i = 1;i <= n;i++) {
s[i] = in.next();
int ss = 0,hh = 0;
for (int j = 0;j < (int)s[i].length();j++) {
char key = s[i].charAt(j);
if (key=='s') ss++;
if (key=='h') hh++;
}
a[i] = new Pair(ss,hh,i);
}
Arrays.sort(a, 1,n+1,new Comparator<Pair>() { @Override
public int compare(Pair A, Pair B) {
// TODO Auto-generated method stub
long As,Ah,Bs,Bh;
As = A.s;Ah = A.h;
Bs = B.s;Bh = B.h;
long temp1 =As*Bh;long temp2 =Ah*Bs;
if (temp1>temp2) {
return -1;
}else if (temp1==temp2) {
return 0;
}else {
return 1;
}
} });
StringBuilder sb = new StringBuilder();
for (int i = 1;i <= n;i++) {
sb.append(s[a[i].i]);
}
String v = sb.toString();
n = v.length();
long ans = 0;
long cnts = 0;
for (int i = 0;i < n;i++) {
if (v.charAt(i)=='s') {
cnts++;
}else if (v.charAt(i)=='h'){
ans = ans + cnts;
}
}
out.println(ans);
}
} static class InputReader{
public BufferedReader br;
public StringTokenizer tokenizer; public InputReader(InputStream ins) {
br = new BufferedReader(new InputStreamReader(ins));
tokenizer = null;
} public String next(){
while (tokenizer==null || !tokenizer.hasMoreTokens()) {
try {
tokenizer = new StringTokenizer(br.readLine());
}catch(IOException e) {
throw new RuntimeException(e);
}
}
return tokenizer.nextToken();
} public int nextInt() {
return Integer.parseInt(next());
}
}
}

最新文章

  1. WeakReference
  2. poj3484 Showstopper 二分
  3. 【golang】go语言,进行并发请求的wrap变参封装
  4. Configuring a Windows Azure Project
  5. BZOJ 1266 上学路线route(最小割)
  6. UIDevice通知
  7. 将表A的数据复制到表B,以及关于主表和子表的删除办法
  8. xml转array
  9. Debug和Release之本质区别
  10. Subsequence(两个单调队列)
  11. java 写文件解析
  12. Mysql ODBC 5.1 Driver免安装脚本
  13. mysql 多个字段拼接
  14. Powerdesigner+PostgreSQL
  15. 从零学习Fluter(五):Flutter中手势滑动拖动已经网络请求
  16. .net core 2.x - 日志 - to elasticsearch - (2)
  17. Mybatis SqlsessionFactory
  18. hashmap源码研究
  19. 解决刚刚安装完mysql 远程连接不上问题
  20. [ZJOI2012]旅游

热门文章

  1. python中socket编程
  2. 汇编程序52:实验15 安装新的int9中断例程
  3. 树形DP UVA 1292 Strategic game
  4. 使用wkwebview后,页面返回不刷新的问题
  5. 牛客网-3 网易编程题(1拓扑&amp;2二叉树的公共最近祖先&amp;3快排找第K大数)
  6. Storm编程入门API系列之Storm的可靠性的ACK消息确认机制
  7. Windows Azure中文博客 Windows Azure入门教学系列 (一): 创建第一个WebRole程序
  8. Kerberos 简介——教你做个好人
  9. Java编程思想读书笔记_第8章
  10. P2668 斗地主 dp+深搜版