2020 年 “联想杯”全国高校程序设计在线邀请赛暨第三届上海理工大学程序设计竞赛C. Cheat Sheet

题目链接:题目
单点时限: 1.0 sec
内存限制: 256 MB

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.
思路:
用set自动去重,然后选取小的
输入:

40 5
myworld lusto KR12138 oneman233 SetsunaQAQ

输出:

4

代码:

#include
using namespace std;
const int P=1333331;
int n,m,h;
int b[1010];
string a;
set<string> ss;//具有自动去重 
int main()
{
 cin>>n>>m;
 for(int i=1;i<=m;i++)
 {
  cin>>a;
  ss.insert(a);
 }
 int cnt=0;
 for (auto it : ss)//遍历 
  b[++cnt] = it.size();
 sort(b+1,b+1+cnt);//排序 
 int ans=0,sum=0;
 for(int i=1;i<=cnt;i++)
 {
  if(sum+b[i]<=n)
  {
   ans++;
   sum+=b[i]+1;
  }
 }
 cout<<ans<<endl;
 return 0;
}

你可能感兴趣的:(2020 年 “联想杯”全国高校程序设计在线邀请赛暨第三届上海理工大学程序设计竞赛C. Cheat Sheet)