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

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 characters at most.

Setsuna has 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 and 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 .

The second line contains keywords separated by exactly one space. The length of each keyword is no more than . 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
提示
In sample , it takes characters to write all the words down. So Setsuna can write down at most four.

In sample , there is only one keyword.

排个序就好了

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 

using namespace std;

typedef long long ll;

const int maxn = 1005;
string str[maxn];

int cmp(string a,string b) {
    if(a.size() == b.size()) return a < b;
    return a.size() < b.size();
}

int main() {
    int n,m;scanf("%d%d",&n,&m);
    for(int i = 1;i <= m;i++) {
        cin >> str[i];
    }
    
    sort(str + 1,str + 1 + m,cmp);
    
    string pre;
    int ans = 0;
    for(int i = 1;i <= m;i++) {
        if(i == 1) {
            if(str[i].size() <= n) {
                ans++;
                n -= str[i].size();
            }
            else break;
        }
        else {
            if(str[i] == pre) {
                continue;
            }
            else if(str[i].size() + 1 <= n){
                ans++;
                n -= str[i].size() + 1;
            }
            else break;
        }
        pre = str[i];
    }
    
    printf("%d\n",ans);
    return 0;
}

你可能感兴趣的:(#,其他比赛题目)