【1071 25 map】 Speech Patterns

传送门

题意

给定一行字符串\(s\),统计\(s\)中出现的单词次数最多的,不区分字母的大小写,单词中的字母为\(0\sim 9,a\sim z,A\sim Z\)

数据范围

\(|s|\leq 1048576\)

题解

  • map 统计即可
  • 只有当统计到的非空才加入map
  • 当统计到末尾的时候也需要添加一次

Code

#include<bits/stdc++.h>
using namespace std;
#define fi first
#define se second
map<string,int>mp;
bool check(char c){
    if(c<='z' && c>='a') return 1;
    else if(c<='Z'&&c>='A') return 1;
    else if(c<='9' && c>='0') return 1;
    return 0;
}
char get(char c){
    if(c<='Z'&&c>='A') return c-'A'+'a';
    return c;
}
int main(){
    string s; getline(cin,s);
    string words="";
    for(int i=0;i<s.size();i++){
        if(check(s[i])) words+=get(s[i]);
        if(!check(s[i]) || i==s.size()-1){
            if(words!="") mp[words]++;
            words="";
        }
    }
    pair<string,int>ans={" ",0};
    for(auto it:mp) if(it.se>ans.se) ans={it.fi,it.se};
    cout<<ans.fi<<' '<<ans.se<<endl;
}
posted @ 2021-03-02 21:26  Hyx'  阅读(53)  评论(0)    收藏  举报