Codeforces 922 C - Robot Vacuum Cleaner (贪心、数据结构、sort中的cmp)

题目链接:点击打开链接

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
18
input
Copy
2
h
s
output
1
Note

The optimal concatenation in the first sample is ssshhshhhs.

题意:给一堆由s和h组成的字符串,让你拼接,使得子序列为“sh”的数量最多,并且输出这个最多的值

题解:对于每个给的字符串,无论怎么拼接,它本身拥有的sh是不变的,而将str1与str2拼接之后(str1在前)增加的sh数量为str1中的s数量乘str2中的h数量。

由此,可以构造数据结构,每个字符串拥有“s数量、h数量、sh数量”三个变量,注意struct的使用方法以及初始化方式。

接下来要解决的问题就是如何进行拼接,既然整体的sh数量只根据拼接方式决定,我们可以让s多的在前面?显然不阔仪。

我们将问题进行简化:如何拼接两个字符串可以让sh最多?授衔,两个字符串分别私有的的sh数量是无论如何都不变的,我们只看拼接之后增加的sh数量如何最多即可。如上面说的,我们只需要比较“str1中的s数量乘str2中的h数量”与“str2中的s数量乘str1中的h数量”哪个最多即可,前者多则str1排在前面,后者多str2排在前面。

知道如何排序了,怎么实现数据结构的排序呢?阔仪自己写一个冒泡,no! algorithm中的sort函数是可以对复杂数据结构进行排序的,只是要自己定义比较函数,所以嘿嘿。。。下面介绍sort中的cmp怎么写

cmp函数是一个bool类型的函数,假如我们定义的数据结构叫sh,那么bool cmp(sh a,sh b)是我们要定义的比较函数,如果返回true,则a排在前面,否则b排在前面  

(加粗染红下划线)一定要注意cmp函数一定要保证涵盖了所有的情况!也就是说只要调用成功,cmp一定要有返回值出来,不然就会re。text 5 的噩梦。。

这题如果还有坑点的话,那就是日常char类型读入回车。。也很好解决,看代码

#include<cstdio>
#include<cstring>
#include<algorithm>
#define min(a, b) a>=b?b:a
#define max(a, b) a>=b?a:b
typedef long long ll;
using namespace std;

struct sh{
	ll ns;
	ll nh;
	ll nsh;
	sh(): ns(0), nh(0), nsh(0){}
}str[100000+ 1000];

bool cmp(sh a, sh b){
	if(a.ns * b.nh > a.nh * b.ns) return true;
	else if(a.ns * b.nh < a.nh * b.ns) return false;
	else {
	    if(a.ns > b. ns) return true;
	    else return false;
    }
}
 
int main(){
	int n;
	scanf("%ld", &n);
	char d;
	scanf("%c", &d);//接收回车符 
	for(int i = 0; i < n; i++){
		while(1){
		
		    scanf("%c", &d);
		    if(d == 's') str[i].ns++;
		    else if (d == 'h') {str[i].nh++;str[i].nsh+=str[i].ns;}
	    	else break;
	    }
		
	}
	sort(str, str + n, cmp);
	long long cnt = 0;
	long long cnts = 0, cnth = 0;
	for(int i = 0; i < n ; i++){
		cnt+= str[i].nsh + cnts * str[i].nh;
		cnts += str[i].ns;
	}
	printf("%lld\n", cnt);
	return 0;
}

 

转载于:https://www.cnblogs.com/wrjlinkkkkkk/p/9552015.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
引用\[1\]提到了一种树形动态规划的方法来解决CodeForces - 982C问题。在这个问题,subtree指的是子连通块,而不是子树。为了使cnt_white - cnt_black尽可能大,可以使用两次树形动态规划来求解。第一次是自底向上的过程,维护一个dp数组,表示以每个节点为根的子树的最大连通块。第二次是自顶向下的过程,处理自底向上过程无法包含的树链所代表的子树。在第二次遍历,需要维护一个sum变量,用于存储树链所代表的子树的贡献。根据ans\[u\]的正负,决定是否能对相邻的子节点做出贡献。如果ans\[u\]为正,则减去dp\[v\]就是树链所代表的子树的权值。最终,ans\[u\]代表包含节点u在内的子连通块的最大权值。\[1\] 问题: CodeForces - 982C 树形DP是什么问题?如何解决? 回答: CodeForces - 982C是一个树形动态规划问题。在这个问题,需要求解子连通块的最大权值和,使得cnt_white - cnt_black尽可能大。解决这个问题的方法是使用两次树形动态规划。第一次是自底向上的过程,维护一个dp数组,表示以每个节点为根的子树的最大连通块。第二次是自顶向下的过程,处理自底向上过程无法包含的树链所代表的子树。在第二次遍历,需要维护一个sum变量,用于存储树链所代表的子树的贡献。根据ans\[u\]的正负,决定是否能对相邻的子节点做出贡献。最终,ans\[u\]代表包含节点u在内的子连通块的最大权值。\[1\] #### 引用[.reference_title] - *1* *2* [CodeForces - 1324F Maximum White Subtree(树形dp)](https://blog.csdn.net/qq_45458915/article/details/104831678)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^koosearch_v1,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值