Cheat Sheet (小抄) Visual Judge (Set3)


题目描述

Cheat Sheet (小抄)

University of Shanghai for Science and Technology starts a course called Film Appreciation of Black Album recently. To be the best “Blackologist” in the university, Setsuna is actively preparing for the exam.

The examination of the course is open book; that is to say, you can only take one single-sided cheat sheet to the exam. The cheat sheet can write n characters at most.

Setsuna has m keywords that she wants to write on the cheat sheet. Her memory is not very good, so there may be some duplicate keywords. Each keyword consists of several visible characters(visible characters refer to characters with ASCII code between 33 and 126 inclusive).

For both readability and neatness, keywords written on the cheat sheet should be separated by at least one space and must be different from each other.

Setsuna wants to know how many distinct keywords she can write down on the cheat sheet at most.

Uppercase and lowercase letters are considered different characters.


输入描述

The first line contains two integers n,m(1≤n,m≤1000).

The second line contains m keywords separated by exactly one space. The length of each keyword is no more than 100. It is guaranteed that keyword only consists of visible characters.


输出描述

Output one integer indicating the answer.


输入输出样例

//Example 1
//Input
40 5
myworld lusto KR12138 oneman233 SetsunaQAQ

//Output
4
//Example 2
//Input
7 2
^_^ ^_^

//Output
1

线索提示

In sample 1, it takes 42 characters to write all the words down. So Setsuna can write down at most four.

In sample 2, there is only one keyword.


一、解题思路

上面说了一大堆,翻译过来的人话就是:

现在我们的主人公要做小抄,但是小抄不能无限长最多为n,有长度限定,但是我们的主人公又要有m个字要写。题目中有以下几点是值得注意的。

  • 第一行的输入包含两个整数n,m(1≤n,m≤1000),分别代表小抄的最大限制字符数和我们亲爱的主人公想要抄上去的字符个数;
  • 题目中要求我们抄上去的单词不能重复
  • 要求我们抄上去的单词的个数越多越好
  • 但是抄上去的单词个数不能超过所给的最大长度n(包含空格)

对于上述的要求我们可以得到对应的解题步骤

  1. 不能重复 : 使用STL标准库中的set来进行筛选,将每一个字符String存进set中;
  2. 越多越好 : 创建一个int数组,统计set中每一个string的字符的个数,接着对int数组进行排序;
  3. 最大长度 : 遍历一遍上一步中创建的int数组,计算最大字符数和字符长度。

二、题解

源代码

代码如下:

/*
* Author:	FeverTwice
* Date:		2021-05-02
* Func:		Solution for Cheat Sheet
*/

#include <iostream>
#include <cstdio>
#include <string>
#include <algorithm>
#include <math.h>
#include <functional>
#include <set>

#define debug(a) cout<<#a<<"="<<a<<endl;
#define lyh(i,a,b) for(int i=a;i<=b;i++)
#define hyl(i,a,b) for(int i=a;i>=b;i--)
#define LL long long
#define mm memset
#define EPS 1e-8
#define INF 0x7fffffff
using namespace std;

set<string> sestr;			//创建具有自动去重的集合setstr

int main()
{
	int cnt = 0, res = 0, len = 0;	//cnt:创建字典后的元素个数,res:最大小抄数,len:总长度
	int l[1020] = {};		//记录字典内元素的长度

	int n, m;
	cin >> n >> m;

	string str;

	for (int i = 0; i < m; i++)
	{
		cin >> str;
		sestr.insert(str);
	}

	for (auto iter : sestr)
	{//迭代器迭代统计对应的字符个数
		l[cnt++] = iter.size();
	}

	sort(l, l + cnt);			//排序,得到字符长度

	for (int i = 0; i < cnt; i++)
	{
		if (len + l[i] <= n)
		{
			res++;				//字符个数加一
			len += (l[i] + 1);	//+1是为了模拟加上后面的空格
		}
	}

	cout << res << endl;

	return 0;
}

VJudge评判结果

VJAC


参考文献

[1]2020 年 “联想杯”全国高校程序设计在线邀请赛暨第三届上海理工大学程序设计竞赛C. Cheat Sheet, Author : 容艾假


写在最后

各位看官,都看到这里了,麻烦动动手指头给博主来个点赞8,您的支持作者最大的创作动力哟! <(^-^)>
才疏学浅,若有纰漏,恳请斧正
本文章仅用于各位同志作为学习交流之用,不作任何商业用途,若涉及版权问题请速与作者联系,望悉知

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值