文章

16

粉丝

0

获赞

41

访问

1.4k

头像
单词识别 题解:c++,按map中的次数排序
P1452 北京理工大学机试题
发布于2026年3月23日 15:58
阅读数 52

#include<bits/stdc++.h>
using namespace std;

map<string, int> m;

bool cmp(string a,string b){
    int count_a = m[a];
    int count_b = m[b];
    if(count_a != count_b)return count_a > count_b;//次数多的排在前面
    else return a < b;//次数一样按字典序排序
}
int main(){
    string s;
    getline(cin,s);
    
    vector<string> v;
    string news = "";
    for(int i=0;i<s.size();i++){
        s[i] = tolower(s[i]);
        if(isalpha(s[i]))news += s[i];//如果是字母就拼接成字符串
        else {//遇见空格或者句号或者其他字符,非字母
            if(news!=""){//如果此时已有拼接成的单词
                if(m.find(news) == m.end()){//如果map中没有...

登录查看完整内容


登录后发布评论

暂无评论,来抢沙发