2020 年 “联想杯”全国高校程序设计在线邀请赛暨第三届上海理工大学程序设计竞赛 C. 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.

样例:

input

40 5

myworld lusto KR12138 oneman233 SetsunaQAQ

output

4

 input

7 2

^_^ ^_^

output

1

 题意:

你有一张能最多写n个字符的纸,m个可能重复的单词,你需要挑选一些不重复的写到纸上,单词之间必须用空格分隔,问最多能写下几个不同的单词

分析:

  • 利用set保留不重复的字符串
  • sort函数 根据字符串长度排序

 

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
using namespace std;

vector v;
bool cmp(string x, string y)
{
    return x.size() < y.size();
}

int main ()
{
    int n, m;
    cin >> n >> m;

    for(int i=0; i> ss;
        v.push_back(ss);
    }
    
    //利用set对vector去重
    set st(v.begin(), v.end());
    v.assign(st.begin(), st.end());

    //按照字符串长度进行排序
    sort(v.begin(), v.end(), cmp);

    //计算
    int ans = -1;
    for(int i=0; i n)
        {
            printf("%d\n", i);
            return 0;
        }
    }
    printf("%d\n", v.size());
    
    return 0;
}

 

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