文章

58

粉丝

66

获赞

58

访问

3.9k

头像
字母频率 (C++11 哈希表)题解:
P1019 贵州大学机试题
发布于2024年4月20日 16:09
阅读数 74

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

unordered_map<char, int> res;

int main()
{
   	string str;
   	
   	getline(cin, str);
   	
   	for(int i = 0; i < str.size(); i ++)
   	{
   		if(str[i] >= 'A' && str[i] <= 'Z' )
			res[str[i] + 32] ++;
		if(str[i] >= 'a' && str[i] <= 'z' )  
			res[str[i]] ++; 	
	}
	
	int ans = 0;
	char c;
	
	for(auto i : res)
	{
		ans = max(ans, i.second);
		if(ans == i.second) c = i.first;
	}
	
	cout << c << ' ' << ans;
	
    return 0;
}

 

登录查看完整内容


登录后发布评论

暂无评论,来抢沙发