题目描述 小蓝正在学习一门神奇的语言,这门语言中的单词都是由小写英文字母组
成,有些单词很长,远远超过正常英文单词的长度。小蓝学了很长时间也记不住一些单词,他准备不再完全记忆这些单词,而是根据单词中哪个字母出现得最多来分辨单词。
现在,请你帮助小蓝,给了一个单词后,帮助他找到出现最多的字母和这 个字母出现的次数。
输入描述
输入一行包含一个单词,单词只由小写英文字母组成。 对于所有的评测用例,输入的单词长度不超过 1000。
输出描述
输出两行,第一行包含一个英文字母,表示单词中出现得最多的字母是哪 个。如果有多个字母出现的次数相等,输出字典序最小的那个。
第二行包含一个整数,表示出现得最多的那个字母在单词中出现的次数。
输入输出样例
示例 1
输入
lanqiao
输出
a
2
示例 2
输入
longlonglongistoolong
输出
o
6
运行限制
最大运行时间:1s
最大运行内存: 256M
#include <iostream>
using namespace std;
#include <string>
int main()
{
// 请在此输入您的代码
string str;
getline(cin, str);
int len = strlen(&str[0]);
int num[26] = { 0 };
int i = 0;
for ( i = 0; i < len; i++)
{
switch (str[i])
{
case 'a': num[0]++; break;
case 'b': num[1]++; break;
case 'c': num[2]++; break;
case 'd': num[3]++; break;
case 'e': num[4]++; break;
case 'f': num[5]++; break;
case 'g': num[6]++; break;
case 'h': num[7]++; break;
case 'i': num[8]++; break;
case 'j': num[9]++; break;
case 'k': num[10]++; break;
case 'l': num[11]++; break;
case 'm': num[12]++; break;
case 'n': num[13]++; break;
case 'o': num[14]++; break;
case 'p': num[15]++; break;
case 'q': num[16]++; break;
case 'r': num[17]++; break;
case 's': num[18]++; break;
case 't': num[19]++; break;
case 'u': num[20]++; break;
case 'v': num[21]++; break;
case 'w': num[22]++; break;
case 'x': num[23]++; break;
case 'y': num[24]++; break;
case 'z': num[25]++; break;
default:break;
}
}
int max = 0;
int j = 0;
int k = 0;
string temp;
//求num数组中的最大值
for (j = 0; j < 26; j++)
{
if (max < num[j])
max = num[j];
}
//未考虑重复有最大字母出现的情况
if (max == num[0] && k == 0)
{
temp = 'a' + 0;
cout << temp << endl;
cout << max << endl;
k++;
}
if (max == num[1] && k == 0)
{
temp = 'a' + 1;
cout << temp << endl;
cout << max << endl;
k++;
}
if (max == num[2] && k == 0)
{
temp = 'a' + 2;
cout << temp << endl;
cout << max << endl;
k++;
}
if (max == num[3] && k == 0)
{
temp = 'a' + 3;
cout << temp << endl;
cout << max << endl;
k++;
}
if (max == num[4] && k == 0)
{
temp = 'a' + 4;
cout << temp << endl;
cout << max << endl;
k++;
}
if (max == num[5] && k == 0)
{
temp = 'a' + 5;
cout << temp << endl;
cout << max << endl;
k++;
}
if (max == num[6] && k == 0)
{
temp = 'a' + 6;
cout << temp << endl;
cout << max << endl;
k++;
}
if (max == num[7] && k == 0)
{
temp = 'a' + 7;
cout << temp << endl;
cout << max << endl;
k++;
}
if (max == num[8] && k == 0)
{
temp = 'a' + 8;
cout << temp << endl;
cout << max << endl;
k++;
}
if (max == num[9] && k == 0)
{
temp = 'a' + 9;
cout << temp << endl;
cout << max << endl;
k++;
}
if (max == num[10] && k == 0)
{
temp = 'a' + 10;
cout << temp << endl;
cout << max << endl;
k++;
}
if (max == num[11] && k == 0)
{
temp = 'a' + 11;
cout << temp << endl;
cout << max << endl;
k++;
}
if (max == num[12] && k == 0)
{
temp = 'a' + 12;
cout << temp << endl;
cout << max << endl;
k++;
}
if (max == num[13] && k == 0)
{
temp = 'a' + 13;
cout << temp << endl;
cout << max << endl;
k++;
}
if (max == num[14] && k == 0)
{
temp = 'a' + 14;
cout << temp << endl;
cout << max << endl;
k++;
}
if (max == num[15] && k == 0)
{
temp = 'a' + 15;
cout << temp << endl;
cout << max << endl;
k++;
}
if (max == num[16] && k == 0)
{
temp = 'a' + 16;
cout << temp << endl;
cout << max << endl;
k++;
}
if (max == num[17] && k == 0)
{
temp = 'a' + 17;
cout << temp << endl;
cout << max << endl;
k++;
}
if (max == num[18] && k == 0)
{
temp = 'a' + 18;
cout << temp << endl;
cout << max << endl;
k++;
}
if (max == num[19] && k == 0)
{
temp = 'a' + 19;
cout << temp << endl;
cout << max << endl;
k++;
}
if (max == num[20] && k == 0)
{
temp = 'a' + 20;
cout << temp << endl;
cout << max << endl;
k++;
}
if (max == num[21] && k == 0)
{
temp = 'a' + 21;
cout << temp << endl;
cout << max << endl;
k++;
}
if (max == num[22] && k == 0)
{
temp = 'a' + 22;
cout << temp << endl;
cout << max << endl;
k++;
}
if (max == num[23] && k == 0)
{
temp = 'a' + 23;
cout << temp << endl;
cout << max << endl;
k++;
}
if (max == num[24] && k == 0)
{
temp = 'a' + 24;
cout << temp << endl;
cout << max << endl;
k++;
}
if (max == num[25] && k == 0)
{
temp = 'a' + 25;
cout << temp << endl;
cout << max << endl;
}
return 0;
}