0
点赞
收藏
分享

微信扫一扫

Acwing 1557. 说话方式

追风骚年 2022-04-03 阅读 46

#include <iostream>
#include <unordered_map>
using namespace std;

bool check(char c)
{
    if(c>='0'&&c<='9') return true;
    if(c>='A'&&c<='Z') return true;
    if(c>='a'&&c<='z') return true;
    return false;
}

char exchange(char d)
{
    if(d>='A'&&d<='Z')
    {
        return d+32;
    }
    return d;
}
int main()
{
    string str;
    getline(cin,str);
    int j;
    unordered_map<string,int> hash;
    for(int i=0;i<str.size();i++)
    {
        if(check(str[i]))
        {
            string word;
            j=i; 
            while(j<str.size()&&check(str[j])){ word+=exchange(str[j++]);}
            hash[word]++;
            i=j;
        }
    }
        string word;
        int cnt=-1;
        for(auto item:hash)
        {
            if(item.second>cnt||(item.second==cnt&&item.first<word))//这个部分是优先级问题,如果出现的次数相同,这个时候选择单词短的,否则选择次数多的。
            {
                word=item.first;
                cnt=item.second;
            }
        }
        cout<<word<<' '<<cnt<<endl;

    
    
}

 //tolower函数,变成小写;

//  unordered_map<string,int> hash;

 for(auto item:hash)
        {
            if(item.second>cnt||(item.second==cnt&&item.first<word))//这个部分是优先级问题,如果出现的次数相同,这个时候选择单词短的,否则选择次数多的。
            {
                word=item.first;
                cnt=item.second;
            }
        }

举报

相关推荐

0 条评论